Text Analysis with Python – Start with Sentiment Analyis

Text Analysis with Python – Start with Sentiment Analyis

Businesses receive text data non-stop (emails, chats, product reviews, etc.), and all this unstructured data contains valuable insights that you can use to make decisions about your products or services. The problem, however, is that analyzing text data manually takes a serious amount of time.

Thankfully, there are advanced tools like text analysis with Python that can help you transform your data into meaningful insights, quickly and at scale.

In this post, discover we’ll quickly go over what text analysis is, how to use text analysis with Python, and all the necessary steps to create your own custom sentiment analysis model.

What Is Text Analysis?

Text analysis is the automated process of extracting and classifying text data using machine learning and natural language processing. . Analyzing these texts by hand is time-consuming, tedious, and ineffective – especially if you deal with large amounts of data every day.

Walmart, for example, receives 200 billion rows of transactional data in just a few weeks! Imagine if they wanted to process all this data manually: it would be impossible. Text analysis tools, on the other hand, and can analyze hundreds and thousands of pieces of data in just a few seconds.

There are different text analysis techniques you can run on your data, such as sentiment analysis, topic classification, urgency detection, and intent categorization.

In the tutorial that follows, we’ll show you how to perform sentiment analysis with Python.

Tutorial: How to Use Text Analysis with Python

Python is the most popular programming language today, especially in the field of scientific computing, as it is a highly intuitive language when compared to others such as Java. It is more concise, so it takes less time and effort to carry out certain tasks. Finally, the syntax and code readability make it efficient, easy to process, and easy to learn. All these perks make Python the perfect option to build a machine learning model for text analysis.

You might opt for open source libraries, such as Scikit-learn or NLTK, for example. Some other libraries include SpaCy (its API is simple and productive), Keras (a machine learning library with a focus on enabling fast experimentation)), TensorFlow (for using deep learning for analyzing text), or PyTorch (another library used for building deep neural networks for NLP).

However, building a text analysis model from scratch is not easy. You will need to spend time and resources building the necessary infrastructure to run the model, train it, text it, and start all over again until it can be put to use. .

SaaS tools can make your venture into text analysis a lot simpler . MonkeyLearn, for example, is a simple but powerful text analysis platform that provides ready-to-use text analysis tools, as well as an API that can be used in Python.

For example, this is how you make an API request to MonkeyLearn’s sentiment analysis model:

from monkeylearn import MonkeyLearn

ml = MonkeyLearn(<<Insert your API Key here>>)
data = ["This is a great tool!"]
model_id = 'cl_pi3C7JiL'
result = ml.classifiers.classify(model_id, data)
print(result.body)

The API response for this request will look like this:

[
  {
    "text": "This is a great tool!",
    "external_id": null,
    "error": false,
    "classifications": [
      {
        "tag_name": "Positive",
       "tag_id": 33767179,
        "confidence": 0.998
      }
    ]
 }
]

Create Your Own Text Analysis Model

Now, pre-trained models are great to start with. They will give you an idea of how machine learning works and how you can get insights out from your texts. However, if you are looking for true accuracy then you should build your own model. As you will be the one defining the tags and training your model with relevant samples, you’ll get better results. 

Let’s take a look at how to build a custom sentiment classifier. We’ll train a model that is able to automatically classify reviews from a SaaS into Positive and Negative. Don’t worry, it’s easy and you’ll be able to integrate your model’s API with Python in no time.

1. Choose Model Type

Access your dashboard and click 'create model' in the top right-hand corner of the page. Then, choose ‘classifier:

The option to select model in MonkeyLearn's wizard creator.

In the following screen, choose the ‘sentiment analysis ’ model:

The option to select classification type in MonkeyLearn's wizard creator.

2. Upload training data

Now, you’ll need to import your data. You can upload a CSV or Excel file with text examples. For this tutorial, we are going to upload a CSV file with a set of reviews from a SaaS company.

Import data in MonkeyLearn's wizard creator.

3. Train your model

Now comes the most important part of creating your model: you have to train it! This means you have to accurately tag as many samples as possible. The model will learn the patterns and criteria you use for categorizing a review as Positive or Negative. The more data you tag, the smarter your model becomes:

Training model in MonkeyLearn's creation wizard.

When you have reached a certain number of samples, your model will start making accurate predictions. 

5. Try your model out

You just need to write something in the text box to see how well your model works:

Test with your own text

Results

TagConfidence
Negative99.9%

If your model needs to improve its predictions, you just have to tag more samples. 

6. Call the API with Python

Finally, you can use our API with Python to integrate the model with your apps. You can do this with a few lines of code:

from monkeylearn import MonkeyLearn

ml = MonkeyLearn('<<Your API key here>>')
data = ['Customer support team is great, super helpful!', 'The UI is super confusing']
model_id = <<Insert model ID here>>
result = ml.classifiers.classify(model_id, data)

print(result.body)

The API will return a response with the result:

[{
    'text': 'Customer support team is great',
    'classifications': [{
        'tag_name': 'Positive',
        'confidence': 0.944,
        'tag_id': 33767179
    }],
    'error': False,
    'external_id': None
}, {
    'text': 'The UI is super confusing',
    'classifications': [{
        'tag_name': 'Negative',
        'confidence': 0.951,
        'tag_id': 33767178
    }],
    'error': False,
    'external_id': None
}]

Wrap-up

Analyzing your texts manually is a drag. Not only is it time-consuming, but Analyzing your texts manually is time-consuming, ineffective and tedious. Human agents can only process a certain amount of information, no matter how hard they work. That is why text analysis with AI is essential for businesses – it allows teams to focus on more relevant and motivating tasks, and helps extract valuable and accurate insights in real time.

Using text analysis with Python will save you a lot of time and resources, especially if you use SaaS tools such as MonkeyLearn instead of building a solution from scratch.

Start using MonkeyLearn’s pre-trained models right away or request a demo if you’d like to know more. Our team is ready to answer any questions you have!

Federico Pascual

October 9th, 2019

Posts you might like...

MonkeyLearn Logo

Text Analysis with Machine Learning

Turn tweets, emails, documents, webpages and more into actionable data. Automate business processes and save hours of manual data processing.

Try MonkeyLearn
Clearbit LogoSegment LogoPubnub LogoProtagonist Logo