����JFIF��x�x����'
| Server IP : 78.140.185.180 / Your IP : 216.73.216.170 Web Server : LiteSpeed System : Linux cpanel13.v.fozzy.com 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64 User : builderbox ( 1072) PHP Version : 7.3.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /proc/thread-self/root/home/builderbox/././www/vendor/teamtnt/tntsearch/src/ |
Upload File : |
<?php
namespace TeamTNT\TNTSearch;
class TNTFuzzyMatch
{
public function norm($vec)
{
$norm = 0;
$components = count($vec);
for ($i = 0; $i < $components; $i++) {
$norm += $vec[$i] * $vec[$i];
}
return sqrt($norm);
}
public function dot($vec1, $vec2)
{
$prod = 0;
$components = count($vec1);
for ($i = 0; $i < $components; $i++) {
$prod += ($vec1[$i] * $vec2[$i]);
}
return $prod;
}
public function wordToVector($word)
{
$alphabet = "aAbBcCčČćĆdDđĐeEfFgGhHiIjJkKlLmMnNoOpPqQrRsSšŠtTvVuUwWxXyYzZžŽ1234567890'+ /";
$result = [];
foreach (str_split($word) as $w) {
$result[] = strpos($alphabet, $w) + 1000000;
}
return $result;
}
public function angleBetweenVectors($a, $b)
{
$denominator = ($this->norm($a) * $this->norm($b));
if ($denominator == 0) {
return 0;
}
return $this->dot($a, $b) / $denominator;
}
public function hasCommonSubsequence($pattern, $str)
{
$pattern = mb_strtolower($pattern);
$str = mb_strtolower($str);
$j = 0;
$patternLength = strlen($pattern);
$strLength = strlen($str);
for ($i = 0; $i < $strLength && $j < $patternLength; $i++) {
if ($pattern[$j] == $str[$i]) {
$j++;
}
}
return ($j == $patternLength);
}
public function makeVectorSameLength($str, $pattern)
{
$j = 0;
$max = max(count($pattern), count($str));
$a = [];
$b = [];
for ($i = 0; $i < $max && $j < $max; $i++) {
if (isset($pattern[$j]) && isset($str[$i]) && $pattern[$j] == $str[$i]) {
$j++;
$b[] = $str[$i];
} else {
$b[] = 0;
}
}
return $b;
}
public function fuzzyMatchFromFile($pattern, $path)
{
$res = [];
$lines = fopen($path, "r");
if ($lines) {
while (!feof($lines)) {
$line = rtrim(fgets($lines, 4096));
if ($this->hasCommonSubsequence($pattern, $line)) {
$res[] = $line;
}
}
fclose($lines);
}
$paternVector = $this->wordToVector($pattern);
$sorted = [];
foreach ($res as $caseSensitiveWord) {
$word = mb_strtolower(trim($caseSensitiveWord));
$wordVector = $this->wordToVector($word);
$normalizedPaternVector = $this->makeVectorSameLength($wordVector, $paternVector);
$angle = $this->angleBetweenVectors($wordVector, $normalizedPaternVector);
if (strpos($word, $pattern) !== false) {
$angle += 0.2;
}
$sorted[$caseSensitiveWord] = $angle;
}
arsort($sorted);
return $sorted;
}
public function fuzzyMatch($pattern, $items)
{
$res = [];
foreach ($items as $item) {
if ($this->hasCommonSubsequence($pattern, $item)) {
$res[] = $item;
}
}
$paternVector = $this->wordToVector($pattern);
$sorted = [];
foreach ($res as $word) {
$word = trim($word);
$wordVector = $this->wordToVector($word);
$normalizedPaternVector = $this->makeVectorSameLength($wordVector, $paternVector);
$angle = $this->angleBetweenVectors($wordVector, $normalizedPaternVector);
if (strpos($word, $pattern) !== false) {
$angle += 0.2;
}
$sorted[$word] = $angle;
}
arsort($sorted);
return $sorted;
}
}