仅在否定单词后否定POS标记的句子

时间:2012-05-14 14:48:26

标签: php regex

考虑以下字符串:

I am mad and will not ever set foot in that store again

我正在使用POS-tagger标记字符串,如下所示:

I/NN am/VBP mad/JJ and/CC will/MD not/RB ever/RB set/VBN foot/NN in/IN that/IN 
store/NN again/RB 

现在,我正在使用正则表达式来连接“不”,a.o.,动词,而忽略了否定词(从不,也没有,等等)

preg_replace(
  "/(\s)(?:(?!never|neither|dont|wont|not|no)(\w*))\/(JJ|MD|RB|VB|VBG|VBN)\b/", 
  "$1not$2",
  $sentence
);

这导致:

I am notmad and notwill notever notset foot in that store notagain 

然而,我想要的只是将“不”连接到出现 AFTER (第一个)否定词的动词。请注意madwill,而不是notmadnotwill

I am mad and will notever notset foot in that store notagain 

所以我想首先我应该在句子中寻找任何否定词(从不|不|不|不|不|),并且只从那里执行正则表达式。但是我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

最简单的方法似乎是在标记的句子上使用preg_split将其分成两部分:第一个否定词之前的部分和该否定词之后的部分。保留分隔符(PREG_SPLIT_DELIM_CAPTURE),然后运行你在第二部分写的正则表达式,之后你可以简单地再将这两个字符串连接在一起。最后,您可以使用正则表达式删除PoS标记以获取I am mad and will notever notset foot in that store notagain,即不使用PoS标记。

相关问题