0

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?

Maria
  • 37
  • 4
  • I think you'll need to give us a reproducible example to get help. From a quick look, that means supplying `opiniones`: but if it's big, make up a smaller example that is big enough to still reproduce the error. See https://stackoverflow.com/a/5963610/2554330 for more advice. – user2554330 May 30 '20 at 19:22
  • Thanks! I have edited my question with the code to get opiniones. – Maria May 30 '20 at 20:41

0 Answers0