如何修改 Python 中匹配特定正则表达式的文本?
How to modify text that matches a particular regular expression in Python?
我需要在句子中标记否定语境。算法如下:
- 检测到否定符(not/never/ain't/don't/等)
- 检测到以标点结尾的子句 (.;:!?)
- 将 _NEG 添加到这之间的所有单词。
现在,我已经定义了一个正则表达式来挑选出所有此类事件:
def replacenegation(text):
match=re.search(r"((\b(never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint)\b)|\b\w+n't\b)((?![.:;!?]).)*[.:;!?\b]", text)
if match:
s=match.group()
print s
news=""
wlist=re.split(r"[.:;!? ]" , s)
wlist=wlist[1:]
print wlist
for w in wlist:
if w:
news=news+" "+w+"_NEG"
print news
我可以检测并替换匹配的组。但是,我不知道如何在这个操作之后重新创建完整的句子。同样对于多个匹配项,match.groups() 给我错误的输出。
比如我输入的句子是:
I don't like you at all; I should not let you know my happiest secret.
输出应该是:
I don't like_NEG you_NEG at_NEG all_NEG ; I should not let_NEG you_NEG know_NEG my_NEG happiest_NEG secret_NEG .
我该怎么做?
首先你最好把否定的look-ahead(?![.:;!?]).)*
改成否定的字class.
([^.:;!?]*)
然后你需要使用 none 捕获组并删除多余的否定词,因为你已经用 3 个捕获组包围了它,它将 returns 3 匹配你的否定词,例如not
。然后你可以使用 re.findall()
找到所有的匹配项:
>>> regex =re.compile(r"((?:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint)\b|\b\w+n't\b)([^.:;!?]*)([.:;!?\b])")
>>>
>>> regex.findall(s)
[("don't", ' like you at all', ';'), ('not', ' let you know my happiest secret', '.')]
或者为了替换单词,您可以使用 re.sub
和 lambda 函数作为替换符:
>>> regex.sub(lambda x:x.group(1)+' '+' '.join([i+'_NEG' for i in x.group(2).split()])+x.group(3) ,s)
"I don't like_NEG you_NEG at_NEG all_NEG; I should not let_NEG you_NEG know_NEG my_NEG happiest_NEG secret_NEG."
请注意,要捕获标点符号,您还需要将其放入捕获组。然后你可以在编辑后re.sub()
中的句子末尾添加它。
我需要在句子中标记否定语境。算法如下:
- 检测到否定符(not/never/ain't/don't/等)
- 检测到以标点结尾的子句 (.;:!?)
- 将 _NEG 添加到这之间的所有单词。
现在,我已经定义了一个正则表达式来挑选出所有此类事件:
def replacenegation(text):
match=re.search(r"((\b(never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint)\b)|\b\w+n't\b)((?![.:;!?]).)*[.:;!?\b]", text)
if match:
s=match.group()
print s
news=""
wlist=re.split(r"[.:;!? ]" , s)
wlist=wlist[1:]
print wlist
for w in wlist:
if w:
news=news+" "+w+"_NEG"
print news
我可以检测并替换匹配的组。但是,我不知道如何在这个操作之后重新创建完整的句子。同样对于多个匹配项,match.groups() 给我错误的输出。
比如我输入的句子是:
I don't like you at all; I should not let you know my happiest secret.
输出应该是:
I don't like_NEG you_NEG at_NEG all_NEG ; I should not let_NEG you_NEG know_NEG my_NEG happiest_NEG secret_NEG .
我该怎么做?
首先你最好把否定的look-ahead(?![.:;!?]).)*
改成否定的字class.
([^.:;!?]*)
然后你需要使用 none 捕获组并删除多余的否定词,因为你已经用 3 个捕获组包围了它,它将 returns 3 匹配你的否定词,例如not
。然后你可以使用 re.findall()
找到所有的匹配项:
>>> regex =re.compile(r"((?:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint)\b|\b\w+n't\b)([^.:;!?]*)([.:;!?\b])")
>>>
>>> regex.findall(s)
[("don't", ' like you at all', ';'), ('not', ' let you know my happiest secret', '.')]
或者为了替换单词,您可以使用 re.sub
和 lambda 函数作为替换符:
>>> regex.sub(lambda x:x.group(1)+' '+' '.join([i+'_NEG' for i in x.group(2).split()])+x.group(3) ,s)
"I don't like_NEG you_NEG at_NEG all_NEG; I should not let_NEG you_NEG know_NEG my_NEG happiest_NEG secret_NEG."
请注意,要捕获标点符号,您还需要将其放入捕获组。然后你可以在编辑后re.sub()
中的句子末尾添加它。