In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to use python to achieve simple text emotion analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
I haven't released any new content on the official account for a long time. During this period of time, I didn't expect that so many python enthusiasts followed me. Hong Kong was really excited.
Today, I will bring you the simple multi-text emotion analysis code, code environment python3.5, which I have just implemented.
Principle
For example: "the picture of this phone is excellent and the operation is smooth." but taking pictures really sucks! the system is not good either. "
① emotion words
To analyze whether a sentence is positive or negative, the simplest and most basic way is to find out the emotional words in the sentence, positive emotional words such as: like, good, easy, gorgeous, negative emotional words such as: bad, cheating, etc. There is a positive word on + 1 and a negative word on-1.
There are two positive emotional words "good" and "smooth" and "bad" a negative emotional word. Then its emotional score is 1-1-1-1-2. Obviously, this score is unreasonable, so let's modify it step by step.
② degree words
"good", "smooth" and "bad" are all preceded by a degree modifier. " "excellent" is stronger than "better" or "good" emotion, and "too bad" is much better than "a little bad" emotion. So you need to find out if there is a degree of modification after finding the emotional words, and give a weight to different degrees. For example, "extreme", "matchless", "too" should put the emotional score * 4, "more", "still" on the emotional score * 2, "only", "only" these are * 0.5. So the emotional score of this sentence is: 4 "1" 1 "2-1" 4 "1" 3
③ exclamation point
You can find that it's too bad, followed by an exclamation point, which means strong emotion. Therefore, it is found that the exclamation point can be an emotional value of + 2. Then the emotional score of this sentence becomes: 4-1-1-2-1-4-2-1.
④ negative words
Discerning people can see at a glance that the last "good" does not mean "good", because there is a "no" in front of it. So when you find emotional words, you need to look forward for negative words. Such as the words "no" and "can't". And count the number of times these negative words appear. If it is odd, the emotion score is *-1, but if it is even, then the emotion is not reversed, or * 1. In this sentence, we can see that there is only a "no" before "good", so the emotional value of "good" should be reversed, *-1.
So the exact emotional score of this sentence is: 4 "1" 1 "2-1" 4-2 "1" 1 =-1
⑤ separates positive from negative.
Then, it is obvious that there are praise and criticism in this sentence, and its emotional tendency cannot be expressed by a score. And the setting of this weight will also affect the final emotional score, the sensitivity is too high. So the final correct way to deal with this sentence is to get a positive score and a negative score (so the negative score is also positive, so there is no need to use negative numbers). They also represent the emotional tendency of this sentence. So the comment should be "positive score: 6, negative score: 7"
⑥ is based on the emotion of clauses.
One more step, in more detail, the emotional score of a comment is added up by different clauses, so to get the emotional score of a comment, it is necessary to calculate the emotional score of each sentence in the comment. The comment on this example has four clauses, so its structure is as follows ([positive score, negative score]): [[4,0], [2,0], [0,6], [0,1]]
The above is the main process of using the emotion dictionary for emotion analysis, and the design of the algorithm will be implemented according to this idea.
Algorithm design
The first step: read the comment data and clause the comment.
Step 2: find the emotional words of the clause, record whether it is positive or negative, and the location.
Step 3: look for the degree word in front of the emotion word, and stop the search when you find it. Set the weight for the degree word and multiply it by the emotional value.
Step 4: look for negative words in front of emotional words, find all negative words, if the number is odd, multiply by-1, if even, multiply by 1.
Step 5: judge whether there is an exclamation point at the end of the clause, if there is an exclamation point, look for emotional words forward, and then the corresponding emotional value is + 2.
Step 6: calculate the emotional values of all the clauses of a comment and record them in an array (list).
Step 7: calculate and record the emotional values of all comments.
Step 8: calculate the positive emotion mean, negative emotion mean, positive emotion variance and negative emotion variance of each comment through clauses.
Actual combat
Here is I refer to the existing code, combined with their own needs, made a simple modification to the code. The running environment of this script is python3.5, those who use 2.x will forgive me.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.