0

I am doing sentiment analysis with german customer reviews and want to implement negation handling. I decided to add the prefix "neg_" both to the word following "not" as well as the word before "not" (this may not make sense for the English language but for German it does).

I already found out how to add the prefix "_neg" to words following a "not" with this function:

addprefix <-function(text){  
  words<-unlist(strsplit(text, " "))
  negative <- grepl("\\<not\\>",words,ignore.case=T)
  negate <- append(FALSE,negative)[1:length(words)]
  words[negate==T]<- paste0("neg_",words[negate==T])
  words<-paste(words,collapse=" ")
}

Is there a possibility to also add the prefix "_neg" also to the word before "not"? So that a review goes from originally this:

> str_negate("I did not like the product")
[1] "I did not like the product"

and currently this:

> str_negate("I did not like the product")
[1] "I did not neg_like the product"

to finally this:

> str_negate("I did not like the product")
[1] "I neg_did not neg_like the product"

Any help would be appreciated. Thank you!

1 Answers1

0

A solution using the index of not with the wich function:

addprefix <-function(text){  
  words<-unlist(strsplit(text, " "))
  negative <- which(grepl("\\<not\\>",words,ignore.case=T))
  to.change = c(negative-1, negative+1)
  to.change = to.change[to.change>0]
  words[to.change] = paste("neg_", words[to.change], sep = '')
  words<-paste(words,collapse=" ")
}

glagla
  • 611
  • 4
  • 9