首页 > 解决方案 > 'NoneType' 对象不可迭代用于搭配功能

问题描述

我是 NLTK 的新手,并试图返回搭配输出。我得到了输出,随之而来的是,我也没有得到任何结果。下面是我的代码,输入和输出。

import nltk
from nltk.corpus import stopwords


def performBigramsAndCollocations(textcontent, word):
    stop_words = set(stopwords.words('english'))
    pattern = r'\w+'
    tokenizedwords = nltk.regexp_tokenize(textcontent, pattern)
    for i in range(len(tokenizedwords)):
        tokenizedwords[i] = tokenizedwords[i].lower()
    tokenizedwordsbigrams = nltk.bigrams(tokenizedwords)
    tokenizednonstopwordsbigrams = [ (w1, w2) for w1, w2 in tokenizedwordsbigrams if w1 not in stop_words and w2 not in stop_words]
    cfd_bigrams = nltk.ConditionalFreqDist(tokenizednonstopwordsbigrams)
    mostfrequentwordafter = cfd_bigrams[word].most_common(3)
    tokenizedwords = nltk.Text(tokenizedwords)
    collocationwords = tokenizedwords.collocations()
    return mostfrequentwordafter, collocationwords


if __name__ == '__main__':
    textcontent = input()

    word = input()


    mostfrequentwordafter, collocationwords = performBigramsAndCollocations(textcontent, word)
    print(sorted(mostfrequentwordafter, key=lambda element: (element[1], element[0]), reverse=True))
    print(sorted(collocationwords))

输入:在7天的比赛中,将提供35个体育项目和4个文化活动。他带着魅力滑冰,从一个档位换到另一个档位,从一个方向换到另一个方向,比跑车还快。如果不支付电视许可费,安顿下来观看奥运会的扶手椅体育迷可能会跳高。此类邀请赛将激发体育迷的兴趣,从而吸引更多体育迷的收视率。她几乎没有注意到一辆华丽的跑车差点把他们撞倒,直到埃迪向前猛扑过去,一把抓住了她的身体。他奉承母亲,她有点生气,他说服她去骑跑车。

运动的

输出:
跑车;体育迷。

[('fans', 3), ('car', 3), ('disciplines', 1)]

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-191-40624b3de987> in <module>
     43     mostfrequentwordafter, collocationwords = performBigramsAndCollocations(textcontent, word)
     44     print(sorted(mostfrequentwordafter, key=lambda element: (element[1], element[0]), reverse=True))
---> 45     print(sorted(collocationwords))

TypeError: 'NoneType' object is not iterable

你能帮我解决这个问题吗

标签: pythonnltktypeerrorcollocation

解决方案


collocations() 有问题并导致 nltk 出错。我最近遇到了这个问题,并且能够使用 collocation_list() 解决这个问题。试试这个方法。

collocationwords = tokenizedwords.collocation_list()

推荐阅读