“非类型”对象不适合搭配函数

2024-05-14 03:44:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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),('productions',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

你能帮我解决这个问题吗


Tags: inelementwordstopwordssortedprintnltk
3条回答

配置()有缺陷,导致nltk出错。我最近遇到了这个问题,并且能够通过使用搭配列表()解决这个问题。 试试这种方法

collocationwords = tokenizedwords.collocation_list()

键在运行之前转换集合项key=实际上意味着当我在这个列表中运行时,我会-所以当您使用key=lambda element: (element[1], element[0])时,您要求它运行两次。相反,试试这样的方法。请注意,这可能不完全正确,因为现在是早上7点,我刚刚醒来,如果它不适合您,我将在稍后编辑它

mylist = [0,1]
print(sorted(mostfrequentwordafter, key=lambda element: (element[mylist]), reverse=True))

使用下面的代码,它应该可以工作。

def performBigramsAndCollocations(textcontent, word):
    
    from nltk.corpus import stopwords
    from nltk import ConditionalFreqDist
    tokenizedword = nltk.regexp_tokenize(textcontent, pattern = r'\w*', gaps = False)
    tokenizedwords = [x.lower() for x in tokenizedword if x != '']
    tokenizedwordsbigrams=nltk.bigrams(tokenizedwords)
    stop_words= stopwords.words('english')
    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.collocation_list()

    return mostfrequentwordafter ,collocationwords
    

相关问题 更多 >