-
Notifications
You must be signed in to change notification settings - Fork 4
/
Disambiguate.php
687 lines (646 loc) · 31.4 KB
/
Disambiguate.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
<?php
abstract class Disambiguator {
// base class for disambiguation
public $scores = [];
public $weights = [];
public $alternateWeights = [];
public $distance = [];
public $minDistance = [];
public $termArray = null;
// $termArray is a TermArray object from Terms.php
function __construct($termArray) {
$this->termArray = $termArray;
if (count($termArray->getAllPhrases()) > 1) {
$this->UpdateWeights();
$this->UpdateDistanceMatrix();
$this->UpdateMinDistances();
}
}
// Calculates great circle distance on a sphere the size of the Earth
// Returned distance is in kilometres.
function calculateDistance($lat1, $lon1, $lat2, $lon2) {
$d = (sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon1 - $lon2)));
return (acos($d) * 6371.0);
}
// calculates the distance between Nominatim results $r1 and $r2
function calculateDistanceResults($r1, $r2) {
return $this->calculateDistance($r1["lat"], $r1["lon"], $r2["lat"], $r2["lon"]);
}
function UpdateWeights() {
$this->weights = $this->termArray->getWeights();
$this->alternateWeights = $this->termArray->getAlternateWeights();
}
function UpdateDistanceMatrix() {
// calculates the distance between each result
$this->distance = [];
$phrases = $this->termArray->getAllPhrases();
$results = $this->termArray->results;
foreach ($phrases as $rowPhrase) { // each row of the matrix
foreach ($phrases as $colPhrase) { // each column of the matrix
if (strcmp($rowPhrase, $colPhrase) > 0) {
foreach ($results[$rowPhrase] as $rowResultIndex => $rowResult) {
foreach ($results[$colPhrase] as $colResultIndex => $colResult) {
// distances are symmetric, so only calculate once then copy:
$this->distance[$rowPhrase][$colPhrase][$rowResultIndex][$colResultIndex] = $this->calculateDistanceResults($rowResult, $colResult);
$this->distance[$colPhrase][$rowPhrase][$colResultIndex][$rowResultIndex] = $this->distance[$rowPhrase][$colPhrase][$rowResultIndex][$colResultIndex];
}
}
}
}
}
}
function UpdateMinDistances() {
// finds the two results with the shortest distance between them
// assumes that $this->distance is updated
$this->minDistances = [];
$phrases = $this->termArray->getAllPhrases();
foreach ($phrases as $rowPhrase) {
foreach ($phrases as $colPhrase) {
if (strcmp($rowPhrase, $colPhrase) > 0) {
$minVal = 100000.0;
foreach ($this->termArray->results[$rowPhrase] as $rowResultIndex => $rowResult) {
foreach ($this->termArray->results[$colPhrase] as $colResultIndex => $colResult) {
if ($minVal > $this->distance[$rowPhrase][$colPhrase][$rowResultIndex][$colResultIndex]) {
$minVal = $this->distance[$rowPhrase][$colPhrase][$rowResultIndex][$colResultIndex];
}
}
}
$this->minDistances[$rowPhrase][$colPhrase] = $minVal;
$this->minDistances[$colPhrase][$rowPhrase] = $minVal;
}
}
}
}
// override this in derived classes to try different scoring functions
function CalculateScore($rowTerm, $rowResultIndex, $rowResult) {
return 0.0;
}
// recalculate scores for every result
function UpdateScores() {
$this->scores = [];
foreach ($this->termArray->getAllTerms() as $rowTerm) {
foreach ($this->termArray->results[$rowTerm->phrase] as $rowResultIndex => $rowResult) {
$this->scores[$rowTerm->phrase][$rowTerm->position][$rowResultIndex] = $this->CalculateScore($rowTerm, $rowResultIndex, $rowResult);
}
}
}
// finds the maximum of a 3D array
function Max3D($a) {
$rval = null;
foreach ($a as $b) {
foreach ($b as $c) {
foreach ($c as $d) {
if ($rval) {
if ($rval < $d) {
$rval = $d;
}
} else {
// this is the first value we find
$rval = $d;
}
}
}
}
return $rval;
}
// Prints a table in HTML
function PrintScoreTable() {
echo "<table border=\"1\"><tr><th colspan=\"2\" rowSpan=\"2\"></th>";
$terms = $this->termArray->getAllTerms();
$results = $this->termArray->results;
foreach ($terms as $term) {
echo "<th colspan=\"" . count($results[$term->phrase]) . "\">" . $term->phrase . " @ " . $term->position . "</th>";
}
echo "<th rowspan=\"2\">Score</th>";
echo "</tr>";
echo "<tr>";
foreach ($terms as $term) {
foreach ($results[$term->phrase] as $result) {
echo "<td>" . $result["display_name"] . " @ " . $result["lat"] . "," . $result["lon"] . "</td>";
}
}
echo "</tr>";
//$this->UpdateMinDistances();
$maxScore = $this->Max3D($this->scores);
foreach ($terms as $rowTerm) {
echo "<tr><th rowspan=\"" . count($results[$rowTerm->phrase]) . "\">" . $rowTerm->phrase . " @ " . $rowTerm->position . "</th>";
foreach ($results[$rowTerm->phrase] as $rowResultIndex => $rowResult) {
echo "<td>" . $rowResult["display_name"] . " @ " . $rowResult["lat"] . "," . $rowResult["lon"] . "</td>";
foreach ($terms as $colTerm) {
foreach ($results[$colTerm->phrase] as $colResultIndex => $colResult) {
if (! isset($this->distance[$rowTerm->phrase][$colTerm->phrase][$rowResultIndex][$colResultIndex])) {
echo "<td style=\"text-align:center\">X</td>";
} else {
echo "<td style=\"text-align:center\">";
$isMax = ($this->distance[$rowTerm->phrase][$colTerm->phrase][$rowResultIndex][$colResultIndex] == $this->minDistances[$rowTerm->phrase][$colTerm->phrase]);
if ($isMax) {
echo "<b>";
}
echo round($this->distance[$rowTerm->phrase][$colTerm->phrase][$rowResultIndex][$colResultIndex],1);
if ($isMax) {
echo "</b>";
}
echo "</td>";
}
}
}
if ($this->scores[$rowTerm->phrase][$rowTerm->position][$rowResultIndex] == $maxScore) {
echo "<td style=\"text-align:center\"><b>" . round($this->scores[$rowTerm->phrase][$rowTerm->position][$rowResultIndex], 4) . "</b></td>";
} else {
echo "<td style=\"text-align:center\">" . round($this->scores[$rowTerm->phrase][$rowTerm->position][$rowResultIndex], 4) . "</td>";
}
echo "</tr><tr>";
}
echo "</tr>";
}
echo "<tr><th colspan=\"2\">Score</th>";
foreach ($terms as $colTerm) {
foreach ($results[$colTerm->phrase] as $colResultIndex => $colResult) {
if ($this->scores[$colTerm->phrase][$colTerm->position][$colResultIndex] == $maxScore) {
echo "<td style=\"text-align:center\"><b>" . round($this->scores[$colTerm->phrase][$colTerm->position][$colResultIndex], 4) . "</b></td>";
} else {
echo "<td style=\"text-align:center\">" . round($this->scores[$colTerm->phrase][$colTerm->position][$colResultIndex], 4) . "</td>";
}
}
}
echo "<td></td></tr>";
echo "</tr>";
echo "</table>";
}
// This is for the step where we decide between "college" and "georgian college", for example
function ChoosePhrase($phrase, $position) {
if (isset($this->alternateWeights[$phrase][$position])) {
foreach ($this->alternateWeights[$phrase][$position] as $otherPhrase => $otherPositionsAndWeights) {
foreach ($otherPositionsAndWeights as $otherPosition => $otherWeight) {
if ($otherWeight == 0.0) {
// remove this term because it conflicts with the chosen phrase
$this->termArray->removeTerm($otherPhrase, $otherPosition);
}
}
}
}
}
function ChooseResult($phrase, $resultIndex) {
$this->termArray->setResults($phrase, [$resultIndex => $this->termArray->results[$phrase][$resultIndex]]);
}
// Searches $scores for the highest score.
// Returns an array with the phrase, position in the text, and index
// of the result with the highest score.
function FindBestPhrase($scores) {
$bestScore = -1.0;
$bestPhrase = '';
$bestPosition = -1;
$bestResultIndex = -1;
foreach ($scores as $phrase => $scores1) {
foreach ($scores1 as $position => $scores2) {
foreach ($scores2 as $resultIndex => $score) {
if (($bestPhrase == '') or
($score > $bestScore) or
(($score == $bestScore) and // tie break with importance:
($this->termArray->results[$phrase][$resultIndex]["importance"] > $this->termArray->results[$bestPhrase][$bestResultIndex]["importance"]))) {
$bestScore = $score;
$bestPhrase = $phrase;
$bestPosition = $position;
$bestResultIndex = $resultIndex;
}
}
}
}
return [$bestPhrase, $bestPosition, $bestResultIndex];
}
// Return the result with the highest score
function BestResult() {
$this->UpdateScores();
$bestScore = -1.0;
$bestPhrase = '';
$bestPosition = -1;
$bestResultIndex = -1;
foreach ($this->scores as $phrase => $scores1) {
foreach ($scores1 as $position => $scores2) {
foreach ($scores2 as $resultIndex => $score) {
if (($bestPhrase == '') or
($score > $bestScore) or
(($score == $bestScore) and // tie break with importance:
($this->termArray->results[$phrase][$resultIndex]["importance"] > $this->termArray->results[$bestPhrase][$bestResultIndex]["importance"]))) {
$bestScore = $score;
$bestPhrase = $phrase;
$bestPosition = $position;
$bestResultIndex = $resultIndex;
}
}
}
}
return $this->termArray->results[$bestPhrase][$bestResultIndex];
}
// Sorts results after disambiguation is complete
// Use with $importance set to true to sort by the Nominatim importance
// field. Otherwise it will sort by score.
function SortResults($importance = false) {
$this->UpdateScores();
$phraseScore = [];
foreach ($this->scores as $phrase => $scores1) {
foreach ($scores1 as $position => $scores2) {
// $scores2 is an array of resultIndex => score, but there
// should only be one result for each phrase and position
$score = reset($scores2); // get the first score
if (isset($phraseScore[$phrase])) {
$phraseScore[$phrase] = max($score, $phraseScore[$phrase]);
} else {
$phraseScore[$phrase] = $score;
}
}
}
$phraseImportance = [];
foreach ($this->scores as $phrase => $scores1) {
$result = reset($this->termArray->results[$phrase]);
$phraseImportance[$phrase] = [$result["importance"], $phraseScore[$phrase]]; // append score to use as a tie-breaker
$phraseScore[$phrase] = [$phraseScore[$phrase], $result["importance"]]; // append importance to use as a tie-breaker
}
$rval = [];
$rval["results"] = []; // results in sorted order
$rval["scores"] = []; // the actual scores
if ($importance) {
arsort($phraseImportance);
foreach ($phraseImportance as $phrase => $importanceScore) {
$rval["results"][] = reset($this->termArray->results[$phrase]);
$rval["scores"][] = $importanceScore[1];
}
} else {
arsort($phraseScore);
foreach ($phraseScore as $phrase => $scoreImportance) {
$rval["results"][] = reset($this->termArray->results[$phrase]);
$rval["scores"][] = $scoreImportance[0];
}
}
return $rval;
}
// checks if we have one result for each phrase
protected function doneDisambiguation() {
$phrases = $this->termArray->getAllPhrases();
foreach ($phrases as $phrase) {
if (count($this->termArray->results[$phrase]) > 1) {
return false;
}
}
return true;
}
// checks if any terms conflict
protected function donePhase1() {
$terms = $this->termArray->getAllTerms();
foreach ($terms as $term) {
if ($this->weights[$term->phrase][$term->position] != 1.0) {
return false;
}
}
return true;
}
// First resolves conflicting terms, then chooses results
function TwoPhaseDisambiguate($timeLimit = 0) {
$startTime = time();
$phrases = $this->termArray->getAllPhrases();
if (count($phrases) == 0) {
echo "Error: no terms available ";
return false;
}
if (count($phrases) == 1) {
// choose the first result, since there's no disambiguation
// we can do anyways
$this->ChooseResult($phrases[0], 0);
return $this->termArray;
}
$this->UpdateWeights();
if (printOutput()) {
echo "<br>Weights:<br>";
var_dump($this->weights);
echo "<br>Alternate weights:<br>";
var_dump($this->alternateWeights);
echo "<br>";
}
while (! $this->doneDisambiguation()) {
if (($timeLimit > 0) and ((time() - $startTime) > $timeLimit)) {
return false;
}
$this->UpdateScores();
if (printOutput()) {
$this->PrintScoreTable();
}
if (! $this->donePhase1()) {
if (printOutput()) {
echo "<br>Running phase 1<br>";
}
$scoresToUse = [];
foreach ($this->termArray->getAllTerms() as $term) {
if ($this->weights[$term->phrase][$term->position] != 1.0) {
foreach ($this->termArray->results[$term->phrase] as $resultIndex => $result) {
$scoresToUse[$term->phrase][$term->position][$resultIndex] = $this->scores[$term->phrase][$term->position][$resultIndex];
}
}
}
$r = $this->FindBestPhrase($scoresToUse);
if (printOutput()) {
echo "The ambiguous phrase with the best score is {$r[0]} at position {$r[1]}<br><br>";
}
$this->ChoosePhrase($r[0], $r[1]);
$this->UpdateWeights();
} else {
if (printOutput()) {
echo "<br>Running phase 2<br>";
}
$scoresToUse = [];
foreach ($this->termArray->getAllTerms() as $term) {
if (count($this->termArray->results[$term->phrase]) > 1) {
foreach ($this->termArray->results[$term->phrase] as $resultIndex => $result) {
$scoresToUse[$term->phrase][$term->position][$resultIndex] = $this->scores[$term->phrase][$term->position][$resultIndex];
}
}
}
$r = $this->FindBestPhrase($scoresToUse);
if (printOutput()) {
echo "The result with the best score is result {$r[2]} for phrase {$r[0]}<br><br>";
}
$this->ChooseResult($r[0], $r[2]);
}
}
if (printOutput()) {
echo "<br>Final score table:<br>";
$this->UpdateScores();
$this->PrintScoreTable();
}
return $this->termArray;
}
function OnePhaseDisambiguate($timeLimit = 0) {
$startTime = time();
$phrases = $this->termArray->getAllPhrases();
if (count($phrases) == 0) {
echo "Error: no terms available ";
return false;
}
if (count($phrases) == 1) {
// choose the first result, since there's no disambiguation
// we can do anyways
$this->ChooseResult($phrases[0], 0);
return $this->termArray;
}
if (printOutput()) {
echo "<br>Weights:<br>";
var_dump($this->weights);
echo "<br>Alternate weights:<br>";
var_dump($this->alternateWeights);
echo "<br>";
}
while ((! $this->doneDisambiguation()) or (! $this->donePhase1())) {
if (($timeLimit > 0) and ((time() - $startTime) > $timeLimit)) {
return false;
}
$this->UpdateWeights();
$this->UpdateScores();
if (printOutput()) {
$this->PrintScoreTable();
echo "<br>Running the only phase of the 1 phase disambiguator<br>";
}
$scoresToUse = [];
foreach ($this->termArray->getAllTerms() as $term) {
if ((count($this->termArray->results[$term->phrase]) > 1) or ($this->weights[$term->phrase][$term->position] != 1.0)) {
foreach ($this->termArray->results[$term->phrase] as $resultIndex => $result) {
$scoresToUse[$term->phrase][$term->position][$resultIndex] = $this->scores[$term->phrase][$term->position][$resultIndex];
}
}
}
$r = $this->FindBestPhrase($scoresToUse);
if (printOutput()) {
echo "The result with the best score is result {$r[2]} for phrase {$r[0]} at postition {$r[1]}<br><br>";
}
$this->ChoosePhrase($r[0], $r[1]);
$this->ChooseResult($r[0], $r[2]);
}
if (printOutput()) {
echo "<br>Final score table:<br>";
$this->UpdateScores();
$this->PrintScoreTable();
}
return $this->termArray;
}
// can override this in sub classes
function Disambiguate($timeLimit = 0) {
//return $this->OnePhaseDisambiguate();
return $this->TwoPhaseDisambiguate($timeLimit);
}
}
class InverseDisambiguator extends Disambiguator {
function CalculateScore($rowTerm, $rowResultIndex, $rowResult) {
$score = 0.0;
foreach ($this->termArray->getAllTerms() as $colTerm) {
if ($rowTerm->phrase != $colTerm->phrase) {
if (!((isset($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position]) and
($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position] == 0.0)) or
(isset($this->alternateWeights[$colTerm->phrase][$colTerm->position][$rowTerm->phrase][$rowTerm->position]) and
($this->alternateWeights[$colTerm->phrase][$colTerm->position][$rowTerm->phrase][$rowTerm->position] == 0.0)))) {
$minVal = 100000.0;
foreach ($this->termArray->results[$colTerm->phrase] as $colResultIndex => $colResult) {
$dist = $this->distance[$rowTerm->phrase][$colTerm->phrase][$rowResultIndex][$colResultIndex];
if ($dist < $minVal) {
$minVal = $dist;
}
}
if ($minVal < 0.001) {
// prevent division by zero errors
$minVal = 0.001;
}
$score += (1.0 / $minVal);
}
}
}
return $score;
}
}
class WeightedInverseDisambiguator extends Disambiguator {
function CalculateScore($rowTerm, $rowResultIndex, $rowResult) {
$score = 0.0;
foreach ($this->termArray->getAllTerms() as $colTerm) {
if ($rowTerm->phrase != $colTerm->phrase) {
if (!((isset($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position]) and
($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position] == 0.0)) or
(isset($this->alternateWeights[$colTerm->phrase][$colTerm->position][$rowTerm->phrase][$rowTerm->position]) and
($this->alternateWeights[$colTerm->phrase][$colTerm->position][$rowTerm->phrase][$rowTerm->position] == 0.0)))) {
$minVal = 100000.0;
foreach ($this->termArray->results[$colTerm->phrase] as $colResultIndex => $colResult) {
$dist = $this->distance[$rowTerm->phrase][$colTerm->phrase][$rowResultIndex][$colResultIndex];
if ($dist < $minVal) {
$minVal = $dist;
}
}
if ($minVal < 0.001) {
// prevent division by zero errors
$minVal = 0.001;
}
if (isset($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position])) {
$score += (1.0 / $minVal * $this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position]);
} else {
$score += (1.0 / $minVal * $this->weights[$colTerm->phrase][$colTerm->position]);
}
}
}
}
return $score;
}
}
class WeightedNormalizedInverseDisambiguator extends Disambiguator {
function CalculateScore($rowTerm, $rowResultIndex, $rowResult) {
$score = 0.0;
$totalWeight = 0.0;
foreach ($this->termArray->getAllTerms() as $colTerm) {
if ($rowTerm->phrase != $colTerm->phrase) {
if (!((isset($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position]) and
($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position] == 0.0)) or
(isset($this->alternateWeights[$colTerm->phrase][$colTerm->position][$rowTerm->phrase][$rowTerm->position]) and
($this->alternateWeights[$colTerm->phrase][$colTerm->position][$rowTerm->phrase][$rowTerm->position] == 0.0)))) {
$minVal = 100000.0;
foreach ($this->termArray->results[$colTerm->phrase] as $colResultIndex => $colResult) {
$dist = $this->distance[$rowTerm->phrase][$colTerm->phrase][$rowResultIndex][$colResultIndex];
if ($dist < $minVal) {
$minVal = $dist;
}
}
if ($minVal < 0.001) {
// prevent division by zero errors
$minVal = 0.001;
}
if (isset($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position])) {
$score += (max($this->minDistances[$rowTerm->phrase][$colTerm->phrase], 0.001)
/ $minVal * $this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position]);
$totalWeight += $this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position];
} else {
$score += (max($this->minDistances[$rowTerm->phrase][$colTerm->phrase], 0.001)
/ $minVal * $this->weights[$colTerm->phrase][$colTerm->position]);
$totalWeight += $this->weights[$colTerm->phrase][$colTerm->position];
}
}
}
}
if ($totalWeight == 0.0) {
// in this case there is only one unique phrase
return 0.0;
}
return ($score / $totalWeight);
}
}
class InverseFrequencyDisambiguator extends InverseDisambiguator {
function CalculateScore($rowTerm, $rowResultIndex, $rowResult) {
return (parent::CalculateScore($rowTerm, $rowResultIndex, $rowResult) * count($this->termArray->terms[$rowTerm->phrase]));
}
}
class WeightedInverseFrequencyDisambiguator extends WeightedInverseDisambiguator {
function CalculateScore($rowTerm, $rowResultIndex, $rowResult) {
$totalWeight = array_sum($this->weights[$rowTerm->phrase]);
if (isset($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$rowTerm->phrase])) {
foreach ($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$rowTerm->phrase] as $colPosition => $altWeight) {
$totalWeight -= $this->weights[$rowTerm->phrase][$colPosition];
$totalWeight += $altWeight;
}
}
return (parent::CalculateScore($rowTerm, $rowResultIndex, $rowResult) * $totalWeight);
}
}
class WeightedNormalizedInverseFrequencyDisambiguator extends WeightedNormalizedInverseDisambiguator {
function CalculateScore($rowTerm, $rowResultIndex, $rowResult) {
$totalWeight = array_sum($this->weights[$rowTerm->phrase]);
if (isset($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$rowTerm->phrase])) {
foreach ($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$rowTerm->phrase] as $colPosition => $altWeight) {
$totalWeight -= $this->weights[$rowTerm->phrase][$colPosition];
$totalWeight += $altWeight;
}
}
return (parent::CalculateScore($rowTerm, $rowResultIndex, $rowResult) * $totalWeight);
}
}
class WeightedDistanceDisambiguator extends Disambiguator {
function CalculateScore($rowTerm, $rowResultIndex, $rowResult) {
$score = 0.0;
foreach ($this->termArray->getAllTerms() as $colTerm) {
if ($rowTerm->phrase != $colTerm->phrase) {
if (!((isset($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position]) and
($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position] == 0.0)) or
(isset($this->alternateWeights[$colTerm->phrase][$colTerm->position][$rowTerm->phrase][$rowTerm->position]) and
($this->alternateWeights[$colTerm->phrase][$colTerm->position][$rowTerm->phrase][$rowTerm->position] == 0.0)))) {
$minVal = 100000.0;
foreach ($this->termArray->results[$colTerm->phrase] as $colResultIndex => $colResult) {
$dist = $this->distance[$rowTerm->phrase][$colTerm->phrase][$rowResultIndex][$colResultIndex];
if ($dist < $minVal) {
$minVal = $dist;
}
}
if (isset($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position])) {
$score -= ( $minVal * $this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position]);
} else {
$score -= ( $minVal * $this->weights[$colTerm->phrase][$colTerm->position]);
}
}
}
}
return $score;
}
}
class TotalDistanceDisambiguator extends Disambiguator {
function CalculateScore($rowTerm, $rowResultIndex, $rowResult) {
$score = 0.0;
foreach ($this->termArray->getAllTerms() as $colTerm) {
if ($rowTerm->phrase != $colTerm->phrase) {
if (!((isset($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position]) and
($this->alternateWeights[$rowTerm->phrase][$rowTerm->position][$colTerm->phrase][$colTerm->position] == 0.0)) or
(isset($this->alternateWeights[$colTerm->phrase][$colTerm->position][$rowTerm->phrase][$rowTerm->position]) and
($this->alternateWeights[$colTerm->phrase][$colTerm->position][$rowTerm->phrase][$rowTerm->position] == 0.0)))) {
$minVal = 100000.0;
foreach ($this->termArray->results[$colTerm->phrase] as $colResultIndex => $colResult) {
$dist = $this->distance[$rowTerm->phrase][$colTerm->phrase][$rowResultIndex][$colResultIndex];
if ($dist < $minVal) {
$minVal = $dist;
}
}
$score -= $minVal;
}
}
}
return $score;
}
}
class Inverse1PhaseDisambiguator extends InverseDisambiguator {
function Disambiguate($timeLimit = 0) {
return $this->OnePhaseDisambiguate($timeLimit);
}
}
class WeightedInverse1PhaseDisambiguator extends WeightedInverseDisambiguator {
function Disambiguate($timeLimit = 0) {
return $this->OnePhaseDisambiguate($timeLimit);
}
}
class WeightedNormalizedInverse1PhaseDisambiguator extends WeightedNormalizedInverseDisambiguator {
function Disambiguate($timeLimit = 0) {
return $this->OnePhaseDisambiguate($timeLimit);
}
}
class InverseFrequency1PhaseDisambiguator extends InverseFrequencyDisambiguator {
function Disambiguate($timeLimit = 0) {
return $this->OnePhaseDisambiguate($timeLimit);
}
}
class WeightedInverseFrequency1PhaseDisambiguator extends WeightedInverseFrequencyDisambiguator {
function Disambiguate($timeLimit = 0) {
return $this->OnePhaseDisambiguate($timeLimit);
}
}
class WeightedNormalizedInverseFrequency1PhaseDisambiguator extends WeightedNormalizedInverseFrequencyDisambiguator {
function Disambiguate($timeLimit = 0) {
return $this->OnePhaseDisambiguate($timeLimit);
}
}
class WeightedDistance1PhaseDisambiguator extends WeightedDistanceDisambiguator {
function Disambiguate($timeLimit = 0) {
return $this->OnePhaseDisambiguate($timeLimit);
}
}
class TotalDistance1PhaseDisambiguator extends TotalDistanceDisambiguator {
function Disambiguate($timeLimit = 0) {
return $this->OnePhaseDisambiguate($timeLimit);
}
}