2

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?

Saleh
  • 2,982
  • 5
  • 34
  • 59
  • possible duplicate of [Comparing strings with tolerance](http://stackoverflow.com/questions/2344320/comparing-strings-with-tolerance) – nawfal May 30 '13 at 08:33

2 Answers2

10

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'

Marino Šimić
  • 7,318
  • 1
  • 31
  • 61
1

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

Community
  • 1
  • 1
Kamyar
  • 18,639
  • 9
  • 97
  • 171