基于spaCy的否定与依赖分析

2024-05-16 21:47:43 发布

您现在位置:Python中文网/ 问答频道 /正文

情感词在否定的语义范围内表现出很大的不同。我想使用Das and Chen (2001)的稍作修改的版本 它们检测诸如nonotnever之类的单词,然后在否定和从句级标点符号之间的每个单词附加一个“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

为了识别否定,我使用以下方法:

^{pr2}$

现在我要检索否定的范围。在

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]

如何只过滤掉令牌.head.text不是,所以got它在定位吗? 有人能帮我吗?在


Tags: toimporttokennlpspacyiswithnot
1条回答
网友
1楼 · 发布于 2024-05-16 21:47:43

您可以简单地定义并循环使用找到的否定标记的头标记:

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的信息。在

相关问题 更多 >