1 Goal


The goal of this tutorial is to predict the lyrics of the song calypso by Luis Fonsi.


2 Loading the libraries


library(caret)

3 Data import


# We have created a cleaned version of the lyrics
# All the lyrics have been changed to lower case
# And the last word is the variable to predict
calypso_lyrics <- read.csv("my_lyrics.csv", header = T, 
                    sep = ",", 
                    stringsAsFactors = FALSE)

head(calypso_lyrics)
##                            Ivar predictme
## 1      yo no se no se no se que    pasara
## 2           tu cuerpo frente al       mar
## 3           mezclando arena con       sal
## 4 pero se yo se yo se que no es    normal
## 5                  lo que puede     pasar
## 6                si tu me dejas    entrar

4 Training our model


# In this case we are going to use a neural network
my_model <- train(predictme ~ Ivar, data = calypso_lyrics, method = "nnet")

5 Creating a function to predict the lyrics


# This function changes the characters to lowercase
# then creates a dataframe to be predicted by the model
# And returns the last word of the lyrics

predict_calypso <- function(x){
  set.seed(235)
  tabletopredict <- data.frame(tolower(x))
  names(tabletopredict) <- "Ivar"
  return(as.character(predict(my_model, newdata = tabletopredict)))
  
}

6 Predicting the lyrics


# We use this function to predict the lyrics

predict_calypso("calienta tanto que")
## [1] "asusta"
predict_calypso("y sabes lo que te")
## [1] "gusta"
predict_calypso("Un dos tres")
## [1] "calypso"
predict_calypso("Un deux trois")
## [1] "calypso"
predict_calypso("One two three")
## [1] "calypso"

7 Conclusion


We have learnt how to use a model to predict the lyrics of a summer song. Remember that the most important part of this exercise is to choose the proper training set.