Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

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

Input: kava
Output: java

Input: ruby
Output: ruby

Input: clujuro
Output: clojure

etc.. 

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

share|improve this question
1  
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
3  
@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
1  
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
add comment

2 Answers

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
add comment

You could use google's "did you mean?" API...
http://pygoogle.sourceforge.net/dist/doc/public/google-module.html#doSpellingSuggestion

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

Your Answer

 
discard

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.