Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';非类型';对象不适用于搭配函数_Python_Nltk_Typeerror_Collocation - Fatal编程技术网

Python ';非类型';对象不适用于搭配函数

Python ';非类型';对象不适用于搭配函数,python,nltk,typeerror,collocation,Python,Nltk,Typeerror,Collocation,我是NLTK新手,尝试返回搭配输出。我得到了输出,同时也没有得到任何输出。下面是我的代码、输入和输出 import nltk from nltk.corpus import stopwords def performBigramsAndCollocations(textcontent, word): stop_words = set(stopwords.words('english')) pattern = r'\w+' tokenizedwords = nltk.r

我是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回溯(最近一次调用上次)
在里面
43最频繁的单词after,搭配词=performigrams和搭配(textcontent,word)
44打印(已排序(mostfrequentwordafter,key=lambda元素:(元素[1],元素[0]),反向=True))
--->45打印(已排序(词语搭配))
TypeError:“非类型”对象不可编辑

您能帮我解决这个问题吗?

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

mylist = [0,1]
print(sorted(mostfrequentwordafter, key=lambda element: (element[mylist]), reverse=True))
配置()有缺陷,导致nltk出错。我最近遇到了这个问题,并且能够通过使用搭配列表()解决这个问题。 试试这种方法

collocationwords = tokenizedwords.collocation_list()

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

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
    

我认为当您使用lambda时,错误在第44行。你能试着运行这个程序并告诉我输出结果吗<代码>打印(已排序(mostfrequentwordafter,key=lambda元素:((元素[1],元素[0]),reverse=True)))该语句给了我语法错误。如果这不起作用,请查看
https://stackoverflow.com/questions/8966538/syntax-behind-sortedkey-lambda
提供一个解决方案,然后说“好吧,如果它不起作用,那就试试这个”是没有意义的。如果你不确定,不要往墙上扔意大利面。喂!请添加说明以显示您所修复的内容。非常感谢。使用.collaboration_list()而不是使用.collaboration(),这样它就可以工作了。
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