【问题标题】:Negation Marking with Regular Expressions in Python在 Python 中使用正则表达式进行否定标记
【发布时间】:2016-08-25 00:48:58
【问题描述】:

我正在努力在 Python 中使用正则表达式实现否定标记,例如 Christopher Potts 的 sentiment analysis tutorial

取自他的教程的否定定义是:

(?:
    ^(?:never|no|nothing|nowhere|noone|none|not|
        havent|hasnt|hadnt|cant|couldnt|shouldnt|
        wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint
    )$
)
|
n't

而从句级标点的定义是:

^[.:;!?]$

这个想法是捕获否定和子句级标点之间的单词,然后修改它们以表明它们被否定,例如:

No one enjoys it.

应该变成这样:

No one_NEG enjoys_NEG it_NEG.

任何建议将不胜感激。

【问题讨论】:

    标签: python regex nlp


    【解决方案1】:

    如果你有一个句子作为字符串,正如你暗示的那样,那么你不能在你的正则表达式中使用'^'和'$'。请改用\b。那么这应该工作:

    def add_negation_markers(m):
        return m.group(1) + re.sub(r'(?<=\w)\b', '_NEG', m.group(2))
    re.sub('(' + neg_re + ')(.*)(?=' + punct_re + ')', add_negation_markers, text)
    

    如果您有一个句子作为单词列表,正如$^ 标记所暗示的那样,那么...

    def negate(word):
        if re.search(punct_re, word):
            negate.should = False
        elif re.search(neg_re, word):
            negate.should = True
        elif negate.should:
            return word + '_NEG'
        return word
    negate.should = False
    map(negate, words)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 2021-11-02
      • 2021-11-28
      相关资源
      最近更新 更多