1. 程式人生 > >如何統計英文文字中詞彙的出現次數

如何統計英文文字中詞彙的出現次數

def getText():
    txt = open('hamlet.txt', 'r').read()
    txt = txt.lower()
    for ch in '!"#$%&()*+,-./:;<=>[email protected][\\]^_{|}~':
        txt = txt.replace(ch, ' ')
    return txt

txt = getText()
words = txt.split()
counts = {}
for word in words:
    counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):
    word, count = items[i]
    print('{0:<10}{1:>5}'.format(word, count))

我統計的英文文字是哈姆雷特,只需要把你想要統計的文字拷貝到專案的根目錄,然後進行相應的修改即可。