Ans*_*shi 5 python nlp spell-checking probability
纠正一个单词拼写错误(非单词和真实单词错误)很容易:
P(w|c) P(c)
Run Code Online (Sandbox Code Playgroud)
w拼写错误的单词在哪里,并且c是我们尝试匹配的候选词,这样候选词是单词令牌.
但是在Google中,当你输入类似内容时spelligncheck,它会将单词更正为两个不同的单词.现在,P(w|c)这里很容易,如果我使用levenshtein距离.但这意味着我再也不能拥有一个单词(一个令牌,而是一个).所以这会以指数方式增加我字典的大小.
此外,当我进入app le谷歌纠正它apple...
那么,给定单令牌字典,进行多字拼写校正的最佳方法是什么?
我认为您正在寻找类似pspell模块的东西。
我准备了这个演示,向您展示如何几乎实现您想要的目标 - 显然还可以进一步改进:
<?php
class SpellChecker
{
public function __construct($lang)
{
$this->pspell = pspell_new($lang);
}
public function check($word)
{
return pspell_check($this->pspell, $word);
}
public function closest_suggestion($word)
{
$suggestions = pspell_suggest($this->pspell, $word);
$similar_sounding_words = array_filter($suggestions,
function ($current_word) use ($word) {
return (metaphone($current_word) == metaphone($word));
});
// No similar sounding words, just return the first suggestion...
if (count($similar_sounding_words) == 0) {
return $suggestions[0];
}
// Return the closest match against similar sounding words...
return array_reduce($similar_sounding_words,
function ($prev, $next) use ($word) {
return (is_array($prev))
? $next
: ((levenshtein($prev, $word) < levenshtein($next, $word))
? $prev
: $next);
});
}
}
$spellchecker = new SpellChecker('en');
foreach (array('spelligncheck', 'app le') as $word) {
if (!$spellchecker->check($word)) {
print "Closest match for \"$word\": {$spellchecker->closest_suggestion($word)}\n";
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里尝试过并得到以下结果:
Closest match for "spelligncheck": spellchecker
Closest match for "app le": apple
Run Code Online (Sandbox Code Playgroud)
祝你好运!:)
| 归档时间: |
|
| 查看次数: |
880 次 |
| 最近记录: |