Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在否定词中添加标记直到R中的下一个标点符号_R_Sentiment Analysis_Negation - Fatal编程技术网

如何在否定词中添加标记直到R中的下一个标点符号

如何在否定词中添加标记直到R中的下一个标点符号,r,sentiment-analysis,negation,R,Sentiment Analysis,Negation,我需要一些帮助来弄清楚我们如何在R中模拟在否定词后面的每个单词直到下一个标点符号前都添加一个标记“NOT_uu”的解决方案 library(gsubfn) str_negate <- function(x) { x1 <- gsub("(not|n't|never|without|unlikely to) (\\w+)", '\\1 NOT_\\2', x) x2 <- gsubfn('NOT_([^[:punct:]]+)', ~ gsub('(\\w+)', '

我需要一些帮助来弄清楚我们如何在R中模拟在否定词后面的每个单词直到下一个标点符号前都添加一个标记“NOT_uu”的解决方案

library(gsubfn)
str_negate <- function(x) {
   x1 <- gsub("(not|n't|never|without|unlikely to) (\\w+)", '\\1 NOT_\\2', x)
   x2 <- gsubfn('NOT_([^[:punct:]]+)', ~ gsub('(\\w+)', 'NOT_\\1', x), x1)
   x2
}
x <- "It was never going to work, he thought. He did not play so well, so he had to practice some more."
str_negate(x)
## [1] "It was never NOT_going NOT_to NOT_work, he thought. He did not NOT_play NOT_so NOT_well, so he had to practice some more."
可以在这里找到Python代码的解决方案

我有下面的解决方案,在否定词后面的下一个词后面加上“NOT_uu”标签:NOT,never,NOT,without,looke to

str_negate <- function(x) {
  gsub("not ","not NOT_",
            gsub("n't ","n't NOT_",
            gsub("never ","never NOT_",
            gsub("without ","without NOT_",
            gsub("unlikely to ","unlikely to NOT_",x)))))
}

str_negate(FeedbackCommentsVectorProc$Sentences)
str\u否定编辑
在试图弄明白这一点之后,这是我能想到的最简单的解决方案注意:如果字符串的标点符号前面有多个否定词,则此操作将失败

library(gsubfn)
str_negate <- function(x) {
   x1 <- gsub("(not|n't|never|without|unlikely to) (\\w+)", '\\1 NOT_\\2', x)
   x2 <- gsubfn('NOT_([^[:punct:]]+)', ~ gsub('(\\w+)', 'NOT_\\1', x), x1)
   x2
}
x <- "It was never going to work, he thought. He did not play so well, so he had to practice some more."
str_negate(x)
## [1] "It was never NOT_going NOT_to NOT_work, he thought. He did not NOT_play NOT_so NOT_well, so he had to practice some more."
库(gsubfn)

谢谢你的帮助!我需要在句子中的下一个标点符号之前,在后面的所有单词中添加not、never等。例如,对于输入:“它永远不会起作用,他想。他打得不好,所以他必须多练习一些。”我需要以下输出:“它永远不会不工作,他想。他没有打得不好,所以他必须多练习一些。”。您的解决方案只是将标记添加到下一个单词,而不是所有单词。您在上面的解决方案中使用的正则表达式\\K是什么意思?我到处都找不到它的意思。。我正试图使你的代码适应我的需要。谢谢您认为从您在这里提供的列表中:是否有任何正则表达式可以满足我的需要?非常感谢!