1. Incremental slot filling and negation
Now we'll work on two topics which are really important for
the user experience of your hotel booking bot, filtering results incrementally, and handling negation
2. Incremental filters
The first part is to add some memory to your bot, so that your users can narrow down their search incrementally.
The process of collecting the users preferences during a conversation is called slot filling.
Here's an example:
User: "I'm looking for a cheap hotel in the north of town"
Bot: "I'm sorry, I couldn't find anything like that"
User: "ok what about mid range ones"
Bot: "X is a mid-priced hotel in the north of town"
It would be annoying for your user if they had to specify the northern part of town
again in the second message. SO how do we do this?
3. Basic memory
To add a basic version of this memory, we can save parameters in a dictionary, so long as its defined in the right scope. The params variable should be outside the scope of the respond function, and passed to it each time a message comes in. We modify the respond function to return both the response message and an updated parameter dictionary. The saved parameters include the user's preferences, for example if they've already said that they're looking for somewhere cheap, we want to save pricerange, lo in the params dict.
One tricky thing can be deciding when to 'wipe' the memory of your bot and let your users
start over. You'll have to choose what makes sense for your application.
4. Negation
A natural thing you'll also see is that people will tell you what they *don't* want.
This especially happens in response to suggestions that your bot makes.
Here's another example:
"where should I go for dinner?"
"what about Sally's Sushi Place?"
"no I don't like sushi"
"ok, what about Joe's Steakhouse?"
5. Negated entities
In these exercises you'll use a super simple approach that works for the most obvious cases your bot is likely to see, but is in no way a general approach. In general, negation is a tricky topic in NLP, we'll only go for the low-hanging fruit here.
You will split
up your sentence into phrases, and see which entities have the word 'not' in front of them. We'll also look for the contraction "n't".
This catches simple negations like "not sushi", "I don't want sushi", but would fail
on cases like "I would rather skip dinner than eat sushi", "I hate sushi", etc.
6. Catching negations
Here's an example, we start by creating a spacy document from the phrase "not sushi, maybe pizza?"
We create a list called indices, which contains the index of the tokens that correspond to the entities, in this case sushi is the second token in the document, and has index 1 because lists are zero indexed.
We initialize two empty lists, one for normal entities, and one for entities which we think are negated.
We then iterate over the entity indices. We extract the part of the phrase just before the entity, starting from index zero for the first entity, and afterwards starting from the previous entity. We can recreate the phrase by using the format method and indexing the spacy document. We then use an if else statement. If the word 'not' appears in this part of the message, we assume this entity is negated, and we add it to the negated entities list by using the list's append method. Otherwise we add it to the list of regular entities in the same way.
7. Let's practice!
Now it's your turn to implement slot filling in your virtual assistant.