|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of gpupo/similarity |
|
5
|
|
|
* Created by Gilmar Pupo <[email protected]> |
|
6
|
|
|
* For the information of copyright and license you should read the file |
|
7
|
|
|
* LICENSE which is distributed with this source code. |
|
8
|
|
|
* Para a informação dos direitos autorais e de licença você deve ler o arquivo |
|
9
|
|
|
* LICENSE que é distribuído com este código-fonte. |
|
10
|
|
|
* Para obtener la información de los derechos de autor y la licencia debe leer |
|
11
|
|
|
* el archivo LICENSE que se distribuye con el código fuente. |
|
12
|
|
|
* For more information, see <https://www.gpupo.com/>. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Gpupo\Similarity; |
|
16
|
|
|
|
|
17
|
|
|
class SimilarText extends SimilarityAbstract |
|
18
|
|
|
{ |
|
19
|
20 |
|
public function hasSimilarity() |
|
20
|
|
|
{ |
|
21
|
20 |
|
if ($this->getPercent() > $this->getAccuracy()) { |
|
22
|
4 |
|
return true; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
18 |
|
if ($this->isApproximate()) { |
|
26
|
9 |
|
return true; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
9 |
|
return false; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
27 |
|
public function getPercent() |
|
33
|
|
|
{ |
|
34
|
27 |
|
return $this->calculatePercentExtended( |
|
35
|
27 |
|
$this->getInput()->getFirst(), |
|
36
|
27 |
|
$this->getInput()->getSecond() |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
23 |
|
public function getLevenshteinDistance() |
|
41
|
|
|
{ |
|
42
|
23 |
|
return levenshtein( |
|
43
|
23 |
|
$this->getInput()->getFirst(), |
|
44
|
23 |
|
$this->getInput()->getSecond(), |
|
45
|
23 |
|
$this->getInput()->getCosts()->insertion, |
|
46
|
23 |
|
$this->getInput()->getCosts()->replacement, |
|
47
|
23 |
|
$this->getInput()->getCosts()->deletion |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
18 |
|
public function getLevenshteinHardDistance() |
|
52
|
|
|
{ |
|
53
|
18 |
|
return levenshtein( |
|
54
|
18 |
|
$this->getInput()->getFirst(), |
|
55
|
18 |
|
$this->getInput()->getSecond() |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
18 |
|
public function getProximityCalculation() |
|
60
|
|
|
{ |
|
61
|
|
|
$calc = [ |
|
62
|
18 |
|
'first' => $this->getInput()->getFirst(), |
|
63
|
18 |
|
'second' => $this->getInput()->getSecond(), |
|
64
|
|
|
]; |
|
65
|
18 |
|
$calc['limit'] = $this->getLimitOfProximity($calc['first'], $calc['second']); |
|
66
|
18 |
|
$calc['ld'] = $this->getLevenshteinDistance(); |
|
67
|
18 |
|
$calc['hardDistance'] = $this->getLevenshteinHardDistance(); |
|
68
|
18 |
|
$calc['hardDifference'] = ($calc['hardDistance'] / $calc['limit']['hardDivider']) + 0.5; |
|
69
|
|
|
|
|
70
|
18 |
|
if ($calc['hardDifference'] > $calc['ld'] |
|
71
|
18 |
|
&& $calc['hardDifference'] >= ($calc['limit']['maxDifference'] - 1)) { |
|
72
|
13 |
|
$calc['mode'] = 'hard'; |
|
73
|
13 |
|
$calc['difference'] = $calc['hardDifference']; |
|
74
|
|
|
} else { |
|
75
|
5 |
|
$calc['mode'] = 'soft'; |
|
76
|
5 |
|
$calc['difference'] = $calc['ld']; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
18 |
|
return $calc; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
18 |
|
protected function getLimitOfProximity($first, $second) |
|
83
|
|
|
{ |
|
84
|
|
|
$calc = [ |
|
85
|
18 |
|
'chars' => strlen($first.$second), |
|
86
|
18 |
|
'divider' => (20 - ($this->getAccuracy() / 10)), |
|
87
|
18 |
|
'hardDivider' => (12 - floor(($this->getAccuracy() / 10))), |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
18 |
|
$calc['maxDifference'] = ($calc['chars'] / $calc['divider']); |
|
91
|
|
|
|
|
92
|
18 |
|
return $calc; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
27 |
|
public function calculatePercent($stringA, $stringB) |
|
96
|
|
|
{ |
|
97
|
27 |
|
$percent = 0; |
|
98
|
27 |
|
similar_text($stringA, $stringB, $percent); |
|
99
|
|
|
|
|
100
|
27 |
|
return $percent; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
27 |
|
public function calculatePercentExtended($stringA, $stringB) |
|
104
|
|
|
{ |
|
105
|
27 |
|
$a = []; |
|
106
|
|
|
|
|
107
|
|
|
foreach ([ |
|
108
|
27 |
|
[$stringA, strtolower($stringB)], |
|
109
|
27 |
|
[strtolower($stringA), strtolower($stringB)], |
|
110
|
27 |
|
[strtolower($stringA), $stringB], |
|
111
|
|
|
] as $item) { |
|
112
|
27 |
|
$a[] = $this->calculatePercent($item[0], $item[1]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
27 |
|
return max($a); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function __toArray() |
|
119
|
|
|
{ |
|
120
|
|
|
return array_merge(parent::__toArray(), [ |
|
121
|
|
|
'percentage' => $this->getPercent(), |
|
122
|
|
|
'isApproximate' => $this->isApproximate(), |
|
123
|
|
|
'proximityCalculation' => $this->getProximityCalculation(), |
|
124
|
|
|
]); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|