You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This Python script will find and return the most frequently appearing lyric of a song.
3
+
In the case where there is more than one lyric, it will return the lyric that appears first in the song.
4
+
5
+
This script accepts song lyrics only as *.txt* files.
6
+
A sample file is provided named *song.txt* that contains the lyrics to Don't Stop Believin' by Journey, but feel free to add your own *.txt* file to the direcctory.
7
+
8
+
Make sure to input the filename after running the script to return results.
withopen(input("Please enter file name: "), 'r') asf:
4
+
forlineinf:
5
+
words=line.lower().split() #Divides line into words
6
+
forwordinwords:
7
+
ifwordnotinwordsTallied: #New Word
8
+
iflen(word) >=4: #Adds new word to dictionary if longer than 3 letters
9
+
wordsTallied[word] =1
10
+
else: #Repeated word
11
+
wordsTallied[word] +=1#Updates number of times word appears
12
+
f.closed
13
+
14
+
maxWord=max(wordsTallied, key=wordsTallied.get) #Gets the most lyric word of song
15
+
maxCount=wordsTallied[maxWord] #Gets number of times the lyric appears
16
+
print("\n"+"The most popular lyric is: '"+maxWord+"' \nIt appears "+str(maxCount) +" times in the song" ) #Prints most popular lyric and the number of occurences in the song
0 commit comments