|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Onoi\Tesa; |
|
4
|
|
|
|
|
5
|
|
|
use Onoi\Tesa\Tokenizer\Tokenizer; |
|
6
|
|
|
use Onoi\Tesa\Synonymizer\Synonymizer; |
|
7
|
|
|
use Onoi\Tesa\StopwordAnalyzer\StopwordAnalyzer; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @license GNU GPL v2+ |
|
12
|
|
|
* @since 0.1 |
|
13
|
|
|
* |
|
14
|
|
|
* @author mwjames |
|
15
|
|
|
*/ |
|
16
|
|
|
class Sanitizer { |
|
17
|
|
|
|
|
18
|
|
|
const WHITELIST = 'WHITELIST'; |
|
19
|
|
|
const MIN_LENGTH = 'MIN_LENGTH'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Any change to the content of its data files should be reflected in a |
|
23
|
|
|
* version change (the version number does not necessarily correlate with |
|
24
|
|
|
* the library version) |
|
25
|
|
|
*/ |
|
26
|
|
|
const VERSION = '0.2'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $string = ''; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $whiteList = array(); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $minLength = 3; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @since 0.1 |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct( $string = '' ) { |
|
49
|
|
|
$this->setText( $string ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @since 1.0 |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $name |
|
56
|
|
|
* @param mixed $value |
|
57
|
17 |
|
*/ |
|
58
|
17 |
|
public function setOption( $name, $value ) { |
|
59
|
17 |
|
|
|
60
|
17 |
|
if ( $name === self::WHITELIST && is_array( $value ) && $value !== array() ) { |
|
61
|
|
|
$this->whiteList = array_fill_keys( $value, true ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ( $name === self::MIN_LENGTH ) { |
|
65
|
|
|
$this->minLength = (int)$value; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
1 |
|
|
|
69
|
|
|
/** |
|
70
|
1 |
|
* @since 0.1 |
|
71
|
1 |
|
* |
|
72
|
1 |
|
* @param string $string |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function setText( $string ) { |
|
75
|
1 |
|
$this->string = $string; |
|
76
|
1 |
|
} |
|
77
|
1 |
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @since 0.1 |
|
80
|
|
|
* |
|
81
|
|
|
* @param integer $flag |
|
82
|
|
|
*/ |
|
83
|
|
|
public function applyTransliteration( $flag = Transliterator::DIACRITICS ) { |
|
84
|
1 |
|
$this->string = Normalizer::applyTransliteration( $this->string, $flag ); |
|
85
|
1 |
|
} |
|
86
|
1 |
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @see Localizer::convertDoubleWidth |
|
89
|
|
|
* |
|
90
|
|
|
* @since 0.1 |
|
91
|
|
|
* |
|
92
|
|
|
* @param integer $flag |
|
|
|
|
|
|
93
|
|
|
*/ |
|
94
|
|
|
public function convertDoubleWidth() { |
|
95
|
11 |
|
$this->string = Normalizer::convertDoubleWidth( $this->string ); |
|
96
|
11 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @since 0.1 |
|
100
|
|
|
* |
|
101
|
|
|
* @param Tokenizer $tokenizer |
|
102
|
|
|
* @param StopwordAnalyzer $stopwordAnalyzer |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
8 |
|
public function sanitizeWith( Tokenizer $tokenizer, StopwordAnalyzer $stopwordAnalyzer, Synonymizer $synonymizer ) { |
|
107
|
|
|
|
|
108
|
8 |
|
// Treat non-words tokenizers (Ja,Zh*) differently |
|
109
|
|
|
$minLength = $tokenizer->isWordTokenizer() ? $this->minLength : 1; |
|
110
|
8 |
|
|
|
111
|
1 |
|
$words = $tokenizer->tokenize( $this->string ); |
|
112
|
|
|
|
|
113
|
|
|
if ( !$words || !is_array( $words ) ) { |
|
114
|
7 |
|
return $this->string; |
|
115
|
7 |
|
} |
|
116
|
|
|
|
|
117
|
7 |
|
$index = array(); |
|
118
|
|
|
$pos = 0; |
|
119
|
|
|
|
|
120
|
|
|
foreach ( $words as $key => $word ) { |
|
121
|
7 |
|
|
|
122
|
6 |
|
$word = $synonymizer->synonymize( $word ); |
|
123
|
|
|
|
|
124
|
|
|
// If it is not an exemption and less than the required minimum length |
|
125
|
|
|
// or identified as stop word it is removed |
|
126
|
7 |
|
if ( !isset( $this->whiteList[$word] ) && ( mb_strlen( $word ) < $minLength || $stopwordAnalyzer->isStopWord( $word ) ) ) { |
|
127
|
2 |
|
continue; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
7 |
|
// Simple proximity, check for same words appearing next to each other |
|
131
|
7 |
|
if ( isset( $index[$pos-1] ) && $index[$pos-1] === $word ) { |
|
132
|
7 |
|
continue; |
|
133
|
|
|
} |
|
134
|
7 |
|
|
|
135
|
|
|
$index[] = trim( $word ); |
|
136
|
|
|
$pos++; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return implode( ' ' , $index ); |
|
140
|
5 |
|
} |
|
141
|
5 |
|
|
|
142
|
5 |
|
/** |
|
143
|
|
|
* @since 0.1 |
|
144
|
|
|
*/ |
|
145
|
|
|
public function toLowercase() { |
|
146
|
|
|
$this->string = Normalizer::toLowercase( $this->string ); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
3 |
|
/** |
|
150
|
|
|
* @since 0.1 |
|
151
|
3 |
|
* |
|
152
|
1 |
|
* @param integer $length |
|
153
|
|
|
*/ |
|
154
|
|
|
public function reduceLengthTo( $length ) { |
|
155
|
3 |
|
$this->string = Normalizer::reduceLengthTo( $this->string, $length ); |
|
156
|
1 |
|
} |
|
157
|
1 |
|
|
|
158
|
|
|
/** |
|
159
|
3 |
|
* @see http://www.phpwact.org/php/i18n/utf-8#str_replace |
|
160
|
3 |
|
* @since 0.1 |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $search |
|
163
|
|
|
* @param string $replace |
|
164
|
|
|
*/ |
|
165
|
|
|
public function replace( $search, $replace ) { |
|
166
|
|
|
$this->string = str_replace( $search, $replace, $this->string ); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
1 |
|
/** |
|
170
|
1 |
|
* @since 0.1 |
|
171
|
1 |
|
* |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
public function __toString() { |
|
175
|
|
|
return $this->string; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..