如何解决使用nltk.conditionalfreqdist.plot
我是NLTK的新手。到目前为止,我做得很好,直到遇到了这个问题。我尝试了多种方法来解决这个问题,但是失败了。
问题类似于:Read two txt file and find the Universal POS tags frequency distribution for each file using nltk.ConditionalFreqDist(). Afterward print out the results using the nltk.ConditionalFreqDist.plot()
假设两个txt文件分别读取为x和y。到目前为止,这是我的代码:
import nltk
words1=nltk.tokenize.word_tokenize(x)
tags1= nltk.pos_tag(words1,tagset='universal')
words2=nltk.tokenize.word_tokenize(y)
tags2=nltk.pos_tag(words2,tagset='universal')
lst1=list()
lst2=list()
for w in tags1:
lst1.append(w[1])
for w in tags2:
lst2.append(w[1])
我试图提取每个txt中的所有POS作为列表。但是不知道如何使用conditionalFreqDist查看分布。
编辑: 我知道了!只需添加一个条件即可。
代码如下:
import nltk
words1=nltk.tokenize.word_tokenize(x)
tags1= nltk.pos_tag(words1,tagset='universal')
lst1=list()
lst2=list()
for w in tags1:
lst1.append(w[1])
for w in tags2:
lst2.append(w[1])
keywords=['PRT','PRON','CONJ','NOUN','VERB','ADV','ADP','DET','X','NUM','ADJ']
b = ConditionalFreqDist()
for w in lst1:
if w in keywords:
b["crime-and-punishment.txt"][w]+=1
for w in lst2:
if w in keywords:
b["Alice-Adventures-in-Wonderland.txt"][w]+=1
b.plot()
从set(lst1)中提取关键字以获取所有POS。 该图如下所示:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。