Análisis de Sentimientos

El análisis de sentimientos, o minería de opinión, es utilizado para extraer

de forma automática, información sobre la connotación positiva o negativa

del lenguaje de un documento.

El análisis mostrará el resultado e 8 emociones básicas y 2 sentimientos:

#Emociones Básicas: # 1. Alegría (Joy) # 2. Tristeza (Sadness) # 3. Ira (Anger) # 4. Sorpresa (Surprise) # 5. Asco (Disgust) # 6. Miedo (Fear) # 7. Anticipación ()

Cargar librerías y cargar texto

#install.packages("syuzhet")
library(syuzhet)

texto_cadena <- scan(file = "https://raw.githubusercontent.com/programminghistorian/jekyll/gh-pages/assets/galdos_miau.txt", fileEncoding = "UTF-8", what = character(), sep = "\n", allowEscapes = T)

Cargar texto

texto_palabras <- get_tokens(texto_cadena)
head(texto_palabras)
## [1] "miau"   "por"    "b"      "pérez"  "galdós" "14"
length(texto_palabras)
## [1] 97254
oraciones_vector <- get_sentences(texto_cadena)
length(oraciones_vector)
## [1] 13136

Realizar análisis de sentimientos - A partir del punto anterior no tenía el código, así que realicé algo muy básico que logré hacer

sentimientos_df <- get_nrc_sentiment(texto_palabras, lang="spanish")
# Idiomas disponibles: english, french, german, italing, portuguese, spanish, etc
head(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     0        0        0
## 3     0            0       0    0   0       0        0     0        0        0
## 4     0            0       0    0   0       0        0     0        0        0
## 5     0            0       0    0   0       0        0     0        0        0
## 6     0            0       0    0   0       0        0     0        0        0
summary(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.01596   Mean   :0.02114   Mean   :0.01263   Mean   :0.02243  
##  3rd Qu.:0.00000   3rd Qu.:0.00000   3rd Qu.:0.00000   3rd Qu.:0.00000  
##  Max.   :5.00000   Max.   :3.00000   Max.   :6.00000   Max.   :5.00000  
##       joy             sadness           surprise           trust        
##  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.01929   Mean   :0.02564   Mean   :0.01035   Mean   :0.03004  
##  3rd Qu.:0.00000   3rd Qu.:0.00000   3rd Qu.:0.00000   3rd Qu.:0.00000  
##  Max.   :5.00000   Max.   :7.00000   Max.   :2.00000   Max.   :3.00000  
##     negative          positive      
##  Min.   :0.00000   Min.   :0.00000  
##  1st Qu.:0.00000   1st Qu.:0.00000  
##  Median :0.00000   Median :0.00000  
##  Mean   :0.04658   Mean   :0.05153  
##  3rd Qu.:0.00000   3rd Qu.:0.00000  
##  Max.   :7.00000   Max.   :5.00000
barplot(colSums(prop.table(sentimientos_df[, 1:8])))