【问题标题】:Spell Correction using TextBlob, autocorrect使用 TextBlob 进行拼写更正,自动更正
【发布时间】:2018-03-20 06:06:40
【问题描述】:

我一直在尝试实现一个可以更正多个文档中的拼写的功能。我尝试了两种方法,即TextBlobautocorrect

使用 TextBlob

def spell_correct(word_list):
    try:
        corrected = []
        for word in word_list:
            w = Word(word)
            corrected.append(w.correct())
        return corrected
    except UnicodeDecodeError:
        return None

使用自动更正

def spell_correct(word_list):
     try:
         corrected = []
         for word in word_list:
             corrected.append(spell(word))
         return corrected
     except UnicodeDecodeError:
         return None

它们都在作为输入提供的单个单词列表上工作得很好。但是,当我在 13k 行的 pandas DataFrame 中处理多个文档时,我通常会花费很多时间 KeyboardInterrupt。是我不耐烦还是有更快的拼写正确方法?

更新 这就是我如何将这些函数应用于 pandas DataFrame 中的多个文档,

df['corrected_words'] = df.words.apply(lambda x: spell_correct(x))

【问题讨论】:

  • 你是如何调用这些函数的?
  • 我以这种方式将其应用于每个条目,df['corrected_words'] = df.words.apply(lambda x: spell_correct(x))
  • 如果您编辑您的答案以回答需要代码的说明性问题,通常是首选。你能做到吗?
  • @ndmeiri 我没明白。我到底应该做什么?
  • 您可以在此处应用的一个简单优化是将文档中的单词列表转换为设置和更正单词并将其应用于所有单词列表。

标签: python pandas nlp textblob autocorrect


【解决方案1】:

这篇文章有点老了,但我遇到了类似的问题。你检查过 Wolf Garbe 的 SymSpell,这是一个快速的拼写更正,能够在 1 秒内通过 5000 个单词(在 Mac Book Pro 上)。我想这可能有助于解决您在此处发布的类似问题。他在https://github.com/wolfgarbe/SymSpell

上发布了整个文档等

确保 pip 安装它 首先

python -m pip install -U symspellpy

然后运行它:

from symspellpy.symspellpy import SymSpell 

def main():
    # maximum edit distance per dictionary precalculation
    max_edit_distance_dictionary = 2
    prefix_length = 7
    # create object
    sym_spell = SymSpell(max_edit_distance_dictionary, prefix_length)

    # create dictionary using corpus.txt
    if not sym_spell.create_dictionary(<path/to/corpus.txt>):
        print("Corpus file not found")
        return

    for key, count in sym_spell.words.items():
        print("{} {}".format(key, count))

if __name__ == "__main__":
    main()

【讨论】:

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