在否定的语义范围内时,情感词的行为有很大不同。我想使用稍微修改的Das and Chen (2001)版本
它们检测到诸如“否”,“不”和“永不”之类的单词,然后将“否定”后缀附加到在否定和子句级标点符号之间出现的每个单词。
我想从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)
可视化的依赖路径:
很好,在依赖标签方案中存在一个否定修饰符;
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]
如何只过滤掉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/