2017-09-08 47 views
0

大家好,我是Python的新手,我有一個文本文件,我想分析一下每個單詞的使用次數。我一直在嘗試一段時間,但是我得到了ex:[('t', 1), ('i', 1), ('m', 1), ('e', 1), ('.', 1)]的輸出。當我正在尋找如下輸出時:('easy',5)等等。計數文本文件中每個單詞的使用次數使用python

我的文本文件中的文本示例: 「不可能相信公司提供的信息 - 即使他們已經確認付款已經付清(已經需要一週的時間),他們可以回覆給你,說不是,他們也拒絕爲這種情況承擔任何責任,他們也沒有幫你任何退款,但只是將你介紹給小額賠償法庭,當你在國外有相當多的顧客時,這不是一個很好的建議。「

感謝您的幫助!

下面是我當前的代碼:

from nltk import FreqDist 
    text = open('GC_review.txt') 
    for word in text.read().split(): 
     fdist1 = FreqDist(word) 
     print (fdist1.most_common(100)) 

回答

2

您下面的代碼:

>>> from collections import defaultdict 
>>> dict = defaultdict(int) 
>>> for word in test.split(): 
...  dict[word] += 1 
... 
>>> print dict #defaultdict(<type 'int'>, {'and': 2, 'already': 1, 'help': 1, 'just': 1, 'when': 1, 'is': 2, 'some': 1, 'back': 1, "they've": 1, 'really': 1, 'say': 1, 'customer': 1, 'have': 1, 'impossible': 1, 'trust': 1, '(which': 1, 'quite': 1, 'out': 1, 'even': 1, 'information': 1, 'confirmed': 1, 'court': 1, 'takes': 1, 'for': 1, 'also': 1, 'with': 2, '-': 1, 'been': 1, 'any': 2, 'to': 4, 'take': 1, 'They': 1, 'which': 1, 'taken': 1, 'you': 4, 'has': 1, 'cases.': 1, 'Nor': 1, 'gives': 1, 'do': 1, 'good': 1, 'week),': 1, 'that': 1, 'company': 1, 'after': 1, 'paid': 1, 'it': 1, 'abroad.': 1, 'but': 1, 'they': 2, 'not': 1, 'such': 1, 'bit': 1, 'chargebacks': 1, 'come': 1, 'payment': 1, 'a': 3, 'refuse': 1, "wasn't.": 1, 'of': 1, 'It': 1, 'responsibility': 1, 'can': 1, 'suggestion': 1, 'small': 1, 'claims': 1, 'the': 3, 'refer': 1}) 

我希望這將有助於.. :)

+0

謝謝Nimi工作:)! –

1

你在單詞計數字母代替文字的在文本中。更改這些行:

for word in text.read().split(): 
    fdist1 = FreqDist(word) 

到:需要

fdist1 = FreqDist(text.read().split()) 

沒有循環。

+0

DYZ awesomeee!該死的應該早些時候請求幫助:D –

相關問題