henrik9999/string-similarity

根据Dice系数找出两个字符串之间的相似度,这通常比Levenshtein距离更优。

1.0.1 2022-06-21 15:56 UTC

This package is not auto-updated.

Last update: 2024-09-26 20:53:22 UTC


README

基于Dice系数找出两个字符串之间的相似度,这通常比Levenshtein距离更优。

此实现实际上将一个双词组的多次出现视为唯一。当获取"GG"和"GGGGGGGG"之间的相似度时,可以最清楚地看到这种行为的正确性,显然不应为1。

这是Node.js包string-similarity的PHP实现。

用法

使用以下命令安装

composer require henrik9999/string-similarity

在您的代码中

$stringSimilarity = new StringSimilarity();

$similarity = $stringSimilarity->compareTwoStrings("healed", "sealed");

$matches = $stringSimilarity->findBestMatch("healed", [
  "edward",
  "sealed",
  "theatre",
]);

API

该包包含两个方法

compareTwoStrings(string $string1, string $string2, bool $casesensitive)

返回一个介于0和1之间的分数,表示两个字符串之间的相似度。0表示完全不同的字符串,1表示相同的字符串。默认情况下,比较是区分大小写的。

参数
  1. string1 (string): 第一个字符串
  2. string2 (string): 第二个字符串
  3. casesensitive (bool): 如果比较应区分大小写

顺序无关紧要。

返回

(number): 从0到1的分数(包括两端)。数字越高表示相似度越高。

示例
$stringSimilarity->compareTwoStrings("healed", "sealed");
// → 0.8

$stringSimilarity->compareTwoStrings(
  "Olive-green table for sale, in extremely good condition.",
  "For sale: table in very good  condition, olive green in colour."
);
// → 0.6060606060606061

$stringSimilarity->compareTwoStrings(
  "Olive-green table for sale, in extremely good condition.",
  "For sale: green Subaru Impreza, 210,000 miles"
);
// → 0.2558139534883721

$stringSimilarity->compareTwoStrings(
  "Olive-green table for sale, in extremely good condition.",
  "Wanted: mountain bike with at least 21 gears."
);
// → 0.1411764705882353

findBestMatch(string mainString, array targetStrings, bool $casesensitive)

比较mainStringtargetStrings中的每个字符串。

参数
  1. mainString (string): 要与每个目标字符串进行比较的字符串。
  2. targetStrings (array): 此数组中的每个字符串都将与主字符串进行比较。
  3. casesensitive (bool): 如果比较应区分大小写。
返回

(Object): 一个对象,具有ratings属性,为每个目标字符串提供相似度评分,bestMatch属性指定最相似的目标字符串,以及bestMatchIndex属性指定bestMatch在targetStrings数组中的索引。

示例
$stringSimilarity->findBestMatch('Olive-green table for sale, in extremely good condition.', [
  'For sale: green Subaru Impreza, 210,000 miles',
  'For sale: table in very good condition, olive green in colour.',
  'Wanted: mountain bike with at least 21 gears.'
]);
// →
array(3) {
  ["ratings"]=>
  array(3) {
    [0]=>
    array(2) {
      ["target"]=>
      string(45) "For sale: green Subaru Impreza, 210,000 miles"
      ["rating"]=>
      float(0.2558139534883721)
    }
    [1]=>
    array(2) {
      ["target"]=>
      string(62) "For sale: table in very good condition, olive green in colour."
      ["rating"]=>
      float(0.6060606060606061)
    }
    [2]=>
    array(2) {
      ["target"]=>
      string(45) "Wanted: mountain bike with at least 21 gears."
      ["rating"]=>
      float(0.1411764705882353)
    }
  }
  ["bestMatch"]=>
  array(2) {
    ["target"]=>
    string(62) "For sale: table in very good condition, olive green in colour."
    ["rating"]=>
    float(0.6060606060606061)
  }
  ["bestMatchIndex"]=>
  int(1)
}

发布说明

1.0.1

  • 进行了一些性能改进

1.0.0

  • 初始发布