gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:34:05 29 4
gpt4 key购买 nike

情感词在否定语义范围内的表现非常不同。我想使用 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

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

 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并且正在定位?有人可以帮我吗?

最佳答案

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

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

关于python - 使用 spaCy 进行否定和依赖解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54849111/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com