我使用python 2.7,我想找到文本文件中单词的频率, 我使用以下表达式编写代码,但没有输出:
import nltk
import os
import re
import string
path="C:\Python27\Lib"
os.chdir(path)
frequency = {}
document_text = open('1.txt', 'r')
text_string = document_text.read().lower()
match_pattern = re.findall(r'^[\u0621-\u064A\u0660-\u0669 ]+$',
text_string)
for word in match_pattern:
count = frequency.get(word,0)
frequency[word] = count + 1
frequency_list = frequency.keys()
for words in frequency_list:
print words, frequency[words]