How to Learn More from Your Customers with NLP


Natural language processing (NLP) is an area of computer science and artificial intelligence concerned with the interactions between computers and human (natural) languages, in particular how to program computers to process and analyse large amounts of natural language data. Challenges in natural language processing frequently involve speech recognition, natural language understanding, and natural language generation.

Given the above introduction and the title of this blog post, you may be asking yourself: why is NLP important or how can I benefit from it?

How Natural Language Processing is Redefining the Business World

If you have ever purchased something online (be it an item or a service), there is a good chance that you have been prompted to give some feedback about your experience with the party that offers the good. Be it a wall of text, a simple phrase or the traditional ⭐ rating — customer feedback is super important to every merchant.

If you are the merchant and use the traditional ⭐ rating, then I hope this makes you think about the way you ask your clients for feedback. In this example, I will show you a pratical way to use NLP, in which a program interacts and analyses the sentiments of an user after making a purchase.

In this “tutorial” (or, re-wording: use case), we will be using the TextBlob library.

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.

from textblob import TextBlob

We have imported the TextBlob class from the textblob module. Since I want this to serve as a simple usage example, I’m going to get feedback from a customer using an unsophisticated approach (but still trying to make as much sense as possible).

feedback = TextBlob(input("Thanks for your purchase! Feedback: "))

Using the input function, we get the user’s feedback. We will then incorporate the input into the TextBlob class.

analyse = feedback.polarity

polarity returns the polarity score as a float within the range [-1.0, 1.0]. The closer to -1.0, the more negative; the closer to 1.0, the more positive the feedback is. We’ll store the result of the feedback analysis in a variable called analyse.

We can now print analyse to the console.


Let’s save the file. Call it or whatever you find more convinient — it doesn’t matter much. At the end, your code should look like this:

final code

Open a terminal window and run py I bought a product and I really enjoyed it, so I will give it a nice review; if you remember what I wrote about regarding polarity, then you would’ve guessed by now that the expected output will be close to 0.6. The experience will be similar to this: positive I got a polarity of 0.6, and that’s a positive score and relatively close to 1.0.

Let’s try to write a really bad review now. This time around, if we express our frustration, we’ll get a negative score. negative A score of -0.5049999999999999 is pretty negative, I’d say!

Then it’s just a matter of how you want to handle the situation. Let’s say that you want to offer the costumer a free discount code in your store if he writes a negative review. We’ll have to write a bit of code in addition to what we had before. Add the following lines below the code you already have:

if analyse <= 0:
    print("We are sorry for your bad experience with our store.\nTo compensate, we are offering you a coupon code with 10% off your next purchase.\nCoupon code: cOOUhaAo79")

    print("Thanks for your feedback!")

We’ll run two experiments: one positive review and one negative review. positive Positive review.

negative Negative review.

I hope I could shine some light about how important Natural Language Processing can be in order to get feedback from clients and offer a better service. The possibilities are endless, and this was a very simple example case — you can do a lot of data analysis with NLP libraries (like TextBlob, used here, or NLTK) and that could definitely help your business grow in several different aspects.

On other news, I plan to write blog posts more often.