【问题标题】:Q : Python Spell Checker using NLTK问:使用 NLTK 的 Python 拼写检查器
【发布时间】:2021-12-19 09:39:22
【问题描述】:

所以我有这行代码使用 NLTK 库

def autospell(text):
        spells = [spell(w) for w in (nltk.word_tokenize(text))]
        return " ".join(spells) 
train_data['Phrase'][:200].apply(autospell) 

我收到这个错误,告诉我名称拼写没有定义,我不知道这是什么意思,因为我认为它来自 NLTK 库,还是我在某处遗漏了什么?

NameError                                 Traceback (most recent call last)
<ipython-input-119-582bf5662c88> in <module>()
      5         spells = [spell(w) for w in (nltk.word_tokenize(text))]
      6         return " ".join(spells)
----> 7 train_data['Phrase'][:200].apply(autospell)

2 frames
pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-119-582bf5662c88> in <listcomp>(.0)
      3         correct the spelling of the word.
      4         """
----> 5         spells = [spell(w) for w in (nltk.word_tokenize(text))]
      6         return " ".join(spells)
      7 train_data['Phrase'][:200].apply(autospell)

NameError: name 'spell' is not defined

【问题讨论】:

  • 好吧,代码正在调用一个名为spell()的函数;它在哪里声明?
  • 之前的函数是这样的: ``` !pip install nltk from autocorrect import Speller def word_tokenize(text): return nltk.word_tokenize(text) ``` 我不确定那个函数 spell 是什么来自图书馆还是应该来自我?

标签: python nltk spell-checking


【解决方案1】:

查看Spell Checker for Python,您可能应该使用autocorrect 库。 示例代码:

from autocorrect import Speller

spell = Speller(lang='en')

def autospell(text):
        spells = [spell(w) for w in (nltk.word_tokenize(text))]
        return " ".join(spells) 
train_data['Phrase'][:200].apply(autospell) 

【讨论】:

  • 啊,是的,我错过了 1 行谢谢
猜你喜欢
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多