I am performing a sentiment analysis on some reviews and got the following error when running the removeWords
function:
Error in gsub(sprintf("(*UCP)\b(%s)\b", paste(sort(words, decreasing = TRUE), : 'pattern' is invalid UTF-8
Here is the scraping to get the data:
#Link
web <- read_html("https://www.tripadvisor.es/Hotel_Review-g187440-d234268-Reviews-Sol_House_Costa_del_Sol-Torremolinos_Costa_del_Sol_Province_of_Malaga_Andalucia.html")
# Dataset para descargar todos los campos de los comentarios
# 1. Texto comentarios
textoComentario<-web%>%
html_nodes(".location-review-review-list-parts-ExpandableReview__containerStyles--1G0AE > ._2uD5bLZZ .cPQsENeY")%>%
html_text()
textoComentario
# 2. Fecha comentario
fechaComentario<-web%>%
html_nodes(".location-review-review-list-parts-EventDate__event_date--1epHa")%>%
html_text()
fechaComentario <- strsplit(fechaComentario, ": ")
fechaComentario <- unlist(lapply(fechaComentario, FUN = function(x) {x[2]}))
fechaComentario
# Inicializamos un data frame
datos<-data.frame(textoComentario,fechaComentario)
# Nos recorremos todas las páginas
for(i in 1:339){
# 1. Preparamos la url
url<-paste0("https://www.tripadvisor.es/Hotel_Review-g187440-d234268-Reviews-or",i*10,"-Sol_House_Costa_del_Sol-Torremolinos_Costa_del_Sol_Province_of_Malaga_Andalucia.html")
# 2. Descargamos la página
pagina<-read_html(url)
# 3. Descargamos los comentarios de esa página
textoComentario<-pagina%>%
html_nodes(".location-review-review-list-parts-ExpandableReview__containerStyles--1G0AE > ._2uD5bLZZ .cPQsENeY")%>%
html_text()
textoComentario
# 4. Descargamos la fecha de esa página
fechaComentario<-pagina%>%
html_nodes(".location-review-review-list-parts-EventDate__event_date--1epHa")%>%
html_text()
fechaComentario <- strsplit(fechaComentario, ": ")
fechaComentario <- unlist(lapply(fechaComentario, FUN = function(x) {x[2]}))
fechaComentario
#Para que el data.frame tenga el mismo número de filas que de columnas
if (length(textoComentario) < length(fechaComentario)) {textoComentario[length(textoComentario):length(fechaComentario)] <- NA}
if (length(fechaComentario) < length(textoComentario)) {fechaComentario[length(fechaComentario):length(textoComentario)] <- NA}
# 5. Lo juntamos en un dataframe
nuevosDatos<-data.frame(textoComentario,fechaComentario)
# 6. Lo juntamos con el dataframe general
datos<-rbind(datos,nuevosDatos)
print(paste0("Página ",i))
}
#Cambiamos el formato de las fechas
df$fechaComentario=gsub("de","",df$fechaComentario)
# install.packages("readr")
library(readr)
df$fechaComentario=parse_date(df$fechaComentario, " %B %Y",locale=locale("es"))
#usamos la función unique para asegurarnos de que no estemos guardando comentarios repetidos
df<-distinct(df)
#vamos a eliminar los datos con NA
df %>%
as_tibble %>%
filter(is.na(textoComentario) | is.na(fechaComentario))
df<-na.omit(df)
opiniones<-df
colnames(opiniones)[1]<-"review_body"
opiniones$review_body=as.character(opiniones$review_body)
The script for the sentiment analysis is the following:
library(syuzhet) nrc <- get_sentiment(opiniones$review_body, method="nrc",lang="spanish")
# Obtenemos las emociones
emotions <- get_nrc_sentiment(opiniones$review_body,lang="spanish")
emo_bar = colSums(emotions)
emo_sum = data.frame(count=emo_bar, emotion=names(emo_bar))
emo_sum$emotion = factor(emo_sum$emotion, levels=emo_sum$emotion[order(emo_sum$count, decreasing = TRUE)])
# Comparison word cloud
all = c(
paste(opiniones$review_body[emotions$anger > 0], collapse=" "),
paste(opiniones$review_body[emotions$anticipation > 0], collapse=" "),
paste(opiniones$review_bodye[emotions$disgust > 0], collapse=" "),
paste(opiniones$review_body[emotions$fear > 0], collapse=" "),
paste(opiniones$review_body[emotions$joy > 0], collapse=" "),
paste(opiniones$review_body[emotions$sadness > 0], collapse=" "),
paste(opiniones$review_body[emotions$surprise > 0], collapse=" "),
paste(opiniones$review_body[emotions$trust > 0], collapse=" ")
)
all <- removeWords(all, stopwords("spanish"))
I have tried to fixed the error by using inconv(all, "UTF-8")
as well as iconv(x, "UTF-8", "UTF-8",sub='')
, but it keeps giving the same error. Is there a way to correct this?