2
我試圖從語料庫中爲特定標記返回頂部出現的值。我可以將標記和單詞本身返回正常,但無法在計算結果中返回計數。Python NLTK - 基於標記返回頂部結果來計算棕色語料庫中單詞的出現次數
import itertools
import collections
import nltk
from nltk.corpus import brown
words = brown.words()
def findtags(tag_prefix, tagged_text):
cfd = nltk.ConditionalFreqDist((tag, word) for (word, tag) in tagged_text
if tag.startswith(tag_prefix))
return dict((tag, cfd[tag].keys()[:5]) for tag in cfd.conditions())
tagdictNNS = findtags('NNS', nltk.corpus.brown.tagged_words())
這將返回以下罰款
for tag in sorted(tagdictNNS):
print tag, tagdictNNS[tag]
我已成功地返回使用這種每個基於NN字計數:
pluralLists = tagdictNNS.values()
pluralList = list(itertools.chain(*pluralLists))
for s in pluralList:
sincident = words.count(s)
print s
print sincident
返回的一切。
有沒有更好的方式插入字典tagdictNN[tag]
發生?
編輯1:
pluralLists = tagdictNNS.values()[:5]
pluralList = list(itertools.chain(*pluralLists))
返回它們的大小順序從對於s循環。仍然不是正確的做法。
編輯2:更新的字典,所以他們實際上搜索NNS複數。
退房Python的收藏品中的計數器。 http://docs.python.org/2/library/collections.html – MercuryRising