Hi I am working on program to print of the unique words and frequency and I came upon the below code...only piece is that I need to write it to a file on my local drive...can someone help me thanks
import re
filename = 'C:\pythonscripts\comments.txt'
word_list = re.split('\s+', file(filename).read().lower())
print 'Words in text:', len(word_list)
freq_dic = {}
punctuation = re.compile(r'[.?!,":;]')
for word in word_list:
word = punctuation.sub("", word)
try:
freq_dic[word] += 1
except:
freq_dic[word] = 1
print 'Unique words:', len(freq_dic)
freq_list = freq_dic.items()
freq_list.sort()
for word, freq in freq_list:
print word, freq
No comments:
Post a Comment