-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequenceMatcher.php
executable file
·472 lines (393 loc) · 17.7 KB
/
SequenceMatcher.php
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php
namespace Ling\SequenceMatcher;
use Ling\SequenceMatcher\Element\AlternateGroupInterface;
use Ling\SequenceMatcher\Element\ElementInterface;
use Ling\SequenceMatcher\Element\EntityInterface;
use Ling\SequenceMatcher\Element\GroupInterface;
use Ling\SequenceMatcher\Exception\EndOfModelException;
use Ling\SequenceMatcher\Exception\EndOfSequenceException;
use Ling\SequenceMatcher\Exception\ModelDoesNotMatchException;
class SequenceMatcher
{
private $matchedElements;
private $sequence;
private $curSequenceIndex;
private $maxSequenceIndex;
private $curTmpSeqIndex;
private $debugLine;
private $debugLineCr;
private $_debugMode;
private $debugThingToStringFunc;
private $level;
public function __construct()
{
$this->_symbol = null;
$this->debugLineCr = ('cli' === php_sapi_name()) ? PHP_EOL : '<br>';
$this->debugLine = '';
$this->_debugMode = false;
}
public static function create()
{
return new self();
}
public function debugMode($func = null)
{
if (null === $func) {
$func = function ($s) {
return (string)$s;
};
}
$this->debugThingToStringFunc = $func;
$this->_debugMode = true;
return $this;
}
public function match(array $sequence, Model $model, $func)
{
$this->matchedElements = [];
$this->sequence = $sequence;
$this->curSequenceIndex = 0;
$this->maxSequenceIndex = count($sequence) - 1;
if ($this->maxSequenceIndex < 0) {
return false;
}
$this->curTmpSeqIndex = 0;
$this->level = 0;
$allMatchedThings = [];
for ($seqIndex = $this->curSequenceIndex; $seqIndex <= $this->maxSequenceIndex; $seqIndex++) {
$matchedElements = [];
$matchedThings = [];
$this->debugStore('s[' . $seqIndex . ']=' . $this->debugThingToString($this->sequence[$seqIndex]) . ':');
$this->debugDump();
$this->level++;
if (true === $this->matchGroupAtIndex($model, $seqIndex, $matchedElements, $matchedThings)) {
$this->matchedElements[] = $matchedElements;
$allMatchedThings[] = $matchedThings;
}
$this->level--;
}
foreach ($this->matchedElements as $k => $match) {
$things = $allMatchedThings[$k];
//------------------------------------------------------------------------------/
// COMPUTE MAKERS
//------------------------------------------------------------------------------/
$allModelInfo = [];
foreach ($match as $element) {
$modelInfo = null;
$this->findModelInfo($model, $element, $modelInfo);
$allModelInfo[] = $modelInfo;
}
$markers = [];
foreach ($allModelInfo as $k => $info) {
if (null !== ($marker = $info[2])) {
$markers[$marker][] = $things[$k];
}
}
call_user_func($func, $match, $things, $markers);
}
}
private function findModelInfo(GroupInterface $group, ElementInterface $element, &$info)
{
$el = null;
foreach ($group->getElements() as $elInfo) {
$_element = $elInfo[0];
if ($_element === $element) {
$info = $elInfo;
} elseif ($_element instanceof GroupInterface) {
$this->findModelInfo($_element, $element, $info);
} elseif ($_element instanceof AlternateGroupInterface) {
foreach ($_element->getAlternatives() as $_group) {
$this->findModelInfo($_group, $element, $info);
}
}
}
return $el;
}
//--------------------------------------------
//
//--------------------------------------------
/**
* If a match occurs:
* - then the matchedElements array should be updated
* - also the index (of the sequence) is updated accordingly
*
*/
private function matchGroupAtIndex(GroupInterface $group, &$index, array &$matchedElements, array &$matchedThings)
{
$modelMatches = false;
$this->curTmpSeqIndex = $index;
try {
/**
* Move through every element of the model, the result should be binary:
* - either the current iteration fails
* - or the current iteration succeeds
*
*/
$_els = $group->getElements();
$nbElements = count($_els);
$numElement = 0;
foreach ($_els as $elInfo) {
$numElement++;
$theThing = $this->sequence[$this->curTmpSeqIndex];
$this->debugStore(' s[' . $this->curTmpSeqIndex . ']=' . $this->debugThingToString($theThing) . ' - ');
/**
* @var Element $element
*/
list($element, $modificator, $marker) = $elInfo;
if ($element instanceof EntityInterface) {
$this->debugStore('entity[' . $element->__toString() . '] ');
if (true === $element->match($theThing)) {
$this->debugStore('match with ' . $this->debugThingToString($theThing));
$matchedElements[] = $element;
$matchedThings[] = $theThing;
$this->nextSequenceElement($modelMatches, $numElement, $nbElements, $_els);
if ('+' === $modificator || '*' === $modificator) {
while (true) {
$theThing = $this->sequence[$this->curTmpSeqIndex];
if (true === $element->match($theThing)) {
$this->debugDump();
$this->debugStore('match ' . $modificator . ' with ' . $this->debugThingToString($theThing));
$matchedElements[] = $element;
$matchedThings[] = $theThing;
$this->nextSequenceElement($modelMatches, $numElement, $nbElements, $_els);
} else {
break;
}
}
}
} else {
if ('?' === $modificator || '*' === $modificator) {
$this->debugStore($modificator . ' ' . $this->debugThingToString($theThing));
$modelMatches = true;
} else {
$matchedElements = [];
$matchedThings = [];
$this->debugStore('fails against ' . $this->debugThingToString($theThing));
$this->modelFails();
}
}
} elseif ($element instanceof GroupInterface) {
$curTmpSeqIndex = $this->curTmpSeqIndex;
$startIndex = $this->curTmpSeqIndex;
$this->debugStore('group - ');
$this->debugDump();
$_matchedElements = [];
$_matchedThings = [];
$this->level++;
$res = $this->matchGroupAtIndex($element, $curTmpSeqIndex, $_matchedElements, $_matchedThings);
$this->level--;
if (true === $res) {
$modelMatches = true;
$this->debugStore('group matched ');
foreach ($_matchedElements as $el) {
$matchedElements[] = $el;
}
foreach ($_matchedThings as $th) {
$matchedThings[] = $th;
}
if ('+' === $modificator || '*' === $modificator) {
while (true) {
$curTmpSeqIndex = $this->curTmpSeqIndex;
$startIndex = $this->curTmpSeqIndex;
if (array_key_exists($curTmpSeqIndex, $this->sequence)) {
$modelMatches = true;
$this->debugStore('group ' . $modificator . ' - ');
$this->debugDump();
$_matchedElements = [];
$_matchedThings = [];
$this->level++;
$res = $this->matchGroupAtIndex($element, $curTmpSeqIndex, $_matchedElements, $_matchedThings);
$this->level--;
if (true === $res) {
$this->debugStore('group ' . $modificator . ' matched ');
foreach ($_matchedElements as $el) {
$matchedElements[] = $el;
}
foreach ($_matchedThings as $th) {
$matchedThings[] = $th;
}
} else {
break;
}
} else {
break;
}
}
}
} else {
if ('?' === $modificator || '*' === $modificator) {
$modelMatches = true;
$this->debugStore('group failed but ' . $modificator . ' ');
$this->curTmpSeqIndex = $startIndex;
} else {
$matchedElements = [];
$matchedThings = [];
$this->debugStore('group failed');
$this->modelFails();
}
}
} elseif ($element instanceof AlternateGroupInterface) {
$alts = $element->getAlternatives();
$nbAlts = count($alts);
$a = 0;
foreach ($alts as $alt) {
$a++;
$group = $alt[0];
$groupModificator = $alt[1];
$curTmpSeqIndex = $this->curTmpSeqIndex;
$startIndex = $this->curTmpSeqIndex;
$this->debugStore('alt - ');
$this->debugDump();
$_matchedElements = [];
$_matchedThings = [];
$this->level++;
$res = $this->matchGroupAtIndex($group, $curTmpSeqIndex, $_matchedElements, $_matchedThings);
$this->level--;
if (true === $res) {
$modelMatches = true;
$this->debugStore('alt matched ');
foreach ($_matchedElements as $el) {
$matchedElements[] = $el;
}
foreach ($_matchedThings as $th) {
$matchedThings[] = $th;
}
if ('+' === $modificator || '*' === $modificator) {
$curTmpSeqIndex++;
$z = 0;
while (true) {
$z++;
if ($z > 10) {
az("k");
}
$b = 0;
foreach ($alts as $alt2) {
$b++;
$group2 = $alt2[0];
$groupModificator = $alt2[1];
// $curTmpSeqIndex = $this->curTmpSeqIndex;
// $startIndex = $this->curTmpSeqIndex;
$this->debugStore('alt - ' . $modificator);
$this->debugDump();
$_matchedElements = [];
$_matchedThings = [];
$this->level++;
$res = $this->matchGroupAtIndex($group2, $curTmpSeqIndex, $_matchedElements, $_matchedThings);
$this->level--;
if (true === $res) {
$modelMatches = true;
$this->debugStore('alt matched ');
foreach ($_matchedElements as $el) {
$matchedElements[] = $el;
}
foreach ($_matchedThings as $th) {
$matchedThings[] = $th;
}
$curTmpSeqIndex++;
if (!array_key_exists($curTmpSeqIndex, $this->sequence)) {
break 2;
}
} else {
if ($nbAlts === $b) {
break 2;
}
}
}
}
}
break;
} else {
$this->curTmpSeqIndex = $startIndex;
// if all alternatives fail, it fails...
if ($a === $nbAlts) {
// ... unless there was an optional modificator
if ('?' === $modificator || '*' === $modificator) {
$modelMatches = true;
$this->debugStore('alt failed but ' . $modificator . ' ');
} else {
$matchedElements = [];
$matchedThings = [];
$this->debugStore('alt failed');
$this->modelFails();
}
}
}
}
} else {
throw new \Exception("not implemented yet");
}
$this->debugDump();
}
$this->debugStore(' EndOfModelOrGroup');
} catch (ModelDoesNotMatchException $e) {
$this->debugStore(' - [ModelDoesNotMatch]');
$modelMatches = false;
} catch (EndOfSequenceException $e) {
$this->debugStore(' - [EndOfSequence]');
// $modelMatches = false;
}
$this->debugStore((true === $modelMatches) ? ' -> success' : ' -> failure');
$this->debugDump();
if (true === $modelMatches) {
$index = $this->curTmpSeqIndex - 1;
$this->debugStore('update real seqIndex: ' . $index . ', merging tmp matched elements');
$this->debugDump();
return true;
}
return false;
}
private function nextSequenceElement(&$modelMatches, $numElement, $nbElements, array $elements)
{
$this->debugStore(' - update tmp seqIndex: ' . $this->curTmpSeqIndex);
$this->curTmpSeqIndex++;
$this->debugStore(' -> ' . $this->curTmpSeqIndex);
if ($numElement === $nbElements) {
$modelMatches = true;
} elseif ($numElement < $nbElements) {
/**
* If the sequence ends before the model,
* by default the match will fail, but sometimes,
* all the remaining elements in the model are optional,
* so it should match.
*/
$allOptional = true;
for ($i = $numElement; $i < $nbElements; $i++) {
$el = $elements[$i];
$modif = $el[1];
if ('?' !== $modif && '*' !== $modif) {
$allOptional = false;
}
}
if (true === $allOptional) {
$modelMatches = true;
}
}
if ($this->curTmpSeqIndex > $this->maxSequenceIndex) {
throw new EndOfSequenceException();
}
}
private function modelFails()
{
throw new ModelDoesNotMatchException();
}
// private function debugStart()
// {
// $this->debugLine = '';
// }
private function debugStore($m)
{
$this->debugLine .= $m;
}
private function debugDump()
{
if (true === $this->_debugMode) {
echo str_repeat('_', $this->level * 2) . ' ' . $this->debugLine . $this->debugLineCr;
}
$this->debugLine = '';
}
private function debugThingToString($thing)
{
if (null !== $this->debugThingToStringFunc) {
return call_user_func($this->debugThingToStringFunc, $thing);
}
}
}