---
title: Song Analysis Psuedocode
tags: Projects, Project 1, 2020
---
# Song Analysis Pseudocode
## Pseudocode Details
Please refer to these hints when attempting to implement the functions compute_idf, compute_tf, compute_tf_idf, compute_corpus_tf_idf, and nearest_neighbor. The idea behind having you think about implementing these functions on your own first is to deeper your understanding of how tf, idf, and nearest neighbor work in unison in order to create a tool that can compare songs by genre. Thinking about how to implement a function based on an algorithm is an important skill that you will continue to build as you continue on your journey as an ethical computer scientist!
### Pseudocode: compute_tf
In order to calculate the Term Frequency,
Loop through each word of a song
if the word has not shown up before
add word to the data structure storing
the song's words and their tf values
Increment the word occurence by one
return the data structure storing the song's term frequencies
### Pseudocode: compute_idf
In order to calculate the Inverse Document Frequency for each song in your corpus,
create a df data structure
Loop through every song in the corpus
Get the lyrics for the song
for every word in the song
If the word has not been seen before
store a new set for the word
in your df data structure
If the song has not exist in the set
add the song to the set
For every word in your df data structure
calculate the idf for the word
and store it in your idf data structure
### Pseudocode: compute_tf_idf
In order to calculate the Term Frequency - Inverse Document Frequency,
Compute the term frequency of a song
For each word in that song, multiply by the idf
Store the result where each word has its tf-idf value
### Pseudocode: nearest_neighbor
In order to calculate the nearest neighbor of a novel song,
Keep track of the highest similarity and best song
Verify new song lyrics contain only valid characters
Calculate the tf-idf for the song
Loop through the corpus
If the similarity of a song is higher than the current highest,
update similarity and song
return the best song
### Testing accuracy
In order to check the accuracy of the program, we have provided some test queries and their results. Here are some lyrics to try to compare your classifier to ours (check the title to see if it matches!):
small_songdata:
> - "Everything you own in the box to the left": back-in-the-box
> - "I want no other love, I wish on stars above": i-wish-you-well
> - "whos that girl in the piano bar": piano-bar
> - "Follow your kind star spangled heir": the-family-album
> - "its provacative, it gets the people going": high-addal-vs-mida
> - "nothing on you baby": nothing
> - "If i was your boyfriend id never let you go": gotta-boyfriend
> - "somebody that I used to know": somebody-that-i-used-to-know
full_songdata:
> - "Everything you own in the box to the left": the-box
> - "I want no other love, I wish on stars above": most-of-all
> - "whos that girl in the piano bar": art-of-the-blacksmith
> - "Follow your kind star spangled heir": how-soon-is-now
> - "its provacative, it gets the people going": when-the-going-gets-tough
> - "nothing on you baby": nothing
> - "If i was your boyfriend id never let you go": pump-ya-man
> - "somebody that I used to know": somebody-that-i-used-to-know