2016-05-16 62 views
0

你好:我如何获得R键来替换“无”与“不是”,但不能代替“不”与“诺特”匹配的原话与stri_replace_all_fixed

下面的代码工作得很好,我现在的词典,但不与另一个字典,用一些标准化的词取代否定词。

#patterns 
replace<-('no') 
#replacements 
with<-c('not') 
#data frame 
neg<-data.frame(replace=replace, with=with) 
#text to modify 
out<-c('not acceptable no good') 
#current code 
stri_replace_all_fixed(out, neg$replace, neg$with, vectorize_all=FALSE) 

回答

3

你需要传递一个正则表达式将匹配no作为一个整体词:

> replace<-('\\bno\\b') ## <= \b is a word boundary 
> with<-c('not') 
> neg<-data.frame(replace=replace, with=with) 
> out<-c('not acceptable no good') 
> stri_replace_all_regex(out, neg$replace, neg$with, vectorize_all=FALSE) 
[1] "not acceptable not good" 
+0

我需要担心在文本周围“不”的空白?或者这个词边界帐户呢? – spindoctor

+0

字边界是不消耗文本的零宽度断言,不应该有空白问题。 –