嗨:我如何获得R,以将“ no”替换为“ not”,而不是将“ not”替换为“ nott”
下面的代码与我当前的字典配合得很好,但与另一本字典将某些标准词替换否定词的字典一起使用时,效果不佳。
#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)
您需要传递一个匹配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"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句