Sentiment Analysis

Sentiment analysis is like having a machine read people's words and figure out how they feel about something. Just like when we talk to friends and they might sound happy, sad, or excited, computers can also listen to what people write online and understand their emotions.

sentiment-analysis.gif

Imagine if you wrote a message about a movie you just watched, and you were either happy, sad, or just okay with it. Sentiment analysis helps a computer figure out if your message is thumbs up, thumbs down, or somewhere in between. Check out the video below to gain more understanding about sentiment analysis.

To analyse sentiments in a text or document, we can look at it from 4 different levels. To understand these, we'll use a movie reviews dataset.

Document level: This type of analysis looks at the overall sentiment of an entire text or document. It aims to determine whether the document is positive, negative, or neutral. Suppose you have a collection of movie reviews about a recent blockbuster film, Document-level sentiment analysis would involve reading each entire review and categorizing it as positive, negative, or neutral based on the overall tone of the review.

Sentence level: Each sentence is classified as positive, negative, or neutral, regardless of the sentiment of the entire text or document. For example, if a reviewer says...

                        The acting was great, but the plot was confusing.

The analysis would identify that the first part of the sentence is positive (praising the acting) and the second part is negative (criticizing the plot).

Aspect level: focuses on extracting sentiments related to specific aspects or features mentioned in the text. For example, aspect-based sentiment analysis could reveal that audiences generally liked the acting and special effects but had mixed feelings about the ending.

Entity or feature level: Similar to aspect-based analysis, this analysis identifies sentiments towards named entities, which could be people, places, products, or any other entities mentioned in the text. If the movie features a popular actor, entity-level sentiment analysis would focus on how people perceive that actor's performance.

Sentiment analysis of movie reviews

Let's see how we can perform sentiment analysis on a movie review dataset. The dataset we'll be using comes with the NLTK library, so all we need to do is download the dataset as shown in the code snippet below. Play around with the additional_test_reviews by adding your own reviews and see how the model classify it as either positive or negative.

In this code snippet, we're using the NLTK library to perform sentiment analysis on the movie reviews dataset. We load positive and negative reviews, split the data into training and testing sets, and then use the Naive Bayes classifier to train the model. The extract_features function is used to extract relevant features from the words in the reviews. Next, we evaluated the model's accuracy on the testing data.

Finally, we added three additional test cases (additional_test_reviews). After evaluating the classifier's accuracy, the code will print the predicted polarity, positive or negative, for each of these additional test cases.


➡️ Next, we'll look at Named entity recognition... 🎯.