-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze song lyrics.py
More file actions
53 lines (46 loc) · 1.56 KB
/
analyze song lyrics.py
File metadata and controls
53 lines (46 loc) · 1.56 KB
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
"""
EXAMPLE DICTIONARIES:
Analyzing Song Lyrics in 3 steps:
+ Create a dictionary that counts the amount of times a word shows up in the lyrics of a song
+ Create a function that finds the most common word and tells us how many times it occured
+ Create a function that collects all the words that appear at least X times in the song
"""
def main():
lyrics = ["this", "is", "a", "test","this", "this", "is", "a", "a", "a", "a", "test", "test", "test", "test", "test", "wouter", "is", "a", "test", "wouter"]
min_count = 2
aDict = lyrics_to_frequencies(lyrics)
result = word_count(aDict, min_count)
print(print_result(result))
def lyrics_to_frequencies(lyrics):
aDict = {}
for word in lyrics:
if word in aDict:
aDict[word] += 1
else :
aDict[word] = 1
return aDict
def most_common_words(aDict):
count = aDict.values()
highest_count = max(count)
list_words = []
for word in aDict:
if aDict[word] == highest_count:
list_words.append(word)
return (list_words, highest_count)
def word_count(aDict, min_count):
result = []
done = False
while not done:
common_words = most_common_words(aDict)
if common_words[1] >= min_count:
result.append(common_words)
for words in common_words[0]:
del(aDict[words])
else :
done = True
return result
def print_result(result):
for i in range (len(result)):
print(result[i][0], "appears", result[i][1], "times")
if __name__ == '__main__':
main()