Chatbot for customer support

These days there are many use cases for conversational AI. Conversational AI refers to the use of messaging apps, speech-based assistants and chatbots to automate communication and create personalized customer experiences at scale. In this article, you will learn about the chatbot.

In the following project, we go through implementing a chatbot using Java and Apache OpenNLP.

Apache OpenNLP

Apache OpenNLP is an open-source Java library that is used to process Natural Language text. Apache OpenNLP provides various services such as

  • Tokenization
  • Part of speech tagging
  • Sentence segmentation
  • Chunking
  • Parsing
  • Named entity extraction
  • Co-reference resolution

How chatbot works

Firstly, the user sends a message and chatbot will reply, but how it works? A chatbot can be used for different scenarios based on business requirements. The most common use cases are customer support, search to buy and sell, booking.

In the chatbot project that I worked on, there were two main scenarios for the chatbot, one customer support and another one search an item. I will cover the chatbot for customer support in this article and search for an item in another article.

Customer support scenario

In this scenario, the user asks the chatbot for reporting an issue or for help on how to work with the website. Based on our business needs, we had multiple scenarios to cover. Therefore, we needed to reply to the user correctly based on the type of questions. Here are two main steps we took to provide the best reply to user message

1. Training the data

Here are some of the examples of categories we covered:

  • SCAM_LOSTMONEY Didn't pay for it
  • SCAM_LOSTMONEY Didn't ship
  • SMS_FRAUD got a weird SMS
  • SMS_FRAUD is this a fake reply
  • SMS_FRAUD received a strange message
  • REPORT_AD against policies
  • REPORT_AD violating policy
  • ACCOUNTS I am not able to sign in
  • ACCOUNTS I can't login

To train the data, we used DocumentCategorizerMe. DocumentCategorizerMe gets a txt file and generates a training set in .bin format. Here is the code:


final Optional trainingFile = getTrainingFile(“model.txt”);

final DoccatModel model = trainAndGetCategoryModel(trainingFile.get(), 3);

final InputStreamFactory factory = new MarkableFileInputStreamFactory(convertPathToTempFile(trainingFile));

final ObjectStream lineStream = new PlainTextByLineStream(factory, "UTF-8"

final TrainingParameters parameters = ModelUtil.createDefaultTrainingParameters();
parameters.put(TrainingParameters.CUTOFF_PARAM, Integer.toString(trainingCutoff));

this.categoriser = DocumentCategorizerME.train(
        "en", new DocumentSampleStream(lineStream),
        new DoccatFactory()

2. Scoring the category

It’s required that chatbot provides the best reply to users. Due to multiple categories to cover the customer support message, we needed to make sure that the chosen category has the highest confidence. In the following code shows how to score the category using DocumentCategorizerMe.scoreMap()

final Optional> result = this.categoriser.scoreMap(inputMessage).entrySet()
        .filter(e ->, scoreThreshold) >= 0)
        .max((e1, e2) -> e1.getValue().compareTo(e2.getValue()));


Based on the above code, if the user message is “Is this a fake message?”, the highest score should be for SMS_FRAUD and not the REPORT_AD. And chatbot can simply reply to the user.

This was only one use case of chatbot.

Share post:

Follow Us On:

Receive Our News and Announcements

*Stay up to date with events, blogs and announcements.

Copyright ©2020 Taikera.