gpt4 book ai didi

python - 使用依赖规则匹配的aspect-opinion提取中的命名实体识别

转载 作者:行者123 更新时间:2023-12-03 18:33:59 26 4
gpt4 key购买 nike

使用 Spacy,我根据我定义的语法规则从文本中提取方面-意见对。规则基于POS标签和依赖标签,由token.pos_获取和 token.dep_ .下面是语法规则之一的示例。如果我传了句Japan is cool,它返回 [('Japan', 'cool', 0.3182)] ,其中该值代表cool的极性.

但是我不知道如何让它识别命名实体。例如,如果我通过 Air France is cool ,我要得到[('Air France', 'cool', 0.3182)]但我目前得到的是 [('France', 'cool', 0.3182)] .

我检查了 Spacy 在线文档,我知道如何提取 NE( doc.ents )。但我想知道可能的解决方法是使我的提取器工作。请注意,我不想要强制措施,例如连接字符串 AirFrance , Air_France等等。

谢谢!

import spacy

nlp = spacy.load("en_core_web_lg-2.2.5")
review_body = "Air France is cool."
doc=nlp(review_body)

rule3_pairs = []

for token in doc:

children = token.children
A = "999999"
M = "999999"
add_neg_pfx = False

for child in children :
if(child.dep_ == "nsubj" and not child.is_stop): # nsubj is nominal subject
A = child.text

if(child.dep_ == "acomp" and not child.is_stop): # acomp is adjectival complement
M = child.text

# example - 'this could have been better' -> (this, not better)
if(child.dep_ == "aux" and child.tag_ == "MD"): # MD is modal auxiliary
neg_prefix = "not"
add_neg_pfx = True

if(child.dep_ == "neg"): # neg is negation
neg_prefix = child.text
add_neg_pfx = True

if (add_neg_pfx and M != "999999"):
M = neg_prefix + " " + M

if(A != "999999" and M != "999999"):
rule3_pairs.append((A, M, sid.polarity_scores(M)['compound']))

结果
rule3_pairs
>>> [('France', 'cool', 0.3182)]

期望输出
rule3_pairs
>>> [('Air France', 'cool', 0.3182)]

最佳答案

在提取器中集成实体非常容易。对于每对 child ,您应该检查“A” child 是否是某个命名实体的头部,如果是,则使用整个实体作为您的对象。

这里我提供整个代码

!python -m spacy download en_core_web_lg
import nltk
nltk.download('vader_lexicon')

import spacy
nlp = spacy.load("en_core_web_lg")

from nltk.sentiment.vader import SentimentIntensityAnalyzer
sid = SentimentIntensityAnalyzer()


def find_sentiment(doc):
# find roots of all entities in the text
ner_heads = {ent.root.idx: ent for ent in doc.ents}
rule3_pairs = []
for token in doc:
children = token.children
A = "999999"
M = "999999"
add_neg_pfx = False
for child in children:
if(child.dep_ == "nsubj" and not child.is_stop): # nsubj is nominal subject
if child.idx in ner_heads:
A = ner_heads[child.idx].text
else:
A = child.text
if(child.dep_ == "acomp" and not child.is_stop): # acomp is adjectival complement
M = child.text
# example - 'this could have been better' -> (this, not better)
if(child.dep_ == "aux" and child.tag_ == "MD"): # MD is modal auxiliary
neg_prefix = "not"
add_neg_pfx = True
if(child.dep_ == "neg"): # neg is negation
neg_prefix = child.text
add_neg_pfx = True
if (add_neg_pfx and M != "999999"):
M = neg_prefix + " " + M
if(A != "999999" and M != "999999"):
rule3_pairs.append((A, M, sid.polarity_scores(M)['compound']))
return rule3_pairs

print(find_sentiment(nlp("Air France is cool.")))
print(find_sentiment(nlp("I think Gabriel García Márquez is not boring.")))
print(find_sentiment(nlp("They say Central African Republic is really great. ")))

此代码的输出将是您所需要的:
[('Air France', 'cool', 0.3182)]
[('Gabriel García Márquez', 'not boring', 0.2411)]
[('Central African Republic', 'great', 0.6249)]

享受!

关于python - 使用依赖规则匹配的aspect-opinion提取中的命名实体识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60967134/

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