dansrocks / treefinder
树查找实用工具
dev-master
2019-04-01 22:21 UTC
Requires
- php: >=7.2
- ext-mbstring: ^7.2
Requires (Dev)
- phpunit/phpunit: ^8.1@dev
This package is not auto-updated.
Last update: 2024-09-20 02:14:36 UTC
README
树查找实用工具
示例
定义闭包
$compareStrings = function (string $str1, string $str2) {
$minChars = min(mb_strlen($str1), mb_strlen($str2));
$changes = levenshtein($str1, $str2);
$percent = (($minChars - $changes) / $minChars) * 100;
return intval($percent);
};
然后,我们尝试将几个字符串与之前的闭包进行比较
$treefinder = new \TreeFinder\TreeFinder($compareStrings, 50);
$coincidences = $treefinder->findCoincidences(
['carrot', 'pastilla', 'comanda' ],
['comando', 'pastillero', 'carotida' ]
);
最后,你可以看到结果
print_r($coincidences);
然后,你应该看到...
treefinder$ php main.php
Ejecutando coincidencias entre (carrot, comando)
Ejecutando coincidencias entre (carrot, pastillero)
Ejecutando coincidencias entre (carrot, carotida)
Ejecutando coincidencias entre (pastilla, comando)
Ejecutando coincidencias entre (pastilla, pastillero)
Ejecutando coincidencias entre (pastilla, carotida)
Ejecutando coincidencias entre (comanda, comando)
Ejecutando coincidencias entre (comanda, pastillero)
Ejecutando coincidencias entre (comanda, carotida)
Array
(
[0] => Array
(
[left-pos] => 1
[right-pos] => 1
[left] => pastilla
[right] => pastillero
[coincidence] => 62
)
[1] => Array
(
[left-pos] => 2
[right-pos] => 0
[left] => comanda
[right] => comando
[coincidence] => 85
)
)