5

Correcting one word spelling mistakes (both non-word & real-word mistakes) is easy:

P(w|c) P(c)

Where w is the incorrectly spelled word and c is the candidate we're trying to match, such that the candidate is a one word token.

But in Google, when you enter something like spelligncheck, it corrects the word into two different words. Now, P(w|c) is easy here, if i use levenshtein distance. But that means i can't have one word (one token, rather) candidates anymore. So this will increase the size of my dictionary exponentially.

Moreover when I enter app le Google corrects it to apple...

So what is the best way of doing multiple word spelling correction, given a one-token dictionary?

4

1 回答 1

0

我认为您正在寻找类似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";
    }
}

我在这里尝试过并得到以下结果:

Closest match for "spelligncheck": spellchecker
Closest match for "app le": apple

祝你好运!:)

于 2013-11-14T03:57:14.540 回答