|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* @Name : similar-text |
|
5
|
|
|
* @Programmer : Akpé Aurelle Emmanuel Moïse Zinsou |
|
6
|
|
|
* @Date : 2019-04-01 |
|
7
|
|
|
* @Released under : https://github.com/manuwhat/similar-text/blob/master/LICENSE |
|
8
|
|
|
* @Repository : https://github.com/manuwhat/similar |
|
9
|
|
|
* |
|
10
|
|
|
**/ |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
namespace EZAMA{ |
|
14
|
|
|
|
|
15
|
|
|
class complexCommonTextSimilaritiesHelper extends simpleCommonTextSimilarities |
|
16
|
|
|
{ |
|
17
|
|
|
const URL_FORMAT_EXTENDED_PATTERN = '/^((https?|ftps?|file):\/\/){0,1}'.// protocol |
|
18
|
|
|
'(([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+'.// username |
|
19
|
|
|
'(:([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+)?'.// password |
|
20
|
|
|
'@)?(?#'.// auth requires @ |
|
21
|
|
|
')((([a-z0-9]\.|[a-z0-9][a-z0-9-]*[a-z0-9]\.)*'.// domain segments AND |
|
22
|
|
|
'[a-z][a-z0-9-]*[a-z0-9]'.// top level domain OR |
|
23
|
|
|
'|((\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])\.){3}'. |
|
24
|
|
|
'(\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])'.// IP address |
|
25
|
|
|
')(:\d+)?'.// port |
|
26
|
|
|
')(((\/+([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)*'.// path |
|
27
|
|
|
'(\?([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)'.// query string |
|
28
|
|
|
'?)?)?'.// path and query string optional |
|
29
|
|
|
'(#([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?'.// fragment |
|
30
|
|
|
'$/i'; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
const URL_POSIX_FORMAT = '"^(\b(https?|ftps?|file):\/\/)?[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#\/%=~_|]$"i'; |
|
36
|
|
|
|
|
37
|
|
|
protected static function isUrl($url, &$getDomain = '') |
|
38
|
|
|
{ |
|
39
|
|
|
$matches = array(); |
|
40
|
|
|
$bool = is_string($url) && preg_match('/\./', $url) && preg_match(self::URL_POSIX_FORMAT, $url) && preg_match(self::URL_FORMAT_EXTENDED_PATTERN, $url, $matches); |
|
41
|
|
|
$getDomain = $bool ? self::getDomain($url, $matches[1]) : ''; |
|
42
|
|
|
return $bool; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected static function getDomain($url, $match) |
|
46
|
|
|
{ |
|
47
|
|
|
return ($getDomain = explode('.', parse_url($url, $match ? PHP_URL_HOST : PHP_URL_PATH))) ? (($c = count($getDomain)) > 1 ? ($getDomain[$c - 2]) : '') : ''; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
protected static function filter(&$a, &$b, $filter, $insensitive = true, $captureLength = false) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($insensitive) { |
|
54
|
|
|
$a = array_filter(self::getParts(self::strtolower($a), $c, $captureLength), $filter); |
|
55
|
|
|
if ($c === 1) { |
|
56
|
|
|
$a = self::strtolower($a); |
|
57
|
|
|
} |
|
58
|
|
|
$b = array_filter(self::getParts(self::strtolower($b), $c, $captureLength), $filter); |
|
59
|
|
|
if ($c === 1) { |
|
60
|
|
|
$b = self::strtolower($b); |
|
61
|
|
|
} |
|
62
|
|
|
} else { |
|
63
|
|
|
$a = array_filter(self::getParts(self::split($a), $c, $captureLength), $filter); |
|
64
|
|
|
if ($c === 1) { |
|
65
|
|
|
$a = self::strtolower($a); |
|
66
|
|
|
} |
|
67
|
|
|
$b = array_filter(self::getParts(self::split($b), $c, $captureLength), $filter); |
|
68
|
|
|
if ($c === 1) { |
|
69
|
|
|
$b = self::strtolower($b); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
protected static function waorDiff($a, $b, $ca, $cb) |
|
76
|
|
|
{ |
|
77
|
|
|
return (bool) (($ca > $cb) ?array_diff_assoc(array_values($a), array_values($b)) : array_diff_assoc(array_values($b), array_values($a))); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
protected static function aoeStemming($a, $b) |
|
83
|
|
|
{ |
|
84
|
|
|
foreach ($a as $index=>$word) { |
|
85
|
|
|
if (!self::haveSameRoot($word[0], $b[$index][0]) || ($a[$index][1] > 2 && $b[$index][1] > 2)) { |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|