| By Misam Abbas |
But computers can make guesses about our emotions, or sentiments, based on what we write and say.
Sentiment analysis is the categorization of pieces of text as having varying degrees of positive or negative sentiment using computational methods. For example, humans can easily see the sentiment of each of these sentences. Computers can learn to do the same.
Sentiment can be analyzed at the level of sentences, paragraphs, articles, books and even large corpora of books. You can tell that “Little Women” is happier than “Crime and Punishment” just by looking at the frequency of the positive and negative words in the novels.
The uses of sentiment analysis vary from marketing departments monitoring social media chatter about their brand to stock traders mining real-time news feeds for trading.
It is an area of active research, and there are fairly sophisticated tools out there from Google, Microsoft, and IBM Watson. All these are fairly good, but not one of them reaches human-level performance.
Contrary to the media hype – computers do not read your thoughts, analyze your emotions and predict your future thoughts before you even have them, yet. (I cannot resist ending any sentence on what computers cannot do with a yet). They can’t even analyze the sentiment of written text as well as humans can, yet (like I said).
However, what they can do is read through more text in a few minutes than you can read in your lifetime. And, they can approximately analyze the sentiment of that text. So, using these tools can help us quickly read all works on eighteenth-century English writers and find the ones who were the most melancholy. Similarly, customer advocates in an organization can quickly find which emails received by their organization were sent by customers who were the most enthusiastic or most frustrated.
This analysis often works better at a macro level. So, sentiment analysis is likely to work better when you give it a whole speech instead of specific sentences in a speech.
Bag of Words Model
How would a computer begin to estimate sentiment?
To solve this task we will have to build a model. A model is a simplification of reality that can make analysis easier.
Let’s start by making reality as simple as we can. Let’s say the sentiment of a sentence is the average of the sentiment expressed in its constituent words. And where would the sentiment of individual words come from? From a human-supplied dictionary.
Here is a sample from the dictionary where we assign scores to specific words:
Let us take an example sentence “The movie was nice, and had a great ending”. We look at all the words that have a sentiment score and add them, that gives us:
Avg Score = [ Score(nice) + Score(great) ] / [Number of words with sentiment score]= (2 + 3) / 2 = 2.5
The choice of the divisor is slightly more involved. We can make a case for dividing by the number of words with score or the total number of words. It turns out the former tends to overstate the sentiment of longer sentences and the latter overstates the sentiment of shorter sentences. So, in practice, a balance between two approaches is worked out.
Nothing is as simple as it first seems. But with a few iterations, it is easy to start seeing interesting results with the Bag of Words model especially for larger pieces of text.
State of the Union Addresses
We thought it would be interesting analyze the sentiment of the State of the Union addresses. We used the bag of words model described above to compute a score for each state of the union address since the late 18th century to recent times.
You can see some interesting structure here:
- There is a dip in sentiment at the time of wars and economic recessions. So, look for troughs around the war of 1812, the world wars, the great depression, 2002-2003, recession starting in 2009.
- You can see that the sentiment has generally moved towards the positive direction after the second world war. It is difficult to say whether the times have generally gotten better or the presidents have gotten better at putting things positivity. The resulting question could be the starting point for scholarly research.
Clearly even the simple bag of words model for sentiment analysis is useful. It is also easy to see where it might fail. For example, the model will fail for double negatives like “not bad” – because it treats words as individual tokens and does not take into account the order of their occurrence. However, as we are less likely to use negative words when expressing positive sentiments, the model has acceptable performance for longer speeches.
Machine Learning and Sentiment Analysis
How can we improve on the bag of words model? Just as we assigned sentiment scores to words, we can assign sentiment scores to sentences and see what we can learn from it.
This is where machine learning comes in – supervised machine learning, to be more specific. In supervised learning human labelled examples are given to learning algorithms which can learn the underlying structure to then be able to make sense of new data. Most state of the art sentiment analysis models use supervised machine learning.
Another flavour of machine learning is unsupervised learning where the machine is tasked with learning some structure of the dataset without human labelled examples.
There is an interesting example of a machine learning model acquiring some notion of sentiment without human-created examples. A model was trained to try to predict the next character in Amazon reviews. When researchers observed the internal state of the model they found that part of the state corresponded to sentiment.
You can find more technical details here if that’s your thing: https://blog.openai.com/unsupervised-sentiment-neuron
Machine learning models can exploit the statistical structures of language to give us some useful insights, but they do not understand language in any human sense.
One sure way to trip up any machine(or geek) is sarcasm. Consider the sentence “Of course machines have emotions, they are always happy to confound us”. All Machine learning models I tested this on consider this sentence to be quite positive.
So, I submit that while sentiment analysis is a useful tool – machines are still quite dumb.
Or, perhaps smarter than I dare to imagine (and are actually happy to confound us).