[英]Extend dictionary to include word frequencies
我有一個正在為NLTK情感分析而構建的python字典。
注意:輸入內容為純文本電子郵件內容。
def word_feats(words):
stopset = list(set(stopwords.words('english')))
words_split = words.split()
result = dict([(word, True) for word in words_split if word not in stopset])
return result
我想將其擴展為包括字典中的單詞頻率以及唯一單詞。
這是我目前得到的:
'To' (4666843744) = {bool} True
'ensure' (4636385096) = {bool} True
'email' (4636383752) = {bool} True
'updates' (4636381960) = {bool} True
'delivered' (4667509936) = {bool} True
'inbox,' (4659135800) = {bool} True
'please' (4659137368) = {bool} True
'add' (4659135016) = {bool} True
我想要類似以下的內容,其中末尾的數字是頻率。 它不必完全像這樣,但我希望能夠訪問每個單詞的頻率。
'To' (4666843744) = {bool} True, 100
'ensure' (4636385096) = {bool} True, 3
'email' (4636383752) = {bool} True, 40
'updates' (4636381960) = {bool} True, 3
'delivered' (4667509936) = {bool} True, 4
'inbox,' (4659135800) = {bool} True, 20
'please' (4659137368) = {bool} True, 150
'add' (4659135016) = {bool} True, 10
Python的Counter
應該可以解決問題:
from collections import Counter
result = dict(Counter(word for word in words_split if word not in stopset))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.