“Long Live Rock’n’Roll”: Getting lyric via python and API
Have you ever wondered how many unique letters in a lyric?
if your answer to the above question YES!!, this writing could be for you.
This work is about calculation of frequency of the unique letters in a lyric using basic python commands. It would be a first step for future project which analyses the rock’n’roll lyric letters (mainly 80’s) by creating database.
You can ask that is it important? In some areas like cryptology, letters and numbers are much more important than what we think. However; the main aim in here is to improve python skills and give useful idea to python users.
Maybe there are encrypted messages in the song you are listening to right now!!! :)
I would want to dedicate this my first public python working and medium article to “Ronnie James DIO” and “Rainbow” which are the one of the best singer and band in the world. Let’s ROCK!
keys:python, API, GENIUS, matplotlib, lyric, lyricsgenius, function.
(to get all codes for this works go to github link)
1-Lyrics from GENIUS (LyricsGenius):
Firstly we use GENIUS database which has the one of the biggest song and artist attribute archive to get song lyric.
step -1 : Before coding, we need API “client acess token” key gettin from GENIUS. Go to this site , sign up firstly if you have not an account. Follow the below guide.
step-2 : install lyricsgenius python library (python 3)
pip install lyricsgenius
for detail info about lyricsgenius : https://pypi.org/project/lyricsgenius/
2- Coding and Create Graphs:
- Import lyricsgenius and matplotlib libaries to get lyrics and create graphs. copy “api client access token” getting from GENIUS and paste between quotation marks into api_key variable.
import matplotlib.pyplot as plt
from lyricsgenius import Geniusapi_key = "api client access token"
- a function named “lyric_letter()” is defined. All codes are located under this function.
def lyric_letter():
"""
this function is calculate number of the unique letter...
- Song name and artist name inputs request you to fill them when the run lyric_letter() function.
- genius variable is assigned to the Genius function which uses the api_key to connect database. “remove_section_headers = True” ignores the some words in the lyric as [Chours], [Bridge]… etc.
- genius.search_song function is used to get the song information. for much more information about Genius library type ?genius.search_song
- song_artist and song_title variables are re-assigned because genius function tryes to correct your inputs even if you don’t write accurately.
- song_before is the variable assigned to the lyrics, including the song name. It is needed to get rid of it for exact calculation. song_before is splited from the first “/n” into two list objects by split function. We have two objects in the list. index[0] is name of song, index[1] is rest of song. song_after is song_before[1].
song_title = input("write song name")
song_artist = input("write artist name")genius = Genius(api_key, remove_section_headers=True, verbose=False)song = genius.search_song(song_title, song_artist)song_artist = song.artist
song_title = song.titlesong_before = song.lyricssong_after = song_before.split("\n", 1)[1]
- All letters turn into uppercase and all unique letters in lyrics are recorded inside counter variable by for loop. unwanted variable is list which is composed of symbols. You can add into this list additional symbols. letter_list is bearing only unique letters without symbols, number_list is bearing only the total number of the each letter.
song_upper = song_after.upper()
counter = ""for letter in song_upper:
if letter not in counter:
counter += letterunwanted = [" ", ",", ".", "'", "!", "#", "$", "\n", "?", "-", "_", "%", "&", "+", "=", "(", ")", "[", "]", '"',
":", "1","2","3","4","5","6","7","8","9","0"]letter_list = []
number_list = []for i in counter:
if i in unwanted:
continue
letter_list.append(i)
number_list.append(song_upper.count(i))
- To sort according to alphapetic or numeric order, the letter and its number need zip and sort function. zipped_letter variable has “letter-number” pairs sorting by alphabetic order. For visualization it is splited into song_keys and song_values list using dictionary methods.
- zipped_number variable has “number-letter” pairs sorting by number order . It was interesting that when i applied dictionary methods on zipped_number like zipped_letter, key and values pairs didn’t get “j” and “r” letters. I dont know why!. Therefore, I used list methods to record “number-letter” pairs separetly into song_number and song_letter lists, instead of dictionary methods.
zipped_letter = list(zip(letter_list, number_list))
zipped_letter.sort()
new_dict = dict(zipped_letter)song_keys = list(new_dict.keys())
song_values = list(new_dict.values())
# ##########################################################
zipped_number = list(zip(number_list, letter_list))
zipped_number.sort()
song_number = []
song_letter = []
for i in range(len(zipped_number)):
song_number.append(zipped_number[i][0])
song_letter.append(zipped_number[i][1])
- plt.style.use(“write plot style “) is used for base graph visulatization. Three graphs are created using matplotlib library. first one (barh) is bar plot showing decreasing letter count values from top to bottom. Second graphs is showing overlapped scatter and line plots of letters ordering in “A” to “Z” letters. The last graph showes frequency of letter with their exact value annotation on the graph ordered by letters.
- Set label part is about adding x-y axis labels, graph titles, annotations and setting font styles. For more info about matplotlib labels go to link
###############################################################
# CREATE GRAPHS
# you can check plot styles;
# plt.style.available[:]plt.style.use('seaborn')fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(20, 20))ax[0].barh(song_letter, song_number);
ax[1].scatter(song_keys, song_values, color="g");
ax[1].plot(song_keys, song_values);
ax[2].stem(song_keys, song_values, use_line_collection=True);# SET LABELS:font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}ax[0].set_title("Number of the Letters: "+song_title.upper() +" "+"("+song_artist+")", fontdict = font1)ax[0].set_xlabel("Frequency", fontdict = font2)
ax[0].set_ylabel("Letters", fontdict = font2)ax[1].set_xlabel("Letters", fontdict = font2)
ax[1].set_ylabel("Frequency", fontdict = font2)ax[2].set_xlabel("Letters", fontdict = font2)
ax[2].set_ylabel("Frequency", fontdict = font2)# add annotation, the frequency number of the each letter
for ii, txt in enumerate(song_values):
ax[2].annotate(txt, (song_keys[ii], song_values[ii]))fig.tight_layout()
plt.show()
- Print function shows some statical values as seen below. If you want, you can add new statical calculations. Song’s title, artist, and lyric are able to use out of the function by return function.
print("\n" + "(Letter,Frequency): " + str(zipped_letter) + "\n")
print("The total number of all letters: " + str(sum(song_values)) + "\n")
print("The total number of different letters: " + str(len(song_keys)) + "\n")# print(10*"*"+ str(song_before.split("\n", 1)[0]) + 10*"*")
# print(song_after)
return song_before, zipped_letter, sum(song_values), len(song_keys)
3- Let’s RUN!!
to run the function, type one of the below commands :
### if you dont want to get automatic print out on the screen uncomment below commad
# song = lyric_letter()### if you want to ouput with return uncomment below commad# lyric_letter()
### to assign variables for output
# song_before, zipped_leter, total_song_values, total_song_keys = lyric_letter()
If you notice that i didn’t write song name accurately, but the genius library corrected it. You can see corrected song and artist name in the title of graphs.Please don’t use weird names, it cannot fix and gives error :)
4- RESULTS
The results of LONG LIVE ROCK ’N’ ROLL. The song artist is Rainbow.
Thank you for your patient and reading…
I would like to thank Mustafa Vahit Keskin, Mehmet Akturk, Ozan Güner, Mehmet Tuzcu, Arif Eker who encouraged me to write this article and did not spare her teachings, and my DSMLBC-8 group friends who are from Veri Bilim Okulu and Miuul.
Contact:
Hakan SARITAŞ
linkedin : www.linkedin.com/in/hakansaritas
GitHub: hakansaritas (HAKAN SARITAŞ) · GitHub
kaggle: Hakan Saritas
**********Long Live Rock ’n’ Roll Lyrics**********
At the end of a dream If you know where I mean
When the mist just starts to clear In a similar way
At the end of today I could feel the sound of writing on the wall
It cries for you It’s the least that you can do
Like a spiral on the wine I can hear it screamin’ in my mind
Long live rock and roll
Long live rock ’n’ roll
Long live rock and roll
Let it live…..
References: