Tokens.php
7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* SearchWP Token Collection.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP;
use SearchWP\Utils;
use SearchWP\Stemmer;
use SearchWP\Settings;
/**
* Class Tokens is a collection of tokenized strings.
*
* @since 4.0
*/
class Tokens {
/**
* Collection items.
*
* @since 4.0
* @var array
*/
private $items = [];
/**
* Tokens that have been extracted by regex patterns.
*
* @since 4.0
* @var array
*/
private $extracted = [];
/**
* Whether regex patterns are tokenized.
*
* @since 4.0
* @var bool
*/
public $tokenize_pattern_matches = false;
/**
* The (stringified) raw value.
*
* @since 4.0
* @var string
*/
public $raw = '';
/**
* Constructor.
*
* @since 4.0
*/
function __construct( $data = false ) {
$this->tokenize_pattern_matches = (bool) apply_filters(
'searchwp\tokens\tokenize_pattern_matches',
Settings::get_single( 'tokenize_pattern_matches', 'boolean' )
);
// If we're indexing we do want to index tokenized matches, the setting is primarily geared toward searching.
if ( did_action( 'searchwp\indexer\batch' ) ) {
$this->tokenize_pattern_matches = (bool) apply_filters(
'searchwp\tokens\tokenize_pattern_matches\indexing',
true
);
}
if ( ! empty( $data ) ) {
$this->tokenize( $data );
}
}
/**
* Tokenizes the submitted data by extracting string values from any data type.
*
* @since 4.0
* @param mixed $data The data to tokenize, can be anything.
*/
public function tokenize( $data ) {
// Tokens are built from strings. We need to convert the data into a string.
$string = Utils::get_string_from( $data );
// Now that we have a string, we need to make sure it's formatted how we expect.
$string = apply_filters( 'searchwp\tokens\string', Utils::normalize_string( $string ) );
// Handle HTML.
$string = Utils::stringify_html( $string );
// Store the original, raw value.
$this->raw = $string;
// Extract pattern matches if the string is not too long to be a performance hit.
$pattern_match_threshold = apply_filters( 'searchwp\tokens\pattern_match_threshold', 1000000, [ 'string' => $string ] );
$this->extracted = [];
if ( strlen( $string ) <= absint( $pattern_match_threshold ) ) {
$this->extracted = $this->get_pattern_match_tokens_from_string( $string );
}
// Remove pattern matches if we are not tokenizing them.
if ( ! empty( $this->extracted ) && ! $this->tokenize_pattern_matches ) {
$string = Utils::remove_strings_from_string( $this->extracted, $string );
}
// Clean the string.
$string = Utils::clean_string( $string );
// We always work with arrays of tokens.
$string_array = explode( ' ', trim( $string ) );
// Remove accents from tokens if applicable.
if ( ! apply_filters( 'searchwp\tokens\strict', false ) ) {
$string_array = array_merge( $string_array, array_filter( array_map( function( $string ) {
$accents_removed = apply_filters(
'searchwp\tokens\removed_accents',
remove_accents( $string )
);
return $accents_removed === $string ? false : $accents_removed;
}, $string_array ) ) );
}
// Explode the string and add each as a Token.
if ( ! empty( $string_array ) ) {
$string_array = $this->apply_rules( $string_array );
array_walk(
$string_array,
function( $string ) {
$this->items[] = $string;
}
);
}
// Append pattern matches.
array_walk(
$this->extracted,
function( $pattern_match_token ) {
$this->items[] = $pattern_match_token;
}
);
// Return the tokens.
return $this->items;
}
/**
* Apply token regex patterns to a string and return matches.
*
* @since 4.0
* @param string $string The string to check.
* @return array
*/
public function get_pattern_match_tokens_from_string( string $string ) {
if ( empty( $string ) ) {
return [];
}
// Iterate over token pattern matches and find matches for each.
$matches = [];
foreach ( Utils::get_token_regex_patterns() as $regex_pattern ) {
preg_match_all( $regex_pattern, $string, $match_set );
if ( ! is_array( $match_set[0] ) || empty( $match_set[0] ) ) {
continue;
}
// We only want to work with the full match set. If this hook returns false,
// all group matches will be considered as well.
if( (bool) apply_filters( 'searchwp\tokens\regex_patterns\only_full_matches', true ) ) {
$match_set = [ $match_set[0] ];
}
// Iterate through this set of matches and process each match.
foreach ( $match_set as $match_set_matches ) {
if ( empty( $match_set_matches ) || ! is_array( $match_set ) ) {
continue;
}
$matches = array_merge(
$matches,
array_map(
function( $match ) {
$match = trim( $match );
$match = function_exists( 'mb_strtolower' )
? mb_strtolower( $match, 'UTF-8' )
: strtolower( $match );
return $match;
},
$match_set_matches
)
);
}
// Remove the matches from the string?
if ( ! $this->tokenize_pattern_matches ) {
$string = Utils::remove_strings_from_string( $matches, $string );
}
}
// Finalize and validate our matches.
$matches = array_map( 'sanitize_text_field', $matches );
$matches = array_map( 'trim', $matches );
$matches = array_filter( $matches );
// When indexing, generate pattern match variations.
if ( apply_filters(
'searchwp\tokens\generate_parttern_match_variations',
Utils::is_indexer()
) ) {
$matches = $this->generate_parttern_match_variations( $matches );
}
if ( apply_filters( 'searchwp\tokens\apply_rules_to_pattern_matches', true ) ) {
$matches = $this->apply_rules( $matches );
}
return $matches;
}
/**
* Retrieves minimum token length in number of characters.
*
* @since 4.0
* @return int Minimum length.
*/
public function get_minimum_length() {
$min_length = Settings::get( 'remove_min_word_length', 'boolean' ) ? 1 : 3;
return absint( apply_filters( 'searchwp\tokens\minimum_length', $min_length ) );
}
/**
* Generates common variations of pattern matches.
*
* @since 4.1
* @param string[] $strings The strings.
* @return string[]
*/
public function generate_parttern_match_variations( array $strings ) {
$original = $strings;
// Instead of replacing punctuation with spaces, generate a version with no spaces as well.
if ( apply_filters( 'searchwp\tokens\generate_parttern_match_variations\compact', true ) ) {
$strings = array_merge( $strings, array_filter( array_map( function( $string ) {
$original_match = $string;
$variation = Utils::clean_string( $string, '' );
return $original_match !== $variation ? $variation : false;
}, $strings ) ) );
}
return apply_filters( 'searchwp\tokens\parttern_match_variations', $strings, [ 'original' => $original, ] );
}
/**
* Applies rules (e.g. minimum character length, stopwords ) to submitted strings.
*
* @since 4.0
* @param string[] $strings The strings.
* @return string[]
*/
public function apply_rules( array $strings ) {
$min_length = $this->get_minimum_length();
$max_length = 80; // Database schema.
$strings = array_filter( $strings, function( $string ) use( $min_length, $max_length ) {
return strlen( $string ) >= $min_length && strlen( $string ) <= $max_length;
} );
// Stopwords hook in here.
return apply_filters( 'searchwp\tokens', $strings );
}
/**
* Getter for collection items.
*
* @since 4.0
* @return array
*/
public function get() {
return $this->items;
}
}