Example usage
Here we are showing an example of how to use pylyrics2 to extract and analyze lyrics. With pylrics you can download songs attribute datasets from Kaggle, extract lyrics and generate a word cloud.
Imports
from pylyrics2 import extract_lyrics as el
from pylyrics2 import plot_cloud as pc
from pylyrics2 import download_data as dd
from pylyrics2 import clean_text as ct
Downloading the data
In this step, we are downloading data from Kaggle, and extract the given columns from csv file and finally create a dataframe, for further analysis.
dd.download_data("geomack/spotifyclassification", "data/spotify_attributes", ["song_title", "artist"])
| song_title | artist | |
|---|---|---|
| 0 | Mask Off | Future |
| 1 | Redbone | Childish Gambino |
| 2 | Xanny Family | Future |
| 3 | Master Of None | Beach House |
| 4 | Parallel Lines | Junior Boys |
| ... | ... | ... |
| 2012 | Like A Bitch - Kill The Noise Remix | Kill The Noise |
| 2013 | Candy | Dillon Francis |
| 2014 | Habit - Dack Janiels & Wenzday Remix | Rain Man |
| 2015 | First Contact | Twin Moons |
| 2016 | I Wanna Get Better | Bleachers |
2017 rows × 2 columns
Extracting a lyrics
The next step is to extract the lyrics, the use inputs the song and the name of the artist and the function extract lyrics from API and saves it as a dataframe.
lyrics = el.extract_lyrics("22", "Taylor Swift")
lyrics
"[Verse 1]It feels like a perfect night To dress up like hipsters And make fun of our exes, uh-uh, uh-uh It feels like a perfect night For breakfast at midnight To fall in love with strangers, uh-uh, uh-uh[Pre-Chorus]Yeah We're happy, free, confused, and lonely at the same time It's miserable and magical, oh, yeah Tonight's the night when we forget about the deadlines It's time, oh-oh[Chorus]I don't know about you, but I'm feeling 2 2 Everything will be alright if you keep me next to you You don't know about me, but I'll bet you want to Everything will be alright if we just keep dancing like we're 22, 2 2[Verse 2]It seems like one of those nights This place is too crowded Too many cool kids, uh-uh, uh-uh(Who's Taylor Swift, anyway? Ew)It seems like one of those nights We ditch the whole scene And end up dreaming Instead of sleeping[Pre-Chorus]Yeah We're happy, free, confused, and lonely in the best way It's miserable and magical, oh, yeah Tonight's the night when we forget about the heartbreaks It's time, oh-oh[Chorus](Hey!)I don't know about you, but I'm feeling 2 2 Everything will be alright (Ooh) if you keep me next to you You don't know about me, but I'll bet you want to Everything will be alright if (Alright)We just keep dancing like we're 2 2 (Oh, oh, oh, oh, oh)2 2 (I don't know about you)2 2, 2 2[Bridge]It feels like one of those nights We ditch the whole scene It feels like one of those nights We won't be sleeping It feels like one of those nights You look like bad news I gotta have you I gotta have you Ooh, ooh, yeah[Chorus](Hey!)I don't know about you, but I'm feeling 2 2 Everything will be alright if (Ooh) you keep me next to you You don't know about me, but I'll bet you want to Everything will be alright if we just keep dancing like we're 22 (Whoa, oh)2 2 (Dancing like)2 2 (Yeah, yeah), 2 2, (Yeah, yeah, yeah)[Outro]It feels like one of those nights We ditch the whole scene It feels like one of those nights We won't be sleeping It feels like one of those nights You look like bad news I gotta have you I gotta have you"
Cleaning the text
The third step is to clean the lyrics. This step removes any special characters, html tags, #tags or contraction words that might be present in the lyrics. The final output is a clean paragraph in lower case letters.
ct.clean_text(lyrics)
'verse it feels like a perfect night to dress up like hipsters and make fun of our exes uhuh uhuh it feels like a perfect night for breakfast at midnight to fall in love with strangers uhuh uhuhprechorusyeah we are happy free confused and lonely at the same time it is miserable and magical oh yeah tonight is the night when we forget about the deadlines it is time ohohchorusi do not know about you but i am feeling everything will be alright if you keep me next to you you do not know about me but i will bet you want to everything will be alright if we just keep dancing like we are verse it seems like one of those nights this place is too crowded too many cool kids uhuh uhuhwho is taylor swift anyway ewit seems like one of those nights we ditch the whole scene and end up dreaming instead of sleepingprechorusyeah we are happy free confused and lonely in the best way it is miserable and magical oh yeah tonight is the night when we forget about the heartbreaks it is time ohohchorusheyi do not know about you but i am feeling everything will be alright ooh if you keep me next to you you do not know about me but i will bet you want to everything will be alright if alrightwe just keep dancing like we are oh oh oh oh oh i do not know about you bridgeit feels like one of those nights we ditch the whole scene it feels like one of those nights we wo not be sleeping it feels like one of those nights you look like bad news i gotta have you i gotta have you ooh ooh yeahchorusheyi do not know about you but i am feeling everything will be alright if ooh you keep me next to you you do not know about me but i will bet you want to everything will be alright if we just keep dancing like we are whoa oh dancing like yeah yeah yeah yeah yeahoutroit feels like one of those nights we ditch the whole scene it feels like one of those nights we wo not be sleeping it feels like one of those nights you look like bad news i gotta have you i gotta have you'
Making the wordcloud
In the last and final step the function takes the “name of the artist” and “song” then downloads the lyrics (or a group of lyrics), cleans the text and then creates a “word cloud” of the most occurring words in the lyrics.
song = { "Taylor Swift": "22"}
file_path = "data/wordcloud"
pc.plot_cloud(song, file_path, max_font_size=30, max_words=100, background_color='black')