3

我一直在阅读和测试 php levenshtein中的一些示例。比较 $input 和 $words 输出比较

$input = 'hw r u my dear angel';

    // array of words to check against
    $words  = array('apple','pineapple','banana','orange','how are you',
                    'radish','carrot','pea','bean','potato','hw are you');

输出

Input word: hw r u my dear angel
Did you mean: hw are you?

比较,删除在数组中的硬件。

$input = 'hw r u my dear angel';

    // array of words to check against
    $words  = array('apple','pineapple','banana','orange','how are you',
                    'radish','carrot','pea','bean','potato');

在第二次删除硬件中,您是否在数组输出中

Input word: hw r u my dear angel
Did you mean: orange? 

在哪里similar_text()

 echo '<br/>how are you:'.similar_text($input,'how are you');
    echo '<br/>orange:'.similar_text($input,'orange');
    echo '<br/>hw are you:'.similar_text($input,'hw are you');

how are you:6
orange:5
hw are you:6

在第二次比较时,为什么它输出橙色,当你好吗还有 6 个类似的文本,比如好吗?有什么方法可以改进或更好的方法吗?我也将所有可能的输入保存在数据库中。我应该查询它并存储array然后用于foreach获取levenshtein distance吗?但如果有数百万,那会很慢。

代码

  <?php
    // input misspelled word
    $input = 'hw r u my dear angel';

    // array of words to check against
    $words  = array('apple','pineapple','banana','orange','how are you',
                    'radish','carrot','pea','bean','potato','hw are you');


    // no shortest distance found, yet
    $shortest = -1;

    $closest = closest($input,$words,$shortest);


    echo "Input word: $input<br/>";
    if ($shortest == 0) {
        echo "Exact match found: $closest\n";
    } else {
        echo "Did you mean: $closest?\n";
    }
    echo '<br/><br/>';

    $shortest = -1;
    $words  = array('apple','pineapple','banana','orange','how are you',
                    'radish','carrot','pea','bean','potato');
    $closest = closest($input,$words,$shortest);
    echo "Input word: $input<br/>";
    if ($shortest == 0) {
        echo "Exact match found: $closest\n";
    } else {
        echo "Did you mean: $closest?\n";
    }

    echo '<br/><br/>';
    echo 'Similar text';
    echo '<br/>how are you:'.similar_text($input,'how are you');
    echo '<br/>orange:'.similar_text($input,'orange');
    echo '<br/>hw are you:'.similar_text($input,'hw are you');



    function closest($input,$words,&$shortest){
        // loop through words to find the closest
    foreach ($words as $word) {

        // calculate the distance between the input word,
        // and the current word
        $lev = levenshtein($input, $word);

        // check for an exact match
        if ($lev == 0) {

            // closest word is this one (exact match)
            $closest = $word;
            $shortest = 0;

            // break out of the loop; we've found an exact match
            break;
        }

        // if this distance is less than the next found shortest
        // distance, OR if a next shortest word has not yet been found
        if ($lev <= $shortest || $shortest < 0) {
            // set the closest match, and shortest distance
            $closest  = $word;
            $shortest = $lev;
        }


    }
    return $closest;
    }
    ?>
4

2 回答 2

8

首先,输出什么并不重要similar_text(),因为它使用另一种算法来计算字符串之间的相似度。

让我们试着理解为什么会这样levenstein()想,我亲爱的安琪更接近橙色而不是“你好吗”。维基百科对莱文斯坦距离有一个很好的定义。

通俗地说,两个单词之间的 Levenshtein 距离是将一个单词更改为另一个单词所需的最小单字符编辑(插入、删除、替换)次数。

现在让我们计算一下我们需要进行多少次编辑才能将hw ru my Dear angel更改为orange

  1. hw ru my Dear angel → hw ru my Dear angel (删除最后一个字符)
  2. hw ru my Dear ange → hw ru my dearange(删除最后一个空格)
  3. hw ru my dearange → arange(删除前 12 个字符)
  4. arange → 橙色(用 o 代替 a)

因此,将hw ru my Dear angel1 + 1 + 12 + 1 = 15更改为orange需要全部编辑。

这是把亲爱的天使变成你好吗

  1. hw ru my Dear angel → how ru my Dear angel(插入o字符)
  2. How ru my Dear angel → How Dear 天使 (删掉7个字)
  3. how Dear angel → how ar angel (删掉2个字)
  4. how ar angel → how are angel(插入 e 字符)
  5. how are angel → how are ang (删除最后 2 个字符)
  6. how are ang → how are you (替换最后 3 个字符)

总编辑1 + 7 + 2 + 1 + 5 = 16。所以你可以看到莱文斯坦距离橙色更接近我亲爱的天使;-)

于 2013-09-12T07:49:37.897 回答
1

找到了一些东西.. 但使用 mysql http://www.artfulsoftware.com/infotree/queries.php#552 我认为这比在 php 上处理它要好得多,因为我的数据存储在数据库中。

CREATE FUNCTION levenshtein( s1 VARCHAR(255), s2 VARCHAR(255) )
  RETURNS INT
  DETERMINISTIC
  BEGIN
    DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
    DECLARE s1_char CHAR;
    -- max strlen=255
    DECLARE cv0, cv1 VARBINARY(256);
    SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = 0x00, j = 1, i = 1, c = 0;
    IF s1 = s2 THEN
      RETURN 0;
    ELSEIF s1_len = 0 THEN
      RETURN s2_len;
    ELSEIF s2_len = 0 THEN
      RETURN s1_len;
    ELSE
      WHILE j <= s2_len DO
        SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
      END WHILE;
      WHILE i <= s1_len DO
        SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
        WHILE j <= s2_len DO
          SET c = c + 1;
          IF s1_char = SUBSTRING(s2, j, 1) THEN 
            SET cost = 0; ELSE SET cost = 1;
          END IF;
          SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
          IF c > c_temp THEN SET c = c_temp; END IF;
            SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
            IF c > c_temp THEN 
              SET c = c_temp; 
            END IF;
            SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
        END WHILE;
        SET cv1 = cv0, i = i + 1;
      END WHILE;
    END IF;
    RETURN c;
  END;

还有一个辅助功能:

CREATE FUNCTION levenshtein_ratio( s1 VARCHAR(255), s2 VARCHAR(255) )
  RETURNS INT
  DETERMINISTIC
  BEGIN
    DECLARE s1_len, s2_len, max_len INT;
    SET s1_len = LENGTH(s1), s2_len = LENGTH(s2);
    IF s1_len > s2_len THEN 
      SET max_len = s1_len; 
    ELSE 
      SET max_len = s2_len; 
    END IF;
    RETURN ROUND((1 - LEVENSHTEIN(s1, s2) / max_len) * 100);
  END; 

但我不知道为什么这会导致错误You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5

于 2013-09-12T08:57:54.633 回答