Speech to text conversion using python followed by sentiment analysis using TextBlob

Sarthak Rout
3 min readSep 26, 2019

--

Speech recognition is deeply rooted in research done at bell labs in the early 1950s. It has come a long way since then and is now available in many of our daily use electronics like smartphones, Google home, Amazon Echo, etc.

Most modern speech recognition systems rely on a Hidden Markov Model (HMM) which works on the assumption that if a speech signal is viewed on a short enough timescale then it can be viewed as stationary process, i.e, a process whose statistical properties don’t change over time. Neural networks are used to simplify the speech signal using techniques for dimensionality reduction and feature transformation before HMM recognition. Voice activity detectors are used to reduce audio signals to only include portions which have voice in it. This saves computation power required and time.

Here I’ll not go into those intricacies and build a simple speech recognizer using SpeechRecognition library.

It can be installed from a terminal using pip:
pip install SpeechRecognition

To access audio using microphone PyAudio is necessary. It can be installed in the following ways:

For windows-
pip install PyAudio
or, if using conda,
conda install -c anaconda pyaudio

For Linux-
sudo apt-get install python-pyaudio python3-pyaudio

Now afterwards it would just take a few lines of code to build a simple speech to text converter:

Speech to text conversion implementation in a few lines of code
Results obtained

The speech now converted into text can be further used for other purposes like sentiment analysis, etc. Sentiment analysis is the process of identifying and categorizing opinions using NLP to determine whether the user’s/speaker’s/writer’s attitude towards a particular topic/ product is positive, negative or neutral. It can be done easily with TextBlob library. It’s very slow but gets the job done. For real world use nltk/spacy, etc should be preferred.

Here’s a simple sentiment analysis using TextBlob library:

Speech to text followed by sentiment analysis
Sentiment Analysis using TextBlob

Summary:
In this article, we were able to create a simple implementation of audio to text conversion using SpeechRecognition library followed by its sentiment analysis using TextBlob. I hope you enjoyed reading the article. Have fun :)

References:

--

--