import re
f = open('walden.txt', mode='r')
txt = f.read()
f.close()
lyric = txt.lower()
lyric_new = re.sub('[,.:"\'?\n;-]', '', lyric)
words = lyric_new.split()
word_freq = {}
for word in words:
if word not in word_freq.keys():
word_freq[word] = 1
else:
word_freq[word] += 1
result = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)
result_string = str(result)
with open('word_freq.txt', mode='w') as f:
f.write(result_string)