如何识别句子中有助于特定情绪的单词?

问题描述 投票:0回答:1

因此,我使用预训练的变形金刚模型来执行情感分析,但我想检查哪些词会导致特定的情感类别。谁能指导我应该采用什么方法? 以下是我正在使用的代码

from transformers import AutoTokenizer
from transformers import pipeline
from transformers import AutoModelForSequenceClassification
MODEL = "nlptown/bert-base-multilingual-uncased-sentiment"
tokenizer = AutoTokenizer.from_pretrained(MODEL)
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
tokens = tokenizer.encode_plus(txt, add_special_tokens=False,
                           return_tensors='pt')
outputs = model(**tokens)

probs = torch.nn.functional.softmax(outputs[0], dim=-1)
probs = probs.mean(dim=0)
"""in this model the range is from 1-5,
1-highly neg
2-negative
3-neutral
4-positive
5-higly pos
so here we will find the max from tensor and its index so we can map our sentiment"""
sen_dict= {1:'Highly-negative',2:'Negative',3:'Neutral',4:'Positive',5:'Highly-positive'}
sen_idx = probs.max(dim=0).indices.item() + 1 #added 1 as the range here is 0-4 and we want to map it between 1-5
sentiment = sen_dict[sen_idx]
print(sentiment)
python huggingface-transformers sentiment-analysis huggingface
1个回答
0
投票

在较高层面上,有 3 或 4 种主要方法。

首先,您可以反复省略一个标记,然后查看哪些单词会改变情绪得分。这有点粗糙,但可以与您现有的

SequenceClassification
模型一起使用。

其次,您可以获取/训练一个模型来告诉您句子中最有贡献的单词的索引。我提到它是为了完整性,因为第三种方法更好。尽管这是聊天法学硕士的方法:“告诉我对……的情感贡献最大的词。”。

第三,您可以获取/训练一个模型来告诉您每个标记的情绪,而不是整个句子的情绪。使用 HuggingFace,这可能就像从

AutoModelForSequenceClassification
切换到
AutoModelForTokenClassification
一样简单。

您显然需要找到一个经过训练的模型来给出每个代币的情绪。或者收集一些训练数据,自己训练。 (大多数训练数据在句子级别都有情感。)

第四种方法是老套:在 Transformer 和 seq2seq 模型出现之前,我们有列出单词情感的词典。然后,找到句子情感的最简单方法是将每个单词的情感相加。

© www.soinside.com 2019 - 2024. All rights reserved.