Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to do emotional Analysis with Python Code

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use Python code to do emotional analysis, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

A computer with Internet access

Basic python code reading ability to modify several model parameters

Strong interest in the latest achievements of Baidu Chinese NLP

Training model: basic introduction of Senta emotion analysis model

Senta is an open Chinese emotion analysis model of Baidu NLP, which can be used to analyze the emotion of Chinese sentences. The output result is one of {positive / neutral / negative}. For details on the structure of the model, please see Senta----github.com/PaddlePaddle/Paddlehub/demo/senta.

This sample code chooses the Senta-BiLSTM model.

Model source: introduction to Paddlehub

PaddleHub is a pre-training model management tool developed based on PaddlePaddle, which can carry out transfer learning more easily with the help of pre-training model.

In this evaluation, only the pre-training model was used and no fine-tune was carried out.

Code running environment: Baidu AI studio

Experimental code

From paddlehub/senta_demo.py

Github: https://github.com/PaddlePaddle/PaddleHub/blob/release/v0.5.0/demo/senta/senta_demo.py

From _ _ future__ import print_function import json import os import six import paddlehub as hub if _ _ name__ = = "_ main__": # load the senta model senta = hub.Module (name= "senta_bilstm") # put the short text to be tested in this list in str format test_text = [this restaurant is not very delicious "," this movie is not very good " ] # specify model input input_dict = {"text": test_text} # the text classification function results = senta.sentiment_classify (data=input_dict) that feeds data to the senta model # traverses and analyzes each short text for index, text in enumerate (test_text): results [index] ["text"] = text for index, result in enumerate (results): if six.PY2: print (json.dumps (results [index], encoding= "utf8") Ensure_ascii=False) else: print ('text: {}, predict: {}' .format (results [index] ['text'], results [index] [' sentiment_key']))

Detailed evaluation

Emotional Analysis of idioms

Input

Test_text = [

"vicissitudes of life", # medium, things have changed a lot

"Xialiba people", # commendatory meaning, popular literature and art

"praise all people", # praise, praise the outstanding good people and good deeds

"dangerous words and dangerous deeds", # commendatory meaning, say words of integrity, do things of integrity

"miraculous craftsmanship", # commendatory, refers to the beauty of nature

"Don't like the word", # commendatory meaning, can not add another sentence, indicating that it is well written

"No points in writing", # commendatory meaning, refers to excellent writing skills

"less than satisfactory", # commendatory meaning, generally satisfactory.

"meticulous", # commendatory, refers to thoughtfulness

"twice the effort with half the effort", a commendatory meaning, means that there is a good effect without effort.

"twice the result with half the effort", # derogatory, refers to a waste of effort but no good effect

"ready to move", # derogatory, refers to the intention of doing bad things

"beyond recognition", # derogatory, refers to great destruction

"the river is declining", a derogatory term, refers to the decline of things.

'comment on the foot', # derogatory, refers to a section that is too picky

"the loss of life", a derogatory term, refers to the extreme hardship of the people

"initiator", # derogatory, the first person to do a bad thing

"do everything", # derogatory, do anything bad

"omnipotent", # derogatory, do anything bad

"Spring and Snow", # derogatory, profound and incomprehensible art

]

Output

Running time: 4 seconds 480ms text: vicissitudes of life, positive_prob: 0.3838, predict: negative # error text: Xialiba people, positive_prob: 0.7957, predict: positive text: reputation, positive_prob: 0.906, predict: positive text: dangerous words and actions, positive_prob: 0.588, predict: positive text: miraculous craftsmanship, positive_prob: 0.657, predict: positive text: dislikes, positive_prob: 0.9698 Predict: positive text: text without adding points, positive_prob: 0.1284, predict: negative # error text: not satisfactory, positive_prob: 0.0429, predict: negative # error text: meticulous, positive_prob: 0.8997, predict: positive text: half the effort, positive_prob: 0.6181, predict: positive text: twice the result with half the effort, positive_prob: 0.8558, predict: positive # error text: ready to do something, positive_prob: 0.7353 Predict: positive # error text: beyond recognition, positive_prob: 0.2186, predict: negative text: decline, positive_prob: 0.2753, predict: negative text: comment, positive_prob: 0.6737, predict: positive # error text: lives destroyed, positive_prob: 0.4661, predict: neutral # error text: author, positive_prob: 0.247, predict: negative text: do everything, positive_prob: 0.5948 Predict: positive # error text: omnipresent, positive_prob: 0.553, predict: positive # error text: Yang Chunxue, positive_prob: 0.7552, predict: positive # error

Correct rate: 10 percentile 20 = 50%

Emotional Analysis of adversative complex sentences

Input

Test_text = ['Xiaoming won the first place in the exam, but he is not proud at all', # positive 'you are not smart, but not serious', # negative 'although Xiaoming worked hard, he still didn't get a score of 100', # negative 'although Xiaoming is naughty sometimes, he is very sensible', although the bridge has been built for many years But she is still strong. Although he is naughty, he studies very well. Active learning is not boring, but interesting. Although it is difficult, I will not flinch. Although my little sister is only 5 years old, she can learn the multiplication formula by heart. But it's all for your own good', # positive 'Xiao Ming's grades are not good, not because he is not smart, but because he doesn't work hard. Although it is not appropriate to do so, it is already the best choice. But I believe I will make it out. Although Grandpa is very old, he still exercises every day. It's not that we don't have beauty, but that we lack the vision to find beauty. Although they have good living conditions, wasting resources will bring bad consequences sooner or later. But our friend', # positive'he doesn't know how to do it, but doesn't want to do it. Although that dream seems out of my reach, I believe it will come true through my efforts.

Output

Running time: 2 seconds 667 milliseconds text: although Xiaoming got the first place in the exam, he is not proud at all. Positive_prob: 0.9598, predict: positive text: you are not smart, but not serious. Positive_prob: 0.0275, predict: negative text: although Xiaoming worked very hard, he still didn't get 100s in the exam. Positive_prob: 0.7188, predict: positive # error text: although Xiaoming is sometimes very naughty But he is very sensible, positive_prob: 0.8776, predict: positive text: although the bridge has been built for many years, she is still very strong. Positive_prob: 0.9782, predict: positive text: although he is very naughty, he studies very well, positive_prob: 0.9181, predict: positive text: learning is not boring, but fun, positive_prob: 0.3279, predict: negative # error text: although it is very difficult But I still won't back down. Positive_prob: 0.3974, predict: negative # error text: although my little sister is only five years old, she can memorize the multiplication formula backwards. Positive_prob: 0.5124, predict: neutral text: although I am out of line, it's all for your own good. Positive_prob: 0.399, predict: negative # error text: Xiaoming's grades are not good, not because she is not smart But because of not working hard, positive_prob: 0.1881, predict: negative text: although this is not appropriate, it is already the best choice, positive_prob: 0.806, predict: positive text: although this failure is the beginning of success, positive_prob: 0.4862, predict: neutral # error text: although this question is very difficult But I believe I will do it. Positive_prob: 0.3959, predict: negative # error text: although Grandpa is very old, he still insists on doing exercise every day. Positive_prob: 0.9178, predict: positive text: not without beauty, but because we lack the vision to find beauty, positive_prob: 0.5614, predict: positive text: although they have good living conditions But wasting resources will bring bad consequences sooner or later. Positive_prob: 0.1086, predict: negative text: he is not our enemy, but our friend. Positive_prob: 0.3749, predict: negative # error text: it's not that he can't do it, but he doesn't want to do it. Positive_prob: 0.1247, predict: negative text: although that dream seems out of reach to me. But I believe it will be realized through my efforts, positive_prob: 0.957, predict: positive

Correct rate: 13 stroke 20 = 65%

Emotional analysis of specific scenes

Input

Test_text = ['the car consumes fuel quickly', 'the car drives very fast', 'the room smells like a dead rat', 'the room smells like smoke', 'his hair smells like killing Matt', 'the clothes are washed off by the machine', 'the clothes wear too much pilling', 'the software is easy to flash','he plays like Cai Xukun', 'this one is 20', 'this one can be played' "he shoots like Kobe Bryant", "the layout of this room is very romantic", "this wine is memorable", "this dress is cool", "his profile looks like Lin Feng", "five-star praise", "he will buy back later", "cost-effective", "good quality and low price", "this girl moves me"]

Output

Running time: 2 seconds 676 milliseconds text: this car consumes gas very fast, positive_prob: 0.2926, predict: negative text: this car drives very fast, positive_prob: 0.8478, predict: positive text: this room smells like a dead rat, positive_prob: 0.0071, predict: negative text: this room smells of smoke, positive_prob: 0.2071, predict: negative text: his hairstyle is like killing Matt, positive_prob: 0.3445 Predict: negative text: this clothes machine washes off color, positive_prob: 0.3912, predict: negative text: this dress is too pilling, positive_prob: 0.679, predict: positive # error text: this software is easy to flicker, positive_prob: 0.0051, predict: negative text: he plays like Cai Xukun, positive_prob: 0.8684, predict: positive # error text: this one is 20, positive_prob: 0.1695, predict: negative text: this one can be played Positive_prob: 0.3503, predict: negative # error text: he shoots like Kobe Bryant, positive_prob: 0.7263, predict: positive text: the layout of this room is very exquisite, positive_prob: 0.9519, predict: positive text: this wine is aftertaste, positive_prob: 0.7431, predict: positive text: this dress is very cool, positive_prob: 0.9817, predict: positive text: his side face looks like Lin Feng, positive_prob: 0.5621 Predict: positive text: five-star praise, positive_prob: 0.9971, predict: positive text: buy back later, positive_prob: 0.6903, predict: positive text: cost-effective, positive_prob: 0.9799, predict: positive text: quality and cheap, positive_prob: 0.9542, predict: positive text: this girl moves me, positive_prob: 0.956, predict: positive

Correct rate: 17 stroke 20 = 85%

To sum up, the three different categories of assessments are as follows:

Summary

1. The calculation of the model takes less time, and the use experience is good.

two。 In terms of emotional analysis of idioms, I specially select some idioms that are difficult to understand literally and easy to confuse emotions (such as being judged to be negative), which are also the contents of the college entrance examination. Although the final correct rate of the model is only average, I think it is acceptable. Adding idiom sentences as training corpus will make the model "better understand" Chinese.

If you are interested, you can try some idioms that are easier to understand emotion literally. I think the score will be better than the result of this evaluation.

3. The emotional analysis of the turning point sentence itself is also a challenge to the model, and the measured effect is 65 points. I think the model does not have enough attention for words like "but" and "although", because the semantics behind these turning words often affect the emotion of the whole sentence most, and the final score is 65 points. I think the model performs generally in this respect.

4. The best score is the emotional analysis of the specific scene, probably there are a large number of Taobao comments in the pre-training corpus? Small words like killing Matt 20 Kobe Bryant are the key to judging emotion, and the model does capture and judge it, which surprises me.

The above content is how to do emotional analysis with Python code. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report