【问题标题】:'LazyCorpusLoader' object is not iterable“LazyCorpusLoader”对象不可迭代
【发布时间】:2016-09-16 17:06:36
【问题描述】:

以下示例创建一个字谜字典。 但是,它会抛出 TypeError: 'LazyCorpusLoader' object is not an iterator:

import nltk
from nltk.corpus import words

anagrams = nltk.defaultdict(list)
for word in words:
    key = ''.join(sorted(word))
    anagrams[key].append(word)

print(anagrams['aeilnrt'])

【问题讨论】:

    标签: nltk


    【解决方案1】:

    您必须在words 语料库对象上使用.words() 方法。

    具体来说:改变

    for word in words:
    

    for word in words.words():
    

    它应该可以工作。

    【讨论】:

    • 正确答案,但 OP 发布的代码可以在 Python 2.7 中运行。我认为问题在于他们试图将它与 Python 3+ 一起使用。
    • @Silenus 好点,我没有意识到这一点。不过,我认为将代码移植到 Python 3 比将环境降级到 Python 2 更好。但这当然值得商榷。
    • 我有同样的错误信息,这是我的代码,你能帮忙吗: import nltk from nltk.corpus import names nltk.download('stopwords') nltk.download('wordnet') import警告 warnings.filterwarnings('ignore') from nltk.corpus import stopwords def cleaning(article): one = " ".join([i for i in article.lower().split() if i not in stopwords]) 二= "".join(i for i in one if i not in punctuation) 三 = " ".join(lemmatize.lemmatize(i) for i in two.split()) 返回三个 text = Search.applymap(cleaning)[ 'Q2'] text_list = [i.split() for i in text]
    • @mkheifetz 请针对您的问题发布一个新问题。我无法像这样阅读您的代码,而且这不是 cmets 的用途。
    【解决方案2】:

    我在使用 nltk 导入停用词时遇到了同样的错误

    由于以下导入而发生错误

    from nltk.corpus import stopwords
    

    将以上内容替换为以下内容对我有用

    # from nltk.corpus import stopwords
    stopwords = nltk.corpus.stopwords.words('english')
    

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 2017-01-05
      • 2015-11-11
      • 2021-06-06
      • 2013-05-27
      • 2013-08-02
      相关资源
      最近更新 更多