Posted on Leave a comment

Analyze text sentiment with Python in 5 lines

It’s no secret that machines can now detect and measure and study our emotions from parsing text. One of ways this is achieved is through sentiment analysis. Just ask Siri, Google or Alexa. Here is how Wikipedia defines sentiment analysis: “Sentiment analysis (also known as opinion mining or emotion AI) refers to the use of natural language processingtext analysiscomputational linguistics, and biometrics to systematically identify, extract, quantify, and study affective states and subjective information. Sentiment analysis is widely applied to voice of the customer materials such as reviews and survey responses, online and social media, and healthcare materials for applications that range from marketing to customer service to clinical medicine.” – https://en.wikipedia.org/wiki/Sentiment_analysis

With the surge of social media platform, sentiment analysis has really gained in popularity with businesses. Imagine for example being able to comb through comments on a product page, a Facebook page or a YouTube video and understanding if the comments are positive, neutral or negative. Imagine being able to identify within the comments which aspects people view as positive or negative. Equipped with this information, businesses can make improvement. Pre-Natural Text Analysis with machine learning, this would have hours of reading and categorizing by people. Now, a computer program can quickly comb through the text and within a few minutes, output the result. That’s the true power behind sentiment analysis.

How does Python do sentiment analysis?

We have released a series of Python courses to help young people pick up this much needed skill. If you have completed our Python 101, that’s enough skill to do what we are about to show you here. Don’t be intimidated. But first, let’s understand how sentiment analysis works.

For this code, we will use the TextBlob Python module. You can learn about this module here: https://textblob.readthedocs.io/en/dev/index.html. This module has abstracted a lot of the hard work so you can do sentiment analysis with just a couple lines of code. For sentiment, this module outputs 2 values:

  1. Polarity: this is a number between -1.0 and 1.0 which indicates on a scale whether the text expresses a negative sentiment (negative number) or a positive sentiment (positive number). The closer the number is to -1.0 or 1.0, the more negative or positive the text is respectively.
  2. Subjectivity: this is a number between 0.0 and 1.0 where 0.0 is very objective (facts) and 1.0 is very subjective (opinion)

Write your first sentiment analysis in Python

Your ready? Here is the code:

from textblob import TextBlob
text = "I have been taking some Python courses at TekZone Academy and I really like it"
blob = TextBlob(text)
print(blob.sentiment)

#output should give you something like: "Sentiment(polarity=0.2, subjectivity=0.2)"

If you get some errors, just make sure the textblob module is installed. You can easily do this by typing the command “pip3 install textblob” or “pip install textblob.” That’s it. You can play around with various text and see how the output changes. You can explore more functionality of the TextBlob by reading the page provided above.

Now you can start analysis emails and text from your friends. In a future post, we will look at how to use another nice plugin to automatically grab text from web pages. Those two modules combined can be quite powerful.

I’ll leave you with this article where you can find a bit more about applications of sentiment analysis: https://medium.com/manishmshiva/a-complete-guide-to-sentiment-analysis-and-its-applications-72adb3b057f5

Posted on Leave a comment

Getting Started with Artificial Intelligence – Resources

Courses

The Machine Learning course offered by Standford through Coursera is a great way to start. It provides details behind the Machine Learning concepts and provide everything needed to practice it. The course goes through the math behind AI and ML. You can access it for Free here: https://www.coursera.org/learn/machine-learning . Millions of people already have.

Another very interesting course for a newby is Udemy’s Pyton for Data Science and Machine Learning Bootcamp. This course does 2 things: teach you Python and how to apply the language to Machine Learning concepts. This course is not free, but it’s well worth the $10 I paid for it. You can access it here: https://www.udemy.com/course/python-for-data-science-and-machine-learning-bootcamp/

Podcasts

The Machine Learning Guide podcast is a great one for a newbie. While it covers some of the technical details of ML, the main objective of this podcast is to cover the forest that is ML. It covers they why behind each concept including when it should be used and why. You can access it for free a the following link or on your favorite podcast app. http://ocdevel.com/mlg

The Emerj AI in Industry podcast helps connect typical industry challenges with corresponding AI fields. I recommend it whether you’re starting or are deep into your AI journey. https://emerj.com/

Books

The book Machines of Loving Grace presents a very good history of AI from the early 1900s to the recent boom in this field. It helps understand current trends. I highly recommend it.