我正在做情绪分析,我想知道如何在上显示我的句子中的其他情绪分数:“特斯拉的股票增长了20%。”
我有三种情绪:阳性、阴性和中性。
这是我的代码,其中包含了我想分类的句子:
pip install happytransformer
from happytransformer import HappyTextClassification
happy_tc = HappyTextClassification("BERT", "ProsusAI/finbert", num_labels=3)
result = happy_tc.classify_text("Tesla's stock just increased by 20%")这是结果代码和输出:
print(result)
TextClassificationResult(label='positive', score=0.929110586643219)这是情绪评分,它只显示正面的分数:
print(result.label)
print(result.score)
positive
0.92现在,我如何使它显示的情绪得分的消极和中立,以及积极?
像这样的东西:
positive
0.92
negative
0.05
neutral
0.03谢谢。
发布于 2021-06-15 14:44:34
因为HappyTransformer不支持多类概率,所以我建议使用另一个库。库flair提供了更多的功能,可以提供所需的多类概率,如下所示:
from flair.models import TextClassifier
from flair.data import Sentence
tc = TextClassifier.load('en-sentiment')
sentence = Sentence('Flair is pretty neat!')
tc.predict(sentence, multi_class_prob=True)
print('Sentence above is: ', sentence.labels)仅供使用的pip install flair。
注意,我们使用的模型与BERT不同,并且只返回两个标签,而不是三个。
另一种选择是使用HuggingFace库。它允许使用自定义标签。
from transformers import pipeline
classifier = pipeline("zero-shot-classification")
classifier(
"This is a course about the Transformers library",
candidate_labels=["education", "politics", "business"],
){'sequence': 'This is a course about the Transformers library',
'labels': ['education', 'business', 'politics'],
'scores': [0.8445963859558105, 0.111976258456707, 0.043427448719739914]}在您的例子中,您将标签切换到["positive", "negative", "neutral"]。
https://stackoverflow.com/questions/67987738
复制相似问题