2017-10-20 63 views
0

我有一个python词典,用于NLTK情感分析。扩展词典以包含词频

注意:输入的是纯文本电子邮件内容。

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 
+1

请提供您的'input'和所需'output' –

+0

@KaushikNP谢谢你帮我提高我的问题。 –

回答

3

Python的Counter应该做的伎俩:

from collections import Counter 
result = dict(Counter(word for word in words_split if word not in stopset)) 
+0

关闭,但现在我错过了真正的布尔部分。我认为这可能需要NLTK –

+2

请注意,任何非零整数将在Python中评估为True,所以这应该工作。 –

+0

我现在试试 –