15 Feb 2018

Introduction

It's amazing how the use of bots has become an everyday reality. Bots are everywhere. According to Incapsula Report about 51.8 % of whole Internet traffic in 2016 was generated by different types of bots - pretty impressive,huh? Whether we want it or not - bots affects reality and not only the virtual one. “Are they capable of influencing on people perception of reality?” - one may wonder. I think the answer is: yes! They may even force a certain pattern of behaviour. Let me give you an example: Bloomberg has recently published an article in which they report that the Russian Bots Retweeted Trump's Twitter 470,000 Times. It wouldn’t be totally crazy to think that it may have at least small influence on the election, right?


Simplified bots equals simplified implementation 

The company Customers Service costs can be reduced to minimum by having a properly implemented bot. It can also significantly increase sales when applied to existing e-commerce system. There are so many benefits of using bots that it’s quite obvious why they become more and more popular. Nevertheless, when I talk to developers about building conversational bots, they start to panic. I think people don’t like to be bothered by AI subjects, mainly because they wrongly assume that the problem is extremely complicated. This is totally incorrect! Technologies and tools which make working with bots fun are created every day. One of these powerful tools is the Microsoft Language Understanding Intelligent Service (LUIS).


What is LUIS and how it simplify creating bots

LUIS is one piece of the whole spectrum of Microsoft Azure cognitive services. According to dedicated website it is “A machine learning-based service to build natural language into apps, bots, and IoT devices”. LUIS can effectively recognize essential information from certain sentences. It detects users intents (what they want to say) and extracts possible entities (places, people, dates, things etc.).

Basic usage

To make better understanding of what LUIS is capable of, let’s analyze a simple case. We start with making a list of intents (take “BookTrainTicket” as example) and popular phrases that relate to this intent. Once this is done we can start talking with LUIS. Let’s say:

Book a ticket for the train to Warsaw on Tomorrow after 5 pm. 

Machine will return expected intent “BookTrainTicket” from our predefined list of intents. It will be accompanied with the following list of entities: 

DestinationCity: Warsaw, 
Datetime: ‘20.02.2018 17:00:00’

Sounds simple? It is simple. Using this tool significantly simplifies developing Bots process and allows developers to be focused on business logic instead of solving natural language analysis challenges.

Playing with Luis using build-in entities (datetime)

If you want to know more about LUIS check out this very well written Microsoft official documentation - here. We will have a look at some examples of how LUIS efficiently extracts entities from the natural language.

Building simple app

Let just play with what Microsoft gave us (use LUIS documentation for reference):

  1. Create New App 
  2. Create New Intent (RentACar)
  3. Add prebuild datetimeV2 entity from the entities panel
  4. Go back to the intent and add some sample sentences and questions (utterances) that people may use when the want to rent a car (eg. “I want to rent a car tomorrow”).
  5. Train and publish your app. After publish you will notice the url at the bottom - this is our app endpoint that allows us to pass query in http request (GET)

From this point, we can start playing with our newly created app. Let's just open our app endpoint in new tab and add following sentence to the end of query string:

I would like to rent a car on 24.02.2017

LUIS should respond with following entity data:

"entities": [
    {
        "entity": "24-02-2018",
        "type": "builtin.datetimeV2.date",
        "startIndex": 27,
        "endIndex": 36,
        "resolution": {
            "values": [
                {
                    "timex": "2018-02-24",
                    "type": "date",
                    "value": "2018-02-24"
                }
            ]
        }
    }
]

You can see that provided datetime was correctly detected. Well, this is not very impressive yet - we can implement the same feature with simple regular expression. But maybe let’s try and put another query, using everyday speech instead of date object.

Quering:

I want to rent a car tomorrow

Should give us something like below:

"entities": [
    {
        "entity": "tomorrow",
        "type": "builtin.datetimeV2.date",
        "startIndex": 21,
        "endIndex": 28,
        "resolution": {
            "values": [
                {
                    "timex": "2018-02-25",
                    "type": "date",
                    "value": "2018-02-25"
                }
            ]
        }
    }
]

It’s started to be a little bit more impressive - Microsoft LUIS correctly identified tomorrow as an entity and extracted datetime value. That’s nice but it’s still not something that couldn’t be done with small effort. 

Let’s try something more complex:

Please rent me a car this wednesday and next week

"entities": [
    {
        "entity": "this wednesday",
        "type": "builtin.datetimeV2.date",
        "startIndex": 24,
        "endIndex": 37,
        "resolution": {
            "values": [
                {
                    "timex": "2018-02-21",
                    "type": "date",
                    "value": "2018-02-21"
                }
            ]
        }
    },
    {
        "entity": "next week",
        "type": "builtin.datetimeV2.daterange",
        "startIndex": 43,
        "endIndex": 51,
        "resolution": {
            "values": [
                {
                    "timex": "2018-W09",
                    "type": "daterange",
                    "start": "2018-02-26",
                    "end": "2018-03-05"
                }
            ]
        }
    }
]

When I saw that for the first time I was pretty impressed. LUIS correctly transformed both - datetime from wednesday phrase and daterange from next week.

OK, so what about transforming holidays into datetimes? Let’s see:

I need a car for Thanksgiving

"entities": [
    {
        "entity": "thanksgiving",
        "type": "builtin.datetimeV2.date",
        "startIndex": 17,
        "endIndex": 28,
        "resolution": {
            "values": [
                {
                    "timex": "XXXX-11-WXX-4-4",
                    "type": "date",
                    "value": "2017-11-23"
                },
                {
                    "timex": "XXXX-11-WXX-4-4",
                    "type": "date",
                    "value": "2018-11-22"
                }
            ]
        }
    }
]


Again, Luis succeeded - it detected two datetimes because it couldn’t recognize if user meant previous or next Thanksgiving, however, we may simply select correct datetime in business logic.

So let’s try a little combo now :):

Rent a car on day after tomorrow , at christmas and next 29th of feb

"entities": [
    {
        "entity": "day after tomorrow",
        "type": "builtin.datetimeV2.date",
        "startIndex": 14,
        "endIndex": 31,
        "resolution": {
            "values": [
                {
                    "timex": "2018-02-26",
                    "type": "date",
                    "value": "2018-02-26"
                }
            ]
        }
    },
    {
        "entity": "christmas",
        "type": "builtin.datetimeV2.date",
        "startIndex": 38,
        "endIndex": 46,
        "resolution": {
            "values": [
                {
                    "timex": "XXXX-12-25",
                    "type": "date",
                    "value": "2017-12-25"
                },
                {
                    "timex": "XXXX-12-25",
                    "type": "date",
                    "value": "2018-12-25"
                }
            ]
        }
    },
    {
        "entity": "29th of feb",
        "type": "builtin.datetimeV2.date",
        "startIndex": 57,
        "endIndex": 67,
        "resolution": {
            "values": [
                {
                    "timex": "XXXX-02-29",
                    "type": "date",
                    "value": "0002-01-01"
                }
            ]
        }
    }
]

This one is tricky because we wanted the LUIS to not only understand that “29th of feb” is a date but also extract correct object. LUIS did not have issues with first part but it couldn’t return correct date value (29th exist only in leap year). Still, two other dates were correct.

Advanced entities

These were only very simple examples of what options LUIS gives us. We can create much more advanced Hierarchical or Composite entities. For instance, we can create the RentRequest entity which - apart of rental date - would also detect the type of car and the pickup location. Having this set up, we will cause the following query:

I would like to rent a van in july and pick it up from the airport

return this complex entity:

"compositeEntities": [
    {
        "parentType": "RentRequest",
        "value": "van",
        "children": [
            {
                "type": "CarType",
                "value": "van"
            }
        ]
    },
    {
        "parentType": "RentRequest",
        "value": "july",
        "children": [
            {
                "type": "builtin.datetimeV2.daterange",
                "value": "july"
            }
        ]
    },
    {
        "parentType": "RentRequest",
        "value": "airport",
        "children": [
            {
                "type": "PickupLocation",
                "value": "airport"
            }
        ]
    }
]

This is a result you can be really impressed with, right? :)


LUIS - a game changer?

With such complex and accurate results, developers do not need to be concerned or skilled in natural language processing - they can be fully focused on business logic implementation. LUIS not only gives us the tool to build our own entities related to our domain, but also provides a whole spectrum of predefined intents and domains that makes conversational bots creation even simpler. I hope that by showing you these simple examples I managed to convince - at least some of you - that developing bots is not that. 

AI Bots is the technology of the future - staying right next to IoT, cloud services or blockchains. Who knows, maybe a few years from now, each of us will have personal virtual concierge? And a moment later programmers will become a relic of the past? Well, hopefully not :)