Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string comparison with the most similar string

does anyone know if exist an algorithm that given one string A and an array of strings B, compares the A string with all the strings in B giving in output the most similar one.

For "the most similar one" I mean that for example,

if the A string is: "hello world how are you"

then

"asdf asdewr hello world how asfrqr you"

is more similar than:

"h2ll4 w1111 h11 111 111"

like image 770
malilzap Avatar asked May 02 '11 19:05

malilzap


People also ask

How do you compare similar strings?

The way to check the similarity between any data point or groups is by calculating the distance between those data points. In textual data as well, we check the similarity between the strings by calculating the distance between one text to another text.

Can we use == for string comparison?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

How do you compare two strings to see if they match?

In order to compare two strings, we can use String's strcmp() function. The strcmp() function is a C library function used to compare two strings in a lexicographical manner. The function returns 0 if both the strings are equal or the same.

Can you use comparison operators with strings?

The comparison operators also work on strings. To see if two strings are equal you simply write a boolean expression using the equality operator. print("Yes, we have bananas!")


2 Answers

The usual measurement for this is the Levenshtein distance. Compute the Levenshtein distance from the original to each candidate, and take the smallest distance as the most likely candidate.

like image 124
Jerry Coffin Avatar answered Sep 18 '22 23:09

Jerry Coffin


Define similarity. Algorithms that can do this include:

  1. Levenshtein/LCS/n-gram distance (compare the string with each of the strings in your set, take the one with lowest distance)
  2. tf-idf indexing
  3. Levenshtein automata
  4. Hopfield networks
  5. BK-trees

All of which can feasibly by implemented in C or C++. Google "string similarity", "duplicate finding" or "record linkage" for the available metrics and algorithms.

like image 42
Fred Foo Avatar answered Sep 17 '22 23:09

Fred Foo