library(syuzhet)
library(RColorBrewer)
#install.packages("tm")
library(tm)
## Loading required package: NLP
himno <- scan(file = "/Users/alejandromontano/Desktop/letra-oficial-del-himno-nacional-mexicano2.txt", fileEncoding = "UTF-8", what = character(), sep = "\n", allowEscapes = T)
# Tokenizar texto (dividir un texto en palabras)
texto_palabras <- get_tokens(himno)
head(texto_palabras)
## [1] "letra"    "oficial"  "del"      "himno"    "nacional" "mexicano"
length(texto_palabras)
## [1] 260
oraciones_vector <- get_sentences(himno)
length(oraciones_vector)
## [1] 57
# Realizar análisis de sentimientos 
himno_sentimientos_df <- get_nrc_sentiment(texto_palabras, lang = "spanish")

summary(himno_sentimientos_df)
##      anger          anticipation        disgust             fear        
##  Min.   :0.00000   Min.   :0.00000   Min.   :0.00000   Min.   :0.00000  
##  1st Qu.:0.00000   1st Qu.:0.00000   1st Qu.:0.00000   1st Qu.:0.00000  
##  Median :0.00000   Median :0.00000   Median :0.00000   Median :0.00000  
##  Mean   :0.06538   Mean   :0.03077   Mean   :0.01923   Mean   :0.08846  
##  3rd Qu.:0.00000   3rd Qu.:0.00000   3rd Qu.:0.00000   3rd Qu.:0.00000  
##  Max.   :2.00000   Max.   :2.00000   Max.   :1.00000   Max.   :2.00000  
##       joy          sadness           surprise           trust        
##  Min.   :0.00   Min.   :0.00000   Min.   :0.00000   Min.   :0.00000  
##  1st Qu.:0.00   1st Qu.:0.00000   1st Qu.:0.00000   1st Qu.:0.00000  
##  Median :0.00   Median :0.00000   Median :0.00000   Median :0.00000  
##  Mean   :0.05   Mean   :0.05769   Mean   :0.02308   Mean   :0.06538  
##  3rd Qu.:0.00   3rd Qu.:0.00000   3rd Qu.:0.00000   3rd Qu.:0.00000  
##  Max.   :2.00   Max.   :1.00000   Max.   :2.00000   Max.   :3.00000  
##     negative         positive     
##  Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.:0.0000  
##  Median :0.0000   Median :0.0000  
##  Mean   :0.1038   Mean   :0.1154  
##  3rd Qu.:0.0000   3rd Qu.:0.0000  
##  Max.   :2.0000   Max.   :2.0000
head(himno_sentimientos_df)
##   anger anticipation disgust fear joy sadness surprise trust negative positive
## 1     0            0       0    0   0       0        0     0        0        0
## 2     0            0       0    0   0       0        0     3        0        1
## 3     0            0       0    0   0       0        0     0        0        0
## 4     0            1       0    0   1       1        0     1        0        1
## 5     0            0       0    0   0       0        0     0        0        0
## 6     0            0       0    0   0       0        0     0        0        0
#Graficar Res
barplot(
  colSums(prop.table(himno_sentimientos_df[,1:8])),
  space = 0.2,
  horiz = FALSE,
  las = 1,
  cex.names = 0.7,
  col = brewer.pal(n = 8, name = "Set3"),
  main = "Análisis de Sentimientos del Himno Nacional Mexicano",
  xlab = "emociones"
)

#Profundizar análisis
palabras_confianza <- texto_palabras[himno_sentimientos_df$trust >0]
palabras_confianza_orden <- sort(table(unlist(palabras_confianza)), decreasing = TRUE)
head(palabras_confianza_orden, n=10)
## 
##     coro     dios   gloria    himno    honor libertad  oficial      paz 
##        2        1        1        1        1        1        1        1 
##      pie    suelo 
##        1        1
#Graficar resultados de sentimientos 
secuencia_sentimientos <- (himno_sentimientos_df$negative*-1)-himno_sentimientos_df$positive
simple_plot(secuencia_sentimientos)