|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeoFixer\helpers; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class FuzzySearchHelper |
|
7
|
|
|
* |
|
8
|
|
|
* @package GeoFixer\helpers |
|
9
|
|
|
*/ |
|
10
|
|
|
class FuzzySearchHelper |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
public $similarity = 0; |
|
14
|
|
|
public $meta_similarity = 0; |
|
15
|
|
|
public $min_levenshtein = 1000; |
|
16
|
|
|
public $meta_min_levenshtein = 1000; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Строгий поиск |
|
20
|
|
|
* |
|
21
|
|
|
* @param $word |
|
22
|
|
|
* @param $translited_words |
|
23
|
|
|
* |
|
24
|
|
|
* @return bool|mixed |
|
25
|
|
|
*/ |
|
26
|
9 |
|
public function findBestMatch($word, $translited_words) |
|
27
|
|
|
{ |
|
28
|
9 |
|
$words_array = []; |
|
29
|
9 |
|
foreach ($translited_words as $russian => $translit) { |
|
30
|
9 |
|
if (levenshtein(metaphone($word), metaphone($translit)) < mb_strlen(metaphone($word)) / 2) { |
|
31
|
2 |
|
if (levenshtein($word, $translit) < mb_strlen($word) / 2) { |
|
32
|
2 |
|
$words_array[$russian] = $translit; |
|
33
|
2 |
|
} |
|
34
|
2 |
|
} |
|
35
|
9 |
|
} |
|
36
|
|
|
|
|
37
|
9 |
|
$most_similar = $this->findMostSimilarWords($word, $words_array); |
|
38
|
9 |
|
$this->findFinalSimilirityAndLevenshtein($most_similar, $word); |
|
39
|
|
|
|
|
40
|
9 |
|
foreach ($most_similar as $russian => $latin) { |
|
41
|
2 |
|
if (levenshtein(metaphone($latin), metaphone($word)) <= $this->meta_min_levenshtein) { |
|
42
|
2 |
|
if (similar_text(metaphone($latin), metaphone($word)) >= $this->meta_similarity) { |
|
43
|
2 |
|
$meta_result[$russian] = $latin; |
|
|
|
|
|
|
44
|
2 |
|
} |
|
45
|
2 |
|
} |
|
46
|
9 |
|
} |
|
47
|
|
|
|
|
48
|
9 |
|
return @key($meta_result) ? @key($meta_result) : false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Нестрогий поиск |
|
53
|
|
|
* |
|
54
|
|
|
* @param $word |
|
55
|
|
|
* @param $words_array |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
19 |
|
public function findMostSimilarWords($word, $words_array) |
|
59
|
|
|
{ |
|
60
|
19 |
|
$this->min_levenshtein = 1000; |
|
61
|
19 |
|
$this->similarity = 0; |
|
62
|
|
|
|
|
63
|
19 |
|
$result = []; |
|
64
|
|
|
|
|
65
|
19 |
|
foreach ($words_array as $russian => $translit) { |
|
66
|
12 |
|
if (levenshtein($translit, $word) <= $this->min_levenshtein) { |
|
67
|
12 |
|
if (similar_text($translit, $word) >= $this->similarity) { |
|
68
|
12 |
|
$this->min_levenshtein = levenshtein($translit, $word); |
|
69
|
12 |
|
$this->similarity = similar_text($translit, $word); |
|
70
|
12 |
|
$result = []; |
|
71
|
12 |
|
$result[$russian] = $translit; |
|
72
|
12 |
|
} |
|
73
|
12 |
|
} |
|
74
|
19 |
|
} |
|
75
|
|
|
|
|
76
|
19 |
|
return $result; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Вспомогательный метод для findBestMatch |
|
81
|
|
|
* |
|
82
|
|
|
* @param $most_similar |
|
83
|
|
|
* @param $word |
|
84
|
|
|
*/ |
|
85
|
9 |
|
private function findFinalSimilirityAndLevenshtein($most_similar, $word) |
|
86
|
|
|
{ |
|
87
|
9 |
|
foreach ($most_similar as $item) { |
|
88
|
2 |
|
$this->meta_min_levenshtein = min($this->meta_min_levenshtein, levenshtein(metaphone($item), metaphone($word))); |
|
89
|
9 |
|
} |
|
90
|
|
|
|
|
91
|
9 |
|
foreach ($most_similar as $item) { |
|
92
|
2 |
|
if (levenshtein($item, $word) == $this->meta_min_levenshtein) { |
|
93
|
2 |
|
$this->meta_similarity = max($this->meta_similarity, similar_text(metaphone($item), metaphone($word))); |
|
94
|
2 |
|
} |
|
95
|
9 |
|
} |
|
96
|
9 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.