多单词拼写更正

2024-06-07 04:22:20 发布

您现在位置:Python中文网/ 问答频道 /正文

更正一个单词的拼写错误(包括非单词和实际单词错误)很容易:

P(w|c) P(c)

其中w是拼写错误的单词,c是我们试图匹配的候选词,因此候选词是一个单词标记。在

但是在Google中,当你输入类似spelligncheck的内容时,它会将这个单词更正成两个不同的单词。现在,P(w|c)在这里很容易,如果我使用levenshtein距离。但这意味着我再也不能有一个词(更确切地说是一种象征)的候选词了。所以这将使我的字典的大小成倍增加。在

此外,当我输入app le时,Google将其更正为apple。。。在

那么,在给定一个标记字典的情况下,进行多个单词拼写更正的最佳方法是什么?在


Tags: 方法标记leapp距离内容apple字典
1条回答
网友
1楼 · 发布于 2024-06-07 04:22:20

我想你在找^{}模块之类的东西。在

我准备了这个演示,向您展示如何几乎达到您想要的效果-它显然还可以改进很多:

<?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";
    }
}

我在这里试了一下,结果是:

^{pr2}$

祝你好运!:)

相关问题 更多 >