Levenshtein distance: how to better handle words swapping positions?

前端 未结 9 1484
忘掉有多难
忘掉有多难 2021-01-30 02:22

I\'ve had some success comparing strings using the PHP levenshtein function.

However, for two strings which contain substrings that have swapped positions, the algorithm

9条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-30 03:08

    You can also try this. (just an extra suggestion)

    $one = metaphone("The quick brown fox"); // 0KKBRNFKS
    $two = metaphone("brown quick The fox"); // BRNKK0FKS
    $three = metaphone("The quiet swine flu"); // 0KTSWNFL
    
    similar_text($one, $two, $percent1); // 66.666666666667
    similar_text($one, $three, $percent2); // 47.058823529412
    similar_text($two, $three, $percent3); // 23.529411764706
    

    This will show that the 1st and 2nd are more similar than one and three and two and three.

提交回复
热议问题