-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmost_common_words.py
135 lines (88 loc) · 3.18 KB
/
most_common_words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# implement use of a dictionary with (word : value)
#3. if num_of_words is greater than the amount of unique words then print all words
def main():
words = {}
fil = get_file_name()
num_of_words = get_num_of_words()
#return a list of words
separated_words = read_file(fil)
list_of_words = get_clean_word(separated_words)
dict_words = get_blank_dict(list_of_words)
dict_words = count_words(list_of_words, dict_words)
determine_top_words(dict_words, num_of_words)
def get_num_of_words() -> int:
num_of_words = input("Enter how many top words you want to see: ")
num_of_words = int(num_of_words)
return num_of_words
def get_file_name() -> str:
fil = input("Enter the name of the file: ")
return fil
def read_file(file_name) -> list:
#file_name = "/Users/seancarnahan/PycharmProjects/ECS10/MostCommonWord/check_test_file.txt"
#file_name = "/Users/seancarnahan/PycharmProjects/ECS10/MostCommonWord/one_more_night.txt"
with open(file_name, 'r') as myfile:
data = myfile.read().replace('\n', ' ')
list_of_words = data.split(" ")
return list_of_words
def get_clean_word(words : list) -> list:
clean_words = []
strip_chars = [",", ".", ":", ";", "\"", "|", "\\", "!", "@", "$", "%", "^", "&", "*", "()", "_", "+", "-", "=", "[", "]", "{", "}", "<", ">", "?", "/", "~", "`", "\'", "/#"]
strip_chars_len = len(strip_chars)
words_to_skip = ["a", "an", "and", "in", "is", "the"]
for word in words:
word = word.strip()
checker = 0
while checker <= strip_chars_len:
for i in strip_chars:
word = word.strip(i)
checker += 1
word = word.lower()
if word in words_to_skip or word == "":
pass
else:
clean_words.append(word)
return clean_words
def get_blank_dict(words : list) -> dict:
dict_words = {}
for word in words:
dict_words[word] = 0
return dict_words
def count_words(list_of_words, dict_words) -> dict:
for word in list_of_words:
dict_words[word] += 1
return dict_words
def determine_top_words(dict_words, num_of_words) -> None:
checker = num_of_words
len_dict_words = len(dict_words)
x = 0
while x < len_dict_words:
n = 0
words = []
values = dict_words.values()
max_value = max(values)
for word in dict_words:
if dict_words[word] == max_value:
words.append(word)
n += 1
x += 1
checker -= 1
for word in words:
del dict_words[word]
words.sort()
len_of_words = len(words)
print("The following words appeared", max_value, "times each: ", end="")
if n == 1:
for i in range(len_of_words):
print(words[0], end="")
else:
for i in range(len_of_words - 1):
if i == 0:
print(words[i] + ",", end = "")
else:
print(" " + words[i] + ",", end="")
print(" " + words[-1], end="")
print("")
if checker <= 0:
break
if __name__ == "__main__":
main()