使用spaCy进行否定和依赖性解析

时间:2019-02-24 05:43:38

标签: python spacy

在否定的语义范围内,情感词的行为有很大不同。我想使用Das and Chen (2001)的稍微修改的版本 他们检测诸如 no not never 之类的词,然后在出现在否定和否定之间的每个词后面附加一个“ neg”后缀子句级标点符号。 我想用spaCy的依赖项解析创建类似的东西。

import spacy
from spacy import displacy

nlp = spacy.load('en')
doc = nlp(u'$AAPL is óóóóópen to ‘Talk’ about patents with GOOG definitely not the treatment #samsung got:-) heh')

options = {'compact': True, 'color': 'black', 'font': 'Arial'}
displacy.serve(doc, style='dep', options=options)

可视化的依赖路径:

enter image description here

很好的是,依赖项标记方案中存在一个否定修饰符; NEG

为了确定否定,我使用以下内容:

 negation = [tok for tok in doc if tok.dep_ == 'neg']

现在,我想检索这些求反的范围。

import spacy
from spacy import displacy
import pandas as pd

nlp = spacy.load("en_core_web_sm")
doc = nlp(u'AAPL is óóóóópen to Talk about patents with GOOG definitely not the treatment got')

print('DEPENDENCY RELATIONS')
print('Key: ')
print('TEXT, DEP, HEAD_TEXT, HEAD_POS, CHILDREN')

for token in doc:
    print(token.text, token.dep_, token.head.text, token.head.pos_,
      [child for child in token.children])

这将提供以下输出:

DEPENDENCY RELATIONS
Key: 
TEXT, DEP, HEAD_TEXT, HEAD_POS, CHILDREN
AAPL nsubj is VERB []
is ROOT is VERB [AAPL, óóóóópen, got]
óóóóópen acomp is VERB [to]
to prep óóóóópen ADJ [Talk]
Talk pobj to ADP [about, definitely]
about prep Talk NOUN [patents]
patents pobj about ADP [with]
with prep patents NOUN [GOOG]
GOOG pobj with ADP []
definitely advmod Talk NOUN []
not neg got VERB []
the det treatment NOUN []
treatment nsubj got VERB [the]
got conj is VERB [not, treatment]

如何仅过滤出 not 的token.head.text,所以got会被定位? 有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您可以简单地定义并遍历找到的否定标记的头部标记:

negation_tokens = [tok for tok in doc if tok.dep_ == 'neg']
negation_head_tokens = [token.head for token in negation_tokens]

for token in negation_head_tokens:
    print(token.text, token.dep_, token.head.text, token.head.pos_, [child for child in token.children])

为您打印got的信息。