Real-Time Sentiment Analysis of a Phone Call Using NLTK and TextBlob in Python

Published by Aarya on

Speech to text conversion and real-time sentiment analysis

In this project we are going to analyse the sentiment of the call. We are first going to convert the speech to text and the analyse the sentiment using TextBlob.

TextBlob is a Python library for processing textual data. It provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more.

!pip install textblob

NLTK is a leading platform for building Python programs to work with human language data. It provides easy-to-use interfaces to over 50 corpora and lexical resources such as WordNet, along with a suite of text processing libraries for classification, tokenization, stemming, tagging, parsing, and semantic reasoning, wrappers for industrial-strength NLP libraries, and an active discussion forum.

!pip install nltk
import nltk

Installing nltk does not install everthing in nltk. We will have to download somethings separately. We are going to download punktaveraged_perceptron_tagger and brown.'punkt')'averaged_perceptron_tagger')'brown')
True opens a GUI by which you can view the packages which are already downloaded and even update or download new packages manually.

Now we are going to import TextBlob and and create its object. We can see that tb is an object of TextBlob.

from textblob import TextBlob as blob
tb = blob('Hi, please like this post!')
TextBlob("Hi, please like this post!")

help(tb) will give you a list of all the functions which are available for tb. We will try some of them.

tags returns a list of tuples of the form (word, POS tag). It is used to get the various parts of speech of the sentence.

  • NNP means proper noun, singular
  • NN means noun, singular
  • DT means determiner
  • IN means preposition/subordinating conjunction
[('Hi', 'NNP'),
 ('please', 'NN'),
 ('like', 'IN'),
 ('this', 'DT'),
 ('post', 'NN')]

noun_phrases returns a list of noun phrases.


sentiment returns a tuple of form (polarity, subjectivity ) where polarity is a float within the range [-1.0, 1.0] where -1.0 is very negative and 1.0 is very positive and subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective.

Sentiment(polarity=0.0, subjectivity=0.0)

Let’s try another example. Here the polarity is 0.4583 which indicates a positive sentiment.

tb = blob('I love this channel. There are many useful posts here!')
Sentiment(polarity=0.4583333333333333, subjectivity=0.3666666666666667)

Real-Time Voice Recording

To install the necessary packages you can run the following commands in anaconda in the administrator mode:-

pip install SpeechRecognition

conda install pyaudio

For detailed explanation of the code you can refer the following video-

import speech_recognition as sr

After importing speech_recognition we are going to convert audio from our microphone into text. For this we are going to use recognize_google(). As timeout=2 it will stop listening if there is no audio for 2 seconds. We are displaying the text and its sentiment at the end. For the example below we have got a negative polarity which indicates that the sentiment is negative.

r = sr.Recognizer()
with sr.Microphone() as source:
    print('Say Something...')
    audio = r.listen(source, timeout=2)
        text = r.recognize_google(audio)
        tb = blob(text)
        print('Sorry... Try again')
Say Something...
these people are really very poor and I am going to kill everybody I I main they don't deserve to live in this country and then either deserve to live on the planet Earth
Sentiment(polarity=-0.02015151515151517, subjectivity=0.5283333333333333)

Now we are going to run the same piece of code 10 times.

iter_num = 10
index = 0
    with sr.Microphone() as source:
        print('Say Something...')
        audio = r.listen(source, timeout=3)
            text = r.recognize_google(audio)
            tb = blob(text)
            print('Sorry... Try again')
        index = index + 1
Say Something...
hello hi baby what's up I am missing you so much
Sentiment(polarity=0.0, subjectivity=0.125)

Say Something...
do you know I love you so much and have anyone told you that you are the one of the most beautiful girl in the world
Sentiment(polarity=0.5125, subjectivity=0.575)

Say Something...
ok so have you are you done with your dinner are you going to have your dinner
Sentiment(polarity=0.5, subjectivity=0.5)

Say Something...
Informatica ok one thing do you know we we have a lot of the things common in between us we like like romantic movies and and you books except Raso these are the really great things between us
Sentiment(polarity=0.25, subjectivity=0.5625)

Say Something...
yes you are right my apology but do not there to tell me again otherwise I'll kill you also that you ok I also you with the gun in your head and in your heart and don't dare to talk to me like this ever
Sentiment(polarity=0.39285714285714285, subjectivity=0.5178571428571428)

Say Something...
but still I love you so much I hate you don't talk to me like this and I'll never call you back
Sentiment(polarity=-0.10000000000000002, subjectivity=0.5)

Say Something...
online now you see here in this sentence when I say that but still I love you so much I hate you and don't talk to me like this and I'll never call you now you can see here there is a negativity in this sentence and it is saying that yes there is any creativity in this sentence super have some 123456 and the 7th 7th time running so what I am talking here its Guna of course printed so let's get it printed and then we'll talk again
Sentiment(polarity=0.01111111111111109, subjectivity=0.7222222222222222)

Say Something...
Sorry... Try again

Say Something...
kidding and please do not mind I love you 3000 even I love you goodnight
Sentiment(polarity=0.5, subjectivity=0.6)

Say Something...
ok now you see here so this is there is polarity at this the positivity Heera 20.5 so this is how you see here our phone call is being converted into word text by using this lesson tutorial you can go and watch speech recognition in Python speed detection in Python at KGP talking you can search and you can get this otherwise I have already given the link for this this listen here you can watch from here as well here
Sentiment(polarity=0.5, subjectivity=0.5)

As you can see for all the statements the polarity is displayed in real time.


Hi, I am Aarya Tadvalkar! Currently, I am pursuing Computer Engineering. I have a keen interest in Machine Learning and Data Science. I am always enthusiastic about learning new things and expanding my knowledge!

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x