forked from ryancramerdesign/WireWordTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWireWordTools.module
272 lines (246 loc) · 6.87 KB
/
WireWordTools.module
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
<?php namespace ProcessWire;
/**
* ProcessWire Word Tools module (EN)
*
* API methods for English word inflection and lemmatisation, plus ability to use them in search queries.
*
* © 2020 by Ryan Cramer
*
* @property int $useHook
* @method bool isSingular($word)
*
*/
class WireWordTools extends WireData implements Module, ConfigurableModule {
public static function getModuleInfo() {
return array(
'title' => 'Wire Word Tools (EN)',
'version' => 1,
'summary' => 'API methods for English word inflection, lemmatisation and stemming, plus ability to use them in search queries.',
'requires' => 'ProcessWire>=3.0.148',
'author' => 'Ryan Cramer',
'autoload' => true,
);
}
/**
* @var WireEnglishInflector|null
*
*/
protected $inflector = null;
/**
* @var WireEnglishLemmatizer|null
*
*/
protected $lemmatizer = null;
/**
* Construct
*
*/
public function __construct() {
$this->set('useHook', 1);
parent::__construct();
}
/**
* API ready
*
* Hook to ProcessWire’s $sanitizer->getTextTools()->getWordAlternates($word) method
* which is used by "query expansion" searches in ProcessWire 3.0.162.
*
*/
public function ready() {
$useHook = (int) $this->useHook;
if(!$useHook) return;
$languages = $this->wire()->languages;
if($languages && $this->wire()->user->language->id !== $useHook) return;
$this->wire()->addHookAfter('WireTextTools::wordAlternates', $this, 'hookWordAlternates');
}
/**
* Is word plural?
*
* @param string $word
* @return bool
*
*/
public function isPlural($word) {
return $this->inflector()->isPlural($word);
}
/**
* Is word singular?
*
* @param string $word
* @return bool
*
*/
public function ___isSingular($word) {
// hookable to avoid collision with Module::isSingular interface method
return $this->inflector()->isSingular($word);
}
/**
* Get plural word from singular word
*
* @param string $word
* @return bool
*
*/
public function getPlural($word) {
return $this->inflector()->getPlural($word);
}
/**
* Get singular word from plural word
*
* @param string $word
* @return bool
*
*/
public function getSingular($word) {
return $this->inflector()->getSingular($word);
}
/**
* Get lemma for given word
*
* @param string $word
* @return bool
*
*/
public function getLemma($word) {
return $this->lemmatizer()->getLemma($word);
}
/**
* Get related/alternate words for given word
*
* @param string $word
* @param bool $inclusive Also include the given word in return value if found to be valid? (default=false)
* @return array
*
*/
public function getWords($word, $inclusive = false) {
return $this->lemmatizer()->getWords($word, $inclusive);
}
/**
* @return WireEnglishInflector
*
*/
public function inflector() {
if($this->inflector === null) {
require_once(__DIR__ . '/WireEnglishInflector.php');
$this->inflector = $this->wire(new WireEnglishInflector());
}
return $this->inflector;
}
/**
* @return WireEnglishLemmatizer
*
*/
public function lemmatizer() {
if($this->lemmatizer === null) {
require_once(__DIR__ . '/WireEnglishLemmatizer.php');
$this->lemmatizer = $this->wire(new WireEnglishLemmatizer());
}
return $this->lemmatizer;
}
public function get($key) {
if($key === 'lemmatizer') return $this->lemmatizer();
if($key === 'inflector') return $this->inflector();
return parent::get($key);
}
/**
* Hook to WireTextTools::wordAlternates (PW 3.0.162+)
*
* @param HookEvent $event
*
*/
public function hookWordAlternates(HookEvent $event) {
/** @var string $word */
$word = $event->arguments(0);
/** @var array $words */
$words = $event->return;
if(empty($word) || !ctype_alnum($word)) return;
if(!is_array($words)) $words = array();
$lemma = $this->getLemma($word);
if($word !== $lemma) $words[] = $lemma;
$words = array_merge($words, $this->lemmatizer()->getWordsFromLemma($lemma));
if($word === $lemma) {
$plural = $this->isPlural($word) ? $word : $this->getPlural($word);
$singular = $plural === $word ? $this->getSingular($word) : $word;
} else {
$plural = $this->getPlural($lemma);
$singular = $this->getSingular($plural);
}
if(!in_array($plural, $words)) $words[] = $plural;
if(!in_array($singular, $words)) $words[] = $singular;
$event->return = $words;
}
/**
* Module config
*
* @param InputfieldWrapper $inputfields
*
*/
public function getModuleConfigInputfields(InputfieldWrapper $inputfields) {
$languages = $this->wire()->languages;
if($languages) {
/** @var InputfieldRadios $f */
$f = $this->wire()->modules->get('InputfieldRadios');
$f->description = 'To enable, select the language that represents EN (English)';
if("$this->useHook" === "1") $this->useHook = 0;
foreach($languages as $language) {
$f->addOption($language->id, $language->get('title|name'));
}
$f->addOption(0, 'Disabled');
$f->optionColumns = 1;
$f->val((int) $this->useHook);
} else {
/** @var InputfieldToggle $f */
$f = $this->wire()->modules->get('InputfieldToggle');
$f->val($this->useHook ? 1 : 0);
}
$f->attr('name', 'useHook');
$f->label = 'Automatically provide EN word alternates to relevant search queries?';
$f->notes = 'This feature requires ProcessWire 3.0.162+';
$inputfields->add($f);
$actions = array(
'words' => 'getWords',
'lemma' => 'getLemma',
'singular' => 'getSingular',
'plural' => 'getPlural',
);
$action = $this->wire()->input->post('_action');
$words = $this->wire()->input->post->text('_words');
$results = array();
if($action && $words && isset($actions[$action])) {
$words = preg_replace('/[^a-zA-Z0-9]/', ' ', $words);
$words = explode(' ', $words);
foreach($words as $key => $word) {
if(empty($word)) continue;
$words[$key] = $word;
$method = $actions[$action];
$result = $this->$method($word);
if(is_array($result)) $result = implode(', ', $result);
$result = "$method('$word') = $result";
$this->warning($result, Notice::noGroup);
$results[] = $result;
}
$this->wire()->session->setFor($this, 'results', $results);
} else {
$results = $this->wire()->session->getFor($this, 'results');
}
$fs = $this->wire()->modules->get('InputfieldFieldset');
$fs->attr('name', '_test_words');
$fs->label = 'Test word functions';
if(count($results)) $fs->description = implode("\n", $results);
$inputfields->add($fs);
/** @var InputfieldSelect $f */
$f = $this->wire()->modules->get('InputfieldSelect');
$f->attr('id+name', '_action');
$f->label = 'Select function to test';
$f->addOptions($actions);
$f->columnWidth = 50;
$fs->add($f);
/** @var InputfieldText $f */
$f = $this->wire()->modules->get('InputfieldText');
$f->attr('name', '_words');
$f->label = 'Enter one or more words to test';
$f->columnWidth = 50;
$f->showIf = '_action!=""';
$fs->add($f);
}
}