|
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\Input; |
|
16
|
|
|
|
|
17
|
|
|
class Decorator |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Remove non alphanumeric characters and replacing multiple spaces with a single space. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $string |
|
23
|
|
|
* |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
42 |
|
public function stripIgnoredCharacters($string) |
|
27
|
|
|
{ |
|
28
|
42 |
|
$aplhaNumeric = preg_replace("/[^\w\d ]/ui", '', $string); |
|
29
|
|
|
|
|
30
|
42 |
|
return $this->stripMultipleSpaces($aplhaNumeric); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
6 |
|
public function stripStopwords($string, $list) |
|
34
|
|
|
{ |
|
35
|
|
|
usort($list, function ($a, $b) { |
|
36
|
6 |
|
return strlen($b) - strlen($a); |
|
37
|
6 |
|
}); |
|
38
|
|
|
|
|
39
|
6 |
|
$addSpaces = function (&$v) { |
|
40
|
6 |
|
$v = ' '.trim($v).' '; |
|
41
|
|
|
|
|
42
|
6 |
|
return $v; |
|
43
|
6 |
|
}; |
|
44
|
|
|
|
|
45
|
6 |
|
array_walk($list, $addSpaces); |
|
46
|
|
|
|
|
47
|
6 |
|
return trim(str_ireplace($list, ' ', $addSpaces($string))); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
42 |
|
public function stripMultipleSpaces($string) |
|
51
|
|
|
{ |
|
52
|
42 |
|
return $this->stripSpaces($string, ' '); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
92 |
|
public function stripSpaces($string, $replacement = '') |
|
56
|
|
|
{ |
|
57
|
92 |
|
return preg_replace('!\s+!', $replacement, $string); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
50 |
|
public function onlyNumbers($string) |
|
61
|
|
|
{ |
|
62
|
50 |
|
$numbers = preg_replace('/[^0-9]/', '', $string); |
|
63
|
|
|
|
|
64
|
50 |
|
return $this->stripSpaces($numbers); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|