【发布时间】:2016-10-28 17:04:32
【问题描述】:
我正在研究一种原始语音分析算法。现在我想改进它如何处理正面/负面陈述的否定。目前,我仅在否定直接发生时才添加字符串“NOT_”:
s_commentsOut$gsubContent <- gsub("not ","not NOT_",gsub("n't ","n't NOT_",s_commentsOut$lowCo))
例如
"This is not good"
变成
"This is not NOT_good"
现在我想实现当目标词向量和否定之间有n个字符时也添加“NOT_”,例如:
targetList <- c("nice", "perfect", "good", "love")
现在借助上面的列表,下面的字符串:
"This isn't a very good way"
应该变成
"This isn't a very NOT_good way"
仅当否定出现在目标之前的n(例如15)字符时,才应进行此替换,例如以下不应该转换(因为目标和否定之间的距离是> 15):
"This is not going to work. However you did this very nicely."
我发现了以下 SO 文章: Negation of several characters before pattern
How to replace a character in a string but only if it occurs within a delimited substring?
但我很难做到正确。与此同时,我帮助自己从文本中删除诸如“like”、“an”、“a”之类的字符串......
进一步的测试短语:
"Nottingham is the love of my life."
"This is good. Nottingham is a town."
"This is not very good"
"This is not good. This is not good. This is not very good. This is nice. This very nice. This is not very nice."
【问题讨论】:
-
第n个字符的东西是从哪里来的?
ifelse(grepl('not|n\'t', x), gsub(sprintf("(?=%s)", paste(targetList, collapse = '|')), "NOT_", x, perl = TRUE), x) -
谢谢@rawr - 我编辑了上面的帖子以使其更清晰。