| 1 |
<?php |
| 2 |
require("PorterStemmer.php"); |
| 3 |
|
| 4 |
class Word{ |
| 5 |
public $text; |
| 6 |
public $suggestion; |
| 7 |
} |
| 8 |
|
| 9 |
class SpellDic{ |
| 10 |
// $fileName [String] |
| 11 |
// The dictionary file name |
| 12 |
private $fileName = "wordlist.txt"; |
| 13 |
|
| 14 |
// $_lang [String] |
| 15 |
// The target language |
| 16 |
private $_lang; |
| 17 |
|
| 18 |
// $_encoding [String] |
| 19 |
// The encoding |
| 20 |
private $_encoding; |
| 21 |
|
| 22 |
// $_words [Array<Word>] |
| 23 |
// The word list to be checked |
| 24 |
private $_words; |
| 25 |
|
| 26 |
// $_dic [Array] |
| 27 |
// The array that mimic the dictionary. It can be more |
| 28 |
// sophisticated. However, this is only a demo. |
| 29 |
private $_dic; |
| 30 |
|
| 31 |
// SIMI [Number] |
| 32 |
// The threshold of the similarity |
| 33 |
const SIMI = 0.6; |
| 34 |
|
| 35 |
public function __construct(){ |
| 36 |
$this->_dic = explode("\n", file_get_contents($this->fileName)); |
| 37 |
} |
| 38 |
|
| 39 |
public function setLanguage($lang){ |
| 40 |
$this->_lang = $lang; |
| 41 |
} |
| 42 |
|
| 43 |
public function setEncoding($encoding){ |
| 44 |
$this->_encoding = $encoding; |
| 45 |
} |
| 46 |
|
| 47 |
public function setWords($words){ |
| 48 |
$this->_words = $words; |
| 49 |
} |
| 50 |
|
| 51 |
public function add($word){ |
| 52 |
$handle = fopen($this->fileName, 'a'); |
| 53 |
fwrite($handle, "$word\n"); |
| 54 |
fclose($handle); |
| 55 |
return "{response:[]}"; |
| 56 |
} |
| 57 |
|
| 58 |
public function check(){ |
| 59 |
// summary: |
| 60 |
// Check the spelling and return a HTML page with a JSON string like |
| 61 |
// {response: [ |
| 62 |
// { |
| 63 |
// text :"teest", |
| 64 |
// suggestion:["test","treat"] |
| 65 |
// } |
| 66 |
// ]} |
| 67 |
if($this->_lang == "EN"){ |
| 68 |
$len = count($this->_words); |
| 69 |
for($i = 0; $i < $len; $i++){ |
| 70 |
$this->_words[$i]->suggestion = $this->lookup($this->_words[$i]->text); |
| 71 |
} |
| 72 |
$temp = array(); |
| 73 |
for($i = 0; $i < $len; $i++){ |
| 74 |
if($this->_words[$i]->suggestion !== NULL){ |
| 75 |
array_push($temp, $this->_words[$i]); |
| 76 |
} |
| 77 |
} |
| 78 |
return "{response:" . json_encode($temp). "}"; |
| 79 |
}else{ |
| 80 |
// This is only a demo, so keep the text as-is |
| 81 |
// if the encoding is not UTF-8 or the language is |
| 82 |
// not English |
| 83 |
return "{response:[]}"; // No spelling error found |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
private function lookup($word){ |
| 88 |
// summary: |
| 89 |
// Look for the given word. If it is not found, a suggestion list |
| 90 |
// will be returned, else an empty list will be returned. |
| 91 |
$len = count($this->_dic); |
| 92 |
$lst = array(); |
| 93 |
for($i = 0; $i < $len; $i++){ |
| 94 |
if($word == $this->_dic[$i] || PorterStemmer::getStem($word) == $this->_dic[$i]){ |
| 95 |
return NULL; |
| 96 |
}else{ |
| 97 |
// Compare the two words to get the similarity. |
| 98 |
$num = $this->_lcs($word, $this->_dic[$i]); |
| 99 |
if($num / strlen($word) > self::SIMI && $num / strlen($this->_dic[$i]) > self::SIMI){ |
| 100 |
array_push($lst, $this->_dic[$i]); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
return $lst; |
| 105 |
} |
| 106 |
|
| 107 |
private function _lcs($word1, $word2){ |
| 108 |
// summary: |
| 109 |
// Longest Common Subsequence |
| 110 |
/* $f[$i][$j] = max(f[$i-1][$j-1] + same($i, $j), f[$i-1][$j], f[$i][$j-1];*/ |
| 111 |
$len1 = strlen($word1); |
| 112 |
$len2 = strlen($word2); |
| 113 |
for($i = 0; $i <= $len1; $i++){ $f[$i][0] = 0; } |
| 114 |
for($j = 0; $j <= $len2; $j++){ $f[0][$j] = 0; } |
| 115 |
for($i = 1; $i <= $len1; $i++){ |
| 116 |
for($j = 1; $j <= $len2; $j++){ |
| 117 |
$f[$i][$j] = max($f[$i - 1][$j - 1] + ($word1[$i - 1] == $word2[$j - 1] ? 1 : 0), |
| 118 |
max($f[$i - 1][$j], $f[$i][$j - 1])); |
| 119 |
} |
| 120 |
} |
| 121 |
return $f[$i - 1][$j - 1]; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
function text2Words($text){ |
| 126 |
$result = array(); |
| 127 |
$words = explode (" ", $text); |
| 128 |
$len = count($words); |
| 129 |
for($i = 0; $i < $len; $i++){ |
| 130 |
$word = new Word(); |
| 131 |
$word->text = $words[$i]; |
| 132 |
array_push($result, $word); |
| 133 |
} |
| 134 |
return $result; |
| 135 |
} |
| 136 |
|
| 137 |
header('Content-Type: text/html; charset=UTF-8'); |
| 138 |
|
| 139 |
$sourceText = array_key_exists("content", $_REQUEST) ? $_REQUEST["content"] : ""; |
| 140 |
$lang = array_key_exists("lang", $_REQUEST) ? $_REQUEST["lang"] : "EN"; |
| 141 |
$action = array_key_exists("action", $_REQUEST) ? $_REQUEST["action"] : "query"; |
| 142 |
$callback = array_key_exists("callback", $_REQUEST) ? $_REQUEST["callback"] : "callback"; |
| 143 |
|
| 144 |
$dic = new SpellDic(); |
| 145 |
$dic->setLanguage($lang); |
| 146 |
if($action == "query"){ |
| 147 |
$dic->setWords(text2Words(trim($sourceText))); |
| 148 |
echo "$callback(" . $dic->check() . ");"; // JSONP Specification |
| 149 |
}else if ($action == "update"){ |
| 150 |
echo "$callback(" . $dic->add($sourceText) . ");"; |
| 151 |
} |
| 152 |
?> |