【发布时间】:2016-04-05 21:43:23
【问题描述】:
我需要在句子中标记否定上下文。算法如下:
- 检测否定符(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 .
我该怎么做?
【问题讨论】:
标签: python regex python-2.7 nlp