【问题标题】:Spell checker with fused spelling error correction algorithm具有融合拼写纠错算法的拼写检查器
【发布时间】:2012-03-04 19:10:25
【问题描述】:

最近我浏览了几种拼写检查算法,包括简单的(如Peter Norvig's)和更复杂的(如Brill and Moore's)。但是有一种错误是他们都无法处理的。例如,如果我输入 stackoverflow 而不是 stack overflow 这些拼写检查器将无法更正错误输入(除非在术语字典中输入 stack overflow)。存储所有单词对太贵了(如果错误是 3 个单词之间没有空格,这将无济于事)。 有没有一种算法可以纠正(尽管通常是错误的类型)这种类型的错误?

我需要的一些例子:
spel checker -> spell checker
spellchecker -> spell checker
spelcheker -> spell checker

【问题讨论】:

    标签: algorithm nlp spell-checking


    【解决方案1】:

    我破解了 Norvig 的拼写校正器来做到这一点。我不得不作弊,在 Norvig 的数据文件中添加“checker”这个词,因为它从未出现过。没有这种作弊,问题真的很难。

    expertsexchange expert exchange
    spel checker spell checker
    spellchecker spell checker
    spelchecker she checker  # can't win them all
    baseball base all  # baseball isn't in the dictionary either :(
    hewent he went
    

    基本上你需要更改代码,以便:

    • 您可以在字母表中添加空格以自动探索断词。
    • 您首先检查组成短语的所有单词都在字典中,以认为该短语有效,而不仅仅是直接的字典成员身份(字典不包含短语)。
    • 您需要一种方法来根据简单单词对短语进行评分。

    后者是最棘手的,我对短语组合使用了一个脑死独立假设,即两个相邻单词的概率是它们各自概率的乘积(这里用对数概率空间中的 sum 完成),并有一个小的惩罚。我敢肯定,在实践中,您会希望保留一些二元统计数据来很好地进行拆分。

    import re, collections, math
    
    def words(text): return re.findall('[a-z]+', text.lower())
    
    def train(features):
      counts = collections.defaultdict(lambda: 1.0)
      for f in features:
        counts[f] += 1.0
      tot = float(sum(counts.values()))
      model = collections.defaultdict(lambda: math.log(.1 / tot))
      for f in counts:
        model[f] = math.log(counts[f] / tot)
      return model
    
    NWORDS = train(words(file('big.txt').read()))
    
    alphabet = 'abcdefghijklmnopqrstuvwxyz '
    
    def valid(w):
      return all(s in NWORDS for s in w.split())
    
    def score(w):
      return sum(NWORDS[s] for s in w.split()) - w.count(' ')
    
    def edits1(word):
      splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
      deletes    = [a + b[1:] for a, b in splits if b]
      transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
      replaces   = [a + c + b[1:] for a, b in splits for c in alphabet if b]
      inserts    = [a + c + b     for a, b in splits for c in alphabet]
      return set(deletes + transposes + replaces + inserts)
    
    def known_edits2(word):
      return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if valid(e2))
    
    def known(words): return set(w for w in words if valid(w))
    
    def correct(word):
      candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word]
      return max(candidates, key=score)
    
    def t(w):
      print w, correct(w)
    
    t('expertsexchange')
    t('spel checker')
    t('spellchecker')
    t('spelchecker')
    t('baseball')
    t('hewent')
    

    【讨论】:

      【解决方案2】:

      这个问题与应用于德语或荷兰语的复合拆分问题非常相似,但也包括嘈杂的英语数据。请参阅Monz & De Rijke 了解非常简单的算法(我认为可以将其实现为有限状态转换器以提高效率)和 Google 了解“复合拆分”和“分解”。

      【讨论】:

        【解决方案3】:

        我有时在 kate 中进行拼写检查时会收到这样的建议,所以肯定有一种算法可以纠正一些这样的错误。我确信可以做得更好,但一种想法是将候选人拆分到可能的位置并检查是否存在组件的紧密匹配。困难的部分是决定什么是可能的地方。在我熟悉的语言中,有些字母组合很少出现在单词中。例如,据我所知,dklh 的组合在英语单词中很少见。其他组合通常出现在单词的开头(例如unch),因此这些组合也是拆分的好猜测。在示例spelcheker 中,lc 组合并不太普遍,而ch 是常见的词首,因此拆分spelcheker 是主要候选者,然后任何体面的算法都会找到spellchecker(但它可能也会找到 spiel,所以不要自动更正,只提供建议)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-30
          • 1970-01-01
          • 1970-01-01
          • 2012-12-05
          • 2012-07-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多