I have an input string and a collection of strings and I want to compare the input string and find the best choices to view.I want to write a function that tell me how much this two string are similar.have you any idea?
2 Answers
For 'short' string differences the algorithm you are searching for is called:
Levenshtein distance
http://en.wikipedia.org/wiki/Levenshtein_distance
For seeking differences in sentences you may wish to check for algorithms that solve the 'longest common sequence' problem.
One tool that does that is the (originally unix) 'diff'

- 7,318
- 1
- 31
- 61
Well, Perl has a String::Approx function which seems that does the job for you. But in general, you might want to take a look at: Levenshtein Distance algorithm.
Some other good resources are (Mostly explained in C#):
Comparing strings with tolerance
http://mihkeltt.blogspot.com/2009/04/dameraulevenshtein-distance.html
http://www.perlmonks.org/?node=Levenshtein%20distance%3A%20calculating%20similarity%20of%20strings
http://www.dotnetperls.com/levenshtein