-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpellCorrector.php
191 lines (174 loc) · 6.04 KB
/
SpellCorrector.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
<?php
/*
***************************************************************************
* Copyright (C) 2008 by Felipe Ribeiro *
* felipernb@gmail.com *
* http://www.feliperibeiro.com *
* *
* Permission is hereby granted, free of charge, to any person obtaining *
* a copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to *
* the following conditions: *
* *
* The above copyright notice and this permission notice shall be *
* included in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR *
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
* OTHER DEALINGS IN THE SOFTWARE. *
***************************************************************************
*/
/**
* This class implements the Spell correcting feature, useful for the
* "Did you mean" functionality on the search engine. Using a dicionary of words
* extracted from the product catalog.
*
* Based on the concepts of Peter Norvig: http://norvig.com/spell-correct.html
*
* @author Felipe Ribeiro <felipernb@gmail.com>
* @date September 18th, 2008
* @package catalog
*
*/
class SpellCorrector {
private static $NWORDS;
/**
* Reads a text and extracts the list of words
*
* @param string $text
* @return array The list of words
*/
private static function words($text) {
$matches = array();
preg_match_all("/[آ ا ب پ ت ث ج چ ح خ د ذ ر ز ژ س ش ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی]+/",strtolower($text),$matches);
return $matches[0];
}
/**
* Creates a table (dictionary) where the word is the key and the value is it's relevance
* in the text (the number of times it appear)
*
* @param array $features
* @return array
*/
private static function train(array $features) {
$model = array();
$count = count($features);
for($i = 0; $i<$count; $i++) {
$f = $features[$i];
$model[$f] +=1;
}
return $model;
}
/**
* Generates a list of possible "disturbances" on the passed string
*
* @param string $word
* @return array
*/
private static function edits1($word) {
$alphabet = ['ٍ','َ','ُ','ِ','ء','ئ','ی','ه','و','ن','م','ل','گ','ک','ق','ف','غ','ع','ظ','ط','ض','ص','ش','س','ژ','ز','ر','ذ','د','خ','ح','چ','ج','ث','ت','پ','ب','آ','ا','ً','ٌ'];
$n = mb_strlen($word);
$edits = array();
for($i = 0 ; $i<$n;$i++) {
$edits[] = mb_substr($word,0,$i).mb_substr($word,$i+1); //deleting one char
foreach($alphabet as $c) {
$edits[] = mb_substr($word,0,$i) . $c . mb_substr($word,$i+1); //substituting one char
}
}
for($i = 0; $i < $n-1; $i++) {
$edits[] = mb_substr($word,0,$i).$word[$i+1].$word[$i].mb_substr($word,$i+2); //swapping chars order
}
for($i=0; $i < $n+1; $i++) {
foreach($alphabet as $c) {
$edits[] = mb_substr($word,0,$i).$c.mb_substr($word,$i); //inserting one char
}
}
return $edits;
}
/**
* Generate possible "disturbances" in a second level that exist on the dictionary
*
* @param string $word
* @return array
*/
private static function known_edits2($word) {
$known = array();
foreach(self::edits1($word) as $e1) {
foreach(self::edits1($e1) as $e2) {
if(array_key_exists($e2,self::$NWORDS)) $known[] = $e2;
}
}
return $known;
}
/**
* Given a list of words, returns the subset that is present on the dictionary
*
* @param array $words
* @return array
*/
private static function known(array $words) {
$known = array();
foreach($words as $w) {
if(array_key_exists($w,self::$NWORDS)) {
$known[] = $w;
}
}
return $known;
}
/**
* Returns the word that is present on the dictionary that is the most similar (and the most relevant) to the
* word passed as parameter,
*
* @param string $word
* @return string
*/
public static function correct($word) {
$word = trim($word);
if(empty($word)) return;
if(is_numeric($word)) return $word;
$word = strtolower($word);
if(empty(self::$NWORDS)) {
/* To optimize performance, the serialized dictionary can be saved on a file
instead of parsing every single execution */
if(!file_exists('serialized_dictionary.txt')) {
self::$NWORDS = self::train(self::words(file_get_contents("big.txt")));
$fp = fopen("serialized_dictionary.txt","w+");
fwrite($fp,serialize(self::$NWORDS));
fclose($fp);
} else {
self::$NWORDS = unserialize(file_get_contents("serialized_dictionary.txt"));
}
}
$candidates = array();
if(self::known(array($word))) {
return $word;
} elseif(($tmp_candidates = self::known(self::edits1($word)))) {
foreach($tmp_candidates as $candidate) {
$candidates[] = $candidate;
}
} elseif(($tmp_candidates = self::known_edits2($word))) {
foreach($tmp_candidates as $candidate) {
$candidates[] = $candidate;
}
} else {
return $word;
}
$max = 0;
foreach($candidates as $c) {
$value = self::$NWORDS[$c];
if( $value > $max) {
$max = $value;
$word = $c;
}
}
return $word;
}
}
?>