Photo by Sergey Arianov

Make Your Bot Recognize the Intent of Human Language Inputs

Yuli Vasiliev
Level Up Coding
Published in
4 min readApr 20, 2020

--

The samples of natural language are typically contextual and mostly driven by intention. So, as a chatbot developer, your primary tasks are to ‘teach’ your bot how to understand the intent and context of what a user says. In my previous article, I touched upon how you can make your bot recognize the context of a sentence being processed, finding the antecedents for substitutes in the previous discourse. In this article, I’ll share some tips on how you can make your bot recognize the intent behind a sentence, using syntactic dependency labels and named entities.

An Example of a User Request

Whether your chatbot is used for food-ordering, ticket-booking, or taxi-ordering, it needs to understand a customer’s intent to take an order correctly. As an example, consider the following request that might be submitted to a ticket booking chatbot application:

I want to book two tickets to Thailand tomorrow morning.

After processing, the above sentence should be shredded into the structure that might look as follows:

Intent: bookTicket
Quantity: two
Destination: Thailand
Date: tomorrow
Time: morning

To implement this with a NLP tool like spaCy, you’ll need to employ two techniques based on syntactic dependency parsing and named entity recognition, as explained in the next sections.

Syntactic Dependency Labels

The syntactic dependency parsing is the key technique to rely on when it comes to intent extraction from a sentence. spaCy has a ­syntactic dependency parser that reveals syntactic relations between individual words in a sentence, connecting syntactically related pairs of words with a single arc. The following script illustrates how to derive syntactic arcs from the sample sentence:

import spacy
nlp = spacy.load('en')
doc = nlp(u'I want to book two tickets to Thailand tomorrow morning.')
for token in doc:
print(token.text, token.dep_, token.head.text)

This should give you the following output:

I         nsubj     want
want ROOT want
to aux book
book xcomp want
two nummod tickets
tickets dobj book
to prep book
Thailand pobj to
tomorrow compound morning
morning npadvmod book
. punct want

Pay attention to the arc connecting ‘book’ with ‘tickets’ (highlighted in bold). In this syntactically related pair, word ‘tickets’ is the direct object as indicated from the ‘dobj’ dependency label assigned to this word.

Navigating the Dependency Tree For Intent Extraction

The head word in this same relation is verb ‘book’, which is the transitive verb of the sentence. In most cases, the transitive verb/direct object pair, if it can be found in a sentence, best describe the intent behind that sentence. The following code snippet shows how this syntactic pair can be extracted from a sentence:

doc = nlp(u'I want to book two tickets to Thailand tomorrow morning.')
for token in doc:
if token.dep_ == 'dobj':
print(token.head.text+token.lemma_.capitalize())

Be warned though, sometimes a sentence that expresses an intent may not include a transitive verb/direct object pair, as in the following example:

I need to sign up for a cloud service.

In this example, the action verb ‘sign’ is connected to the noun ‘service’ (to which the action is applied) via the preposition ‘for’.

Finding the Necessary Details with Syntactic Dependencies

Exploring the dependency relations within a sentence can be useful not only when it comes to intent recognition. In our particular example, you may search for the quantity of tickets to book as a number modifier of the direct object, as illustrated in the following snippet:

doc = nlp(u'I want to book two tickets to Thailand tomorrow morning.')
for token in doc:
if token.dep_ == 'dobj':
print('Intent: ', token.head.text+token.lemma_.capitalize())
print('Quantity: ', [t.text for t in token.lefts if t.pos_ == 'NUM'][0])

which should give you the following output:

Intent:   bookTicket
Quantity: two

As you might guess, this is a simplified implementation. In the real-world code, you’ll have to proceed from the fact that a user may use another transitive verb, say:

I’d like two tickets to Thailand tomorrow morning.

This example illustrates that the direct object can be more important than its transitive verb when it comes to intent recognition. You may argue that the word that acts as the direct object in a sentence may also have synonyms, say:

I’d like two seats to Thailand tomorrow morning.

To address the problem, you might use the list of synonyms.

Extracting the Details with Named Entities

Now that you have extracted the intent of the sample, you still need to collect some details, such as the destination, date, and time. In spaCy, this can be accomplished with the named entity recognizer. In the simplest form, this might be implemented as follows:

doc = nlp(u’I want to book two tickets to Thailand tomorrow morning.’)for ent in doc.ents:
if ent.label_ == 'GPE':
print('Destination: ', ent.text)
if ent.label_ == 'DATE':
print('Date: ', ent.text)
if ent.label_ == 'TIME':
print('Time: ', ent.text)

producing the following output:

Destination:  Thailand
Date: tomorrow
Time: morning

You may still need to turn ‘tomorrow’ into a certain data. This is where the datetime library may come in handy.

--

--