php : word proximity script?

前端 未结 3 857
臣服心动
臣服心动 2021-01-27 05:21

Okay - so, I\'ve spent ages searching in Google, and even went through a few specific searches at hotscripts etc., several php forums and this place ... nothing (not of use anyw

3条回答
  •  囚心锁ツ
    2021-01-27 05:58

    If you are speaking about specific word comparisons, you will want to look at the SOUNDEX function of MySQL. (I will assume you may be using mysql). When comparing two words, you can get a reference to how they sound:

    SELECT `word` FROM `list_of_words` WHERE SOUNDEX(`word`) = SOUNDEX('{TEST_WORD}');
    

    Then when you get your list of words (as most likely you will get quite a few), you cna check the distance between those words for the word that is CLOSEST (or the group of words depending on how you write your code).

    $word = '{WORD TO CHECK}';
    $distance = 4; // the smalled the distance the closed the word
    foreach($word_results as $comparison_word) {
       $distance = levenshtein($comparison_word, $word);
       if($distance < $threshold) {
          $threshold = $distance;
          $similar_word = $comparison_word;
       }
    }
    echo $similar_word;
    

    Hope that helps you find the direction you are looking for.

    Happy coding!

提交回复
热议问题