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!