【发布时间】:2020-09-02 00:00:57
【问题描述】:
我正在动手做,预期的输出是
[('fans', 3), ('car', 3), ('disciplines', 1)]
['跑车','体育迷']
我的代码如下。我能够获得第一个预期输出,但无法正确获得第二个输出。谁能帮我看看这里出了什么问题
from nltk.tokenize import RegexpTokenizer
text='Thirty-five sports disciplines and four cultural activities will be offered during seven days of competitions. He skated with charisma, changing from one gear to another, from one direction to another, faster than a sports car. Armchair sports fans settling down to watch the Olympic Games could be for the high jump if they do not pay their TV licence fee. Such invitationals will attract more viewership for sports fans by sparking interest among sports fans. She barely noticed a flashy sports car almost run them over, until Eddie lunged forward and grabbed her body away. And he flatters the mother and she kind of gets prissy and he talks her into going for a ride in the sports car.'
word='sports'
tokenizedword = nltk.tokenize.regexp_tokenize(text, pattern = '\w*', gaps = False)
#Step 2
tokenizedwords = [x.lower() for x in tokenizedword if x != '']
tokenizedwordsbigram=list(nltk.bigrams(tokenizedwords))
stop_words = set(stopwords.words('english'))
filteredwords = []
for x in tokenizedwordsbigram:
if x not in stop_words:
filteredwords.append(x)
tokenizednonstopwordsbigram = nltk.ConditionalFreqDist(filteredwords)
print(tokenizednonstopwordsbigram[word].most_common(3))
gen_text=nltk.Text(tokenizedwords)
print(gen_text.collocations())
【问题讨论】:
标签: nlp