For Day 3 of this series, I wanted to start diving into an application of Machine Learning. This has long been one of my favorite topics in Computer Science. For this post I wanted to touch upon Natural Language Processing. It’s a field of artificial intelligence (AI) that focuses on the ability for machines to manipulate Natural Language and derive meaning from it. It probably sounds badass because it is. Sentiment Analysis with TextBlob In particular I want to focus on Sentiment Analysis and mashing that up with Email. In this blog post I’m going to create a quick Python Flask application with the SendGrid Parse Webhook and a Python Lib called TextBlob (http://textblob.readthedocs.org/en/dev/) that will get the sentiment of every incoming email and respond back accordingly. You can imagine if this email address were to be the first responder when a customer emails in for support on your current product. It could email back a different, unique response if the message is positive or negative. Requirements: SendGrid account to get incoming & send outgoing emails with your app (www.sendgrid.com/free) SendGrid Python Lib $ pip install sendgrid Flask: $ pip install flask Textblob: $ pip install -U textblob $ python -m textblob.download_corpora Sample Code: from flask import Flask, request from textblob import TextBlob import sendgrid app = Flask(__name__) sg = sendgrid.SendGridClient('username', 'password') @app.route('/incoming',methods=['POST']) def besmart(): email = request.form['from'] subject = request.form['subject'] incoming_body = request.form['text'] send_from = request.form['to'] #Get the sentiment of the body blob = TextBlob(incoming_body) sentiment = blob.sentiment #Based on Sentiment send back a different email #polarity > 0 is positive and less is negative if sentiment.polarity > 0: body = "Thats good! We appreciate the feedback. Expect another response from a support rep soon" elif sentiment.polarity < 0: body = "We're sorry to hear that!. We just forwarded your request to the appropriate person in support, expect a response shortly" else: body = "We are forwarding your question to the appropriate person in support, Will get back to you shortly" new_sub = "Re: "+subject #Send back that return email message = sendgrid.Mail() message.add_to(email) message.set_subject(new_sub) message.set_html(body) message.set_text(body) message.set_from(send_from) status, msg = sg.send(message) return "OK" if __name__ == '__main__': app.run() In Action Below are two images of the above code in action. The first image shows the scenario of a happy customer and the second of a fictitious upset customer. Over the next few weeks, I’m going to continue writing about different technologies, so check back every Monday, Wednesday, and Friday for new tech posts! You can also visit my intro post that will have an updated list of everything I’ve written.