NLP and ML for Your Bot

Yesterday, we went through how to draft the overall flow and dialog for your bot. Today, we’ll examine how natural language processing (NLP) techniques and machine learning (ML) can be applied to your bot.

Natural language processing for bots

The premise of NLP is to help the machine better understand human language through processing this language in the right way. A complex field ranging from parsing text to summarizing information, NLP is increasingly important to bots for information retrieval (IR), question answering (QA), and dialog management.

If you have a PhD in NLP, you’re probably busy beating benchmarks in one of these areas—and writing lots of experimental Python code (we do this occasionally when we are struck by a novel idea). Practically speaking, though, most developers these days rely on a few NLP packages to help them manage the dialog they have crafted. The most popular services are Wit.ai (acquired by Facebook), api.ai (acquired by Google), and LUIS (by Microsoft).

All these services provide an easy way to train if-then scenarios based on what a user says and what the anticipated answer should be. They have user interfaces and easy integrations with Messenger bots, so you can easily plug and play one of these and test the performance.

To go beyond using an external service, you can start by looking at text preparation services (SpaCy, for example), classic word embedding models (like word2vec), topic modeling (LDA), or classification models (using open-source frameworks like fastText). Usually, experimentation is necessary to find the right approach to your bot, and getting it to understand user input consistently can require significant tuning. A full explanation of how it all works will take a long time and so is outside the scope of this course, though I highly encourage you check out videos on the topic if you’re really interested.

Machine learning for bots

For the NLP portion of ML, knowing what users have asked—and asking them to confirm if the bot got it right—will help to generate supervised training. It’s basically how people learn; when someone reinforces that they have done something right, they are more likely to do the same thing the next time. (Something like this is already handled by the NLP services listed above.)

Personalizing content using machine learning may be one of the most useful applications for bots. Basically, as a variant of a recommender engine, you capture what your users have done (text typed, buttons pressed, content interacted) and their profile (gender, location, etc.) to personalize what content you provide to them.

ML for bots is all about setting up the right information architecture to actually do something about the interaction data that you collect. For example, if you don’t capture all interactions that someone has had with your bot, it’s pretty hard to meaningfully segment them based on their history with your bot. Hence, it’s advisable to think about what exactly you want to learn from your bot users and explicitly make these information choices when you start to build your bot.

After you set up your ML engine (such as on AWS or Azure), you can package these as an API that your bot can call as users interact with it. An example could be a user asking for recommended stories by pressing a button—your bot will call this API, which has input from a table of all the interactions the user has had with it, and return content that it believes will best suit the user’s preferences.

Today was just a primer on the possibilities of NLP and ML for bots! Look for more resources if you’re interested in these fields, and in the meantime, look forward to tomorrow, where we figure out how to integrate workflows into your bot for greater functionality.