Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working over a OCR Application where I need to select a option based on user text input.

Available Options:
["python", "ruby", "java", "clojure", "haskell"]

Input: kava
Output: java

Input: ruby
Output: ruby

Input: clujuro
Output: clojure


my application is python based, is there a algorithm already exist to solve this problem.

share|improve this question
It may not be worth it to correct now but IMO a more appropriate title would be "how to determine distance (difference) between strings" – Jeff Welling Aug 8 '11 at 9:22
@Jeff: and then I would never find this question, because I wouldn't have any idea that what I need might be called "the distance difference"... And that's not the only reason not to put the solution you need in the question. It may preclude people from suggesting alternate solutions to the problem you are actually having. Case in point: loudsight's answer. – Marjan Venema Aug 8 '11 at 10:28
My point was, now that you do know, using a more accurate title may help attract answers from people who don't know what "how to select a most probable..." means but who would recognize "distance between strings", but to each his own. It was just a suggestion. I didn't know what you meant and nearly didn't click on your question because of it is all. – Jeff Welling Aug 8 '11 at 11:13
up vote 10 down vote accepted

This is done by algorithms of the "approximate string matching" category. A common measure to compare two strings is called the Levenshtein distance. There's a python implementation available here.

You basically compare the input to the possible outputs and choose the one with the smallest distance to the desired output.

The wikipedia article also mentions various other algorithms which you can try in case you need something more specific.

share|improve this answer
I had a similar problem in ruby and this is exactly how I solved it. – Jeff Welling Aug 8 '11 at 9:18

You could use google's "did you mean?" API...

share|improve this answer
+1 for being creative. I bet it'd work. – Falcon Aug 8 '11 at 9:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.