2016-08-25 6 views
-1

私はPythonで正規表現を使用してネゲーションマーキングを実装するのに苦労しています、ラ・クリストファー・ポッツのsentiment analysis tutorial正規表現でのPythonでのネガティブマーキング

彼のチュートリアルから取ら否定、の定義は次のとおりです。

(?: 
    ^(?: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. 

いずれかの提案があります。

答えて

0

文字列として文章がある場合は、正規表現で '^'と '$'を使用することはできません。代わりに\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)