Processing negation in R, how can I replace a word after negation in R?

I am doing a sentiment analysis for financial articles. To increase the accuracy of my naive Bayes classifier, I would like to implement negation processing.

In particular, I want to add the prefix "not_" to the word "no" or "not"

So if in my case something like this:

 x <- "They didn't sell the company." 

I want to get the following:

"they didn't not_sell the company."

(the stopwatch has not been deleted later)

I could only find the function gsub(), but it does not seem to work for this task.

Any help would be appreciated! Thank!

+1
source share
1 answer

In particular, I want to add the prefix "not_" to the word next to "not" or "not"

str_negate <- function(x) {
  gsub("not ","not not_",gsub("n't ","n't not_",x))
}

, strsplit:

str_negate <- function(x) {
  str_split <- unlist(strsplit(x=x, split=" "))
  is_negative <- grepl("not|n't",str_split,ignore.case=T)
  negate_me <- append(FALSE,is_negative)[1:length(str_split)]
  str_split[negate_me==T]<- paste0("not_",str_split[negate_me==T])
  paste(str_split,collapse=" ")
}

:

> str_negate("They didn't sell the company")
[1] "They didn't not_sell the company"
> str_negate("They did not sell the company")
[1] "They did not not_sell the company"
+1

Source: https://habr.com/ru/post/1690627/


All Articles