#Text Mining Fundamentals

Sys.setenv(LANG = "en")
# Load libraries
library("tm") 
## Loading required package: NLP
library("SnowballC")
library("wordcloud")
## Loading required package: RColorBrewer
library("RColorBrewer")

Base de datos

Esta base de dtos elegida es una recopilación de las canciones de Drake. En esta estan los lyrics de todas sus canciones por lo que vamos a poder ser capaces de analizar cuáles son las palabras que más usa en sus canciones y poder ayudarle a otros raperos a que puedan descubrir un poco de lo que usa Drake para el éxito de sus canciones.

#Let's start by importing the text to analyze
text <- readLines(("/Users/fernandocardelasperez/Desktop/Mineria de datos/Drake_lyrics.txt"), warn=FALSE, encoding = "UTF-8")
tex <- readLines(("/Users/fernandocardelasperez/Desktop/Mineria de datos/Drake_lyrics.txt"), warn=FALSE)
text <- iconv(tex,"WINDOWS-1252","UTF-8")
#We need to convert the text to a corpus

docs <- Corpus(VectorSource(text))
#inspect(docs)
#General text cleaning

# Convert the text to lower case
docs <- tm_map(docs, content_transformer(tolower))
## Warning in tm_map.SimpleCorpus(docs, content_transformer(tolower)):
## transformation drops documents
# Remove numbers
docs <- tm_map(docs, removeNumbers)
## Warning in tm_map.SimpleCorpus(docs, removeNumbers): transformation drops
## documents
# Remove english common stopwords
docs <- tm_map(docs, removeWords, stopwords("english"))
## Warning in tm_map.SimpleCorpus(docs, removeWords, stopwords("english")):
## transformation drops documents
# Remove your own stop word
# specify your stopwords as a character vector
docs <- tm_map(docs, removeWords, c("blabla1", "blabla2")) 
## Warning in tm_map.SimpleCorpus(docs, removeWords, c("blabla1", "blabla2")):
## transformation drops documents
# Remove punctuations
docs <- tm_map(docs, removePunctuation)
## Warning in tm_map.SimpleCorpus(docs, removePunctuation): transformation drops
## documents
# Eliminate extra white spaces
docs <- tm_map(docs, stripWhitespace) 
## Warning in tm_map.SimpleCorpus(docs, stripWhitespace): transformation drops
## documents
#Term-document matrix. Document matrix is a table containing the frequency of the words.

dtm <- TermDocumentMatrix(docs)
m <- as.matrix(dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
head(d, 100)
##                  word freq
## know             know  338
## like             like  330
## just             just  261
## got               got  220
## yeah             yeah  217
## shit             shit  208
## now               now  176
## nigga           nigga  163
## back             back  129
## niggas         niggas  128
## get               get  127
## love             love  122
## never           never  117
## fuck             fuck  107
## one               one  105
## say               say  104
## girl             girl  102
## time             time  102
## way               way   97
## need             need   94
## make             make   93
## cause           cause   93
## man               man   83
## take             take   82
## aint             aint   80
## tell             tell   78
## real             real   78
## things         things   76
## still           still   76
## wanna           wanna   73
## right           right   72
## much             much   67
## gotta           gotta   66
## want             want   66
## see               see   65
## can               can   65
## think           think   63
## even             even   62
## feel             feel   59
## look             look   59
## come             come   58
## money           money   57
## give             give   54
## said             said   53
## good             good   52
## guess           guess   50
## really         really   49
## bout             bout   49
## let               let   47
## thing           thing   45
## care             care   44
## started       started   43
## everything everything   42
## boy               boy   42
## new               new   42
## whole           whole   41
## i’m           i’m   40
## tryna           tryna   39
## lot               lot   39
## used             used   39
## call             call   37
## night           night   37
## life             life   37
## every           every   37
## last             last   37
## done             done   37
## going           going   36
## people         people   36
## name             name   36
## city             city   34
## made             made   34
## keep             keep   33
## god               god   33
## always         always   33
## around         around   33
## fuckin         fuckin   33
## dedicate     dedicate   33
## hold             hold   32
## told             told   32
## home             home   31
## baby             baby   31
## live             live   31
## talk             talk   31
## days             days   30
## stay             stay   30
## play             play   29
## long             long   29
## talkin         talkin   29
## hit               hit   29
## free             free   29
## bottom         bottom   29
## gon               gon   28
## someone       someone   28
## friends       friends   28
## face             face   28
## day               day   28
## looking       looking   28
## different   different   27
## girls           girls   27
## put               put   27
# vamos a visualizar como se verían las palabras más ocupadas de Drake. Para esto vamos a agarrar una muestra de 190 palabras que se repitan de forma aleatoria.
set.seed(1234)
wordcloud(words = d$word, freq = d$freq, min.freq = 5,
          max.words=190, random.order=T, rot.per=0.5, 
          colors=brewer.pal(8, "Dark2"))

findFreqTerms(dtm, lowfreq = 20)
##   [1] "wishin"     "yeah"       "keep"       "tryna"      "know"      
##   [6] "like"       "die"        "wanna"      "see"        "things"    
##  [11] "got"        "different"  "gon"        "shit"       "tell"      
##  [16] "back"       "hold"       "feel"       "good"       "god"       
##  [21] "might"      "hard"       "make"       "sure"       "bad"       
##  [26] "still"      "lot"        "love"       "said"       "even"      
##  [31] "never"      "someone"    "call"       "used"       "phone"     
##  [36] "late"       "need"       "night"      "can"        "one"       
##  [41] "thing"      "city"       "ever"       "left"       "now"       
##  [46] "girl"       "started"    "girls"      "get"        "just"      
##  [51] "going"      "days"       "nobody"     "always"     "home"      
##  [56] "stay"       "right"      "baby"       "way"        "play"      
##  [61] "much"       "time"       "friends"    "gotta"      "long"      
##  [66] "cause"      "take"       "everything" "want"       "around"    
##  [71] "loved"      "something"  "think"      "man"        "money"     
##  [76] "live"       "real"       "aint"       "niggas"     "guess"     
##  [81] "nigga"      "put"        "talkin"     "fuck"       "woah"      
##  [86] "boy"        "life"       "look"       "say"        "fuckin"    
##  [91] "made"       "really"     "give"       "told"       "bitches"   
##  [96] "hit"        "hear"       "better"     "every"      "let"       
## [101] "come"       "free"       "came"       "bitch"      "heard"     
## [106] "new"        "damn"       "i’m"      "last"       "done"      
## [111] "whole"      "old"        "smoke"      "trust"      "people"    
## [116] "fake"       "face"       "day"        "skrrt"      "name"      
## [121] "bout"       "care"       "looking"    "remember"   "talk"      
## [126] "bottom"     "team"       "though"     "hell"       "low"       
## [131] "cry"        "dedicate"   "thru"       "motion"
#Vamos a busacar palabras que se asocien con la palabra now
findAssocs(dtm, terms = "now", corlimit = 0.2)
## $now
##  bottom started   stuck   right 
##    0.37    0.31    0.23    0.21
barplot(d[1:10,]$freq, las = 2, names.arg = d[1:10,]$word,
        col ="lightblue", main ="Most frequent words",
        ylab = "Word frequencies")

Interpretación de Resultados

Dentro de la prueba se puede ver que palabras como know, now, like, got, n words, son las más utilizadas dentro de las letras de las canciones de Drake. Si nosotros fueramos reperos o compositores de canciones dentro de este género musical, nos sería de mucha utilidad saber esto a la hora de poder generar las rimas de las canciones.