El análisis de sentimientos, o minería de opinión es utilizado para extraer de forma automática información sobre la conotación positiva o negativa del lenguaje de un documento
El análisis mostrará el resultado en 8 emociones básicas y 2 sentimientos
Emociones Básicas:
1. Alegría (joy)
2. Trsiteza (sadness)
3. Ira (anger)
4. Sorpresa (surprise)
5. Asco (disgust)
6. Miedo (fear)
7. Anticipación (anticipation)
8. Confianza (trust)
Sentimientos
1. Positivo (positive)
2. Negativo (negative)
texto_cadena_miau <- scan(file = "https://raw.githubusercontent.com/programminghistorian/jekyll/gh-pages/assets/galdos_miau.txt", fileEncoding = "UTF-8", what = character(), sep = "\n", allowEscapes = T)
# install.packages("syuzhet")
library(syuzhet)
# install.packages("RColorBrewer")
library(RColorBrewer)
texto_palabras_miau <- get_tokens(texto_cadena_miau)
head(texto_palabras_miau)
## [1] "miau" "por" "b" "pérez" "galdós" "14"
length(texto_palabras_miau)
## [1] 97254
oraciones_vector <- get_sentences(texto_cadena_miau)
length(oraciones_vector)
## [1] 13136
sentimientos_miau <- get_nrc_sentiment(texto_palabras_miau , lang = "spanish")
summary(sentimientos_miau)
## 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
head(sentimientos_miau)
## 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
barplot(
colSums(prop.table(sentimientos_miau[,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 de la Novela Miau",
xlab = "Emociones"
)
palabras_confianza_miau <- texto_palabras_miau[sentimientos_miau$trust >0]
palabras_confianza_miau_orden <- sort(table(unlist(palabras_confianza_miau)), decreasing = TRUE)
head(palabras_confianza_miau_orden, n=10)
##
## dios amigo padre abuelo hecho puerta verdad bueno palabra iglesia
## 142 97 77 69 56 53 51 41 41 37
secuencia_sentimientos_miau <- (sentimientos_miau$negative*-1) + sentimientos_miau$positive
simple_plot(secuencia_sentimientos_miau)
himno <- read.delim("C:\\Users\\HUAWEI\\OneDrive\\Documentos\\himno_nacional.txt")
himno <- as.character(himno)
# texto_cadena <- scan("C:\\Users\\HUAWEI\\OneDrive\\Documentos\\himno_nacional.txt")
# install.packages("syuzhet")
library(syuzhet)
# install.packages("RColorBrewer")
library(RColorBrewer)
texto_palabras <- get_tokens(himno)
class(himno)
## [1] "character"
head(texto_palabras)
## [1] "c" "el" "acero" "aprestad" "y" "el"
length(texto_palabras)
## [1] 502
oraciones_vector <- get_sentences(himno)
length(oraciones_vector)
## [1] 10
sentimientos_df <- get_nrc_sentiment(texto_palabras, lang = "spanish")
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.05578 Mean :0.04183 Mean :0.02191 Mean :0.06773
## 3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.00000
## Max. :3.00000 Max. :2.00000 Max. :2.00000 Max. :2.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.04781 Mean :0.03785 Mean :0.02191 Mean :0.03984
## 3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.00000
## Max. :2.00000 Max. :2.00000 Max. :2.00000 Max. :2.00000
## negative positive
## Min. :0.00000 Min. :0.0000
## 1st Qu.:0.00000 1st Qu.:0.0000
## Median :0.00000 Median :0.0000
## Mean :0.07371 Mean :0.0996
## 3rd Qu.:0.00000 3rd Qu.:0.0000
## Max. :3.00000 Max. :3.0000
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
barplot(
colSums(prop.table(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_miedo <- texto_palabras[sentimientos_df$fear >0]
palabras_miedo_orden <- sort(table(unlist(palabras_miedo)), decreasing = TRUE)
head(palabras_miedo_orden, n=10)
##
## guerra sangre golpe guerrero metralla destino dios enemigo
## 7 4 2 2 2 1 1 1
## impotente muerte
## 1 1
secuencia_sentimientos <- (sentimientos_df$negative*-1) + sentimientos_df$positive
simple_plot(secuencia_sentimientos)
[Link text]https://rpubs.com/valeriapena/1092404
[Link text]https://rpubs.com/valeriapena/1092386