29 lines
673 B
Python
29 lines
673 B
Python
from re import sub, split
|
|
|
|
|
|
def read_from_file(path):
|
|
with open(path, 'r') as file:
|
|
return file.read()
|
|
|
|
|
|
def remove_symbols(string):
|
|
return sub(r'[^A-Za-z ]', '', string).lower()
|
|
|
|
|
|
def count_words(path):
|
|
with open(path, 'r') as file:
|
|
words = split('\s+', remove_symbols(file.read()))
|
|
if words[0] == '': words = words[1:]
|
|
if words[-1] == '': words = words[:-1]
|
|
|
|
word_counts = {}
|
|
for word in words:
|
|
if not word in word_counts: word_counts[word] = 0
|
|
word_counts[word] += 1
|
|
return word_counts
|
|
|
|
|
|
if __name__ == "__main__":
|
|
alice_dict = count_words('alice_in_wonderland.txt')
|
|
for word, value in alice_dict.items():
|
|
print(word, value) |