请考虑以下字符串:
I am mad and will not ever set foot in that store again
我正在使用POS标记器来标记字符串,如下所示:
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
现在,我使用正则表达式将"not"连接到动词,同时忽略否定词(从不,两者都不是等)
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
但是,我想要的只是将"not"连接到出现在(第一个)否定词之后的动词。注意mad
和will
而不是notmad
和notwill
:
I am mad and will notever notset foot in that store notagain
所以我认为首先我应该在句子中寻找任何否定词(从不|也不是|不要|不会|不|不|不)并且只从那里执行正则表达式。但是我该怎么做呢?
最简单的方法似乎是在标记的句子上使用preg_split
将其分成两部分:第一个否定词之前的部分和否定词之后的部分。保留分隔符 (PREG_SPLIT_DELIM_CAPTURE),然后运行您在第二部分编写的正则表达式,之后您可以简单地将这两个字符串再次连接在一起。最后,您可以使用正则表达式删除 PoS 标签来获取I am mad and will notever notset foot in that store notagain
,即没有 PoS 标签。