Mood predictor using Naïve Bayes classifier

Here, I elaborate about a short project which I pulled over last few days. I was revising some concepts in Machine Learning, when this idea struck me. This software deals with predicting the mood of the person based on what he/she is talking. The user would be speaking into the microphone of the device and the output would be the sentiment; positive, neutral or negative. I have used a Naïve Bayes classifier to form my prediction model. Unfortunately, I could not provide a working demo since I am using a shared hosting and the company doesn’t have nltk or python 2.7+ installed.


Using the microphone of the device, the user says the sentence. This is then processed by using the Web Speech API( which identifies the words spoken and converts them into text. This is done in Javascript. The script written was included in the html code. Not much fancy css has been added. Just a submit button to send the text further.


Now we click on the mic button and say the sentence.


Once we click the submit button, this text is passed to the python file in the backend. I decided to send it as a command line argument using exec function in PHP. Other methods may include writing to a flat file and then processing, and so on. But since I had to analyze sentences few words long, I found flat file method not a good way.

In my python code, I have placed checks to strip off any punctuations, undesirable words etc., so that if the user tries to enter something malicious, it can be taken care of. This list of undesirable words also contains words such as ‘the’, ‘a’, ’is’ and so on, which play little role in determining the sentiment of the speech. Hence, these are removed too. A training corpus was saved as a csv file in the directory. The processed sentence is then used as a test input in a Naïve Bayes classifier to tell if it is a neutral, positive or negative speech.


Once we get to know the sentiment of the text, we can modify the response to suit the same. This could help in online chat bots or even blind chatting sites where one would like to combine two users based on what kind of emotion they are in and other parameters.

Future Work:

Expand the sentiment from neutral, positive and negative to happy, sad, distressed, euphoric and neutral.

Try to include a sarcasm level. This would be a long term goal, and I guess a challenging one too, since it would vary person to person. I have something like, “sarcasm percent: 80%” in my mind, similar to one in the movie Interstellar. This sarcasm feature vector could be set against a word (like ‘awesome’) or a group of words (‘what a lovely day’) and then our model would learn and predict accordingly.

I plan to carry this project further and analyse it from some more angles not discussed while incorporating some more features. I would read more on Pattern recognition and Natural Language processing. Two books for the purpose I have ordered are; ‘Pattern Classification (English) 2nd Edition’ by Richard Duda, Peter Hart, David Stork and Speech and Language Processing: An Introduction to Natural Language Processing, Computational Linguistics and Speech Recognition by Jurafsky

If the illustrations don’t help and you would like a video demo, I would be happy to record one. Let me know in the comments below.


4 thoughts on “Mood predictor using Naïve Bayes classifier

  1. Interesting project. I skimmed through the text to find the lines explaining where the actual “magic” happens – how text is analysed/represented as mood. Forget the part where the whole speech to text and the web request is involved. Could you elaborate a little more on the step I mentioned above, that is the machine learning methodology used. Im from a non-NLP background, and find this a little hard to digest although I find it very much interesting.

    • Hi Rajsri!
      Thank you very much for going through the article.
      In some machine learning techniques we provide a training sample to our algorithm. Consider it as giving a bunch oranges, apples, guavas and bananas to a child and teaching her how to identify a red coloured round object called apple.
      Similarly, I gave a training input to my machine learning model which works on the principle of calculating probability of an oocurence based on earlier outcomes (Naïve Bayes theorem). The training input I gave was something like:
      |negative||I have a bad headache today|
      |positive||That is a beautiful dress|
      Based on such training inputs, the algorithm would classify the test sentence “It is a beautiful day” as a positive sentiment or a negative sentiment. To assist the algorithm, I stripped off the sentences from useless words like a, an, the, this and so on before passing for prediction. Hence, the algorithm would essentially work on verbs and adjectives to pick out the mood.

      Hope this helps.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s