Twilio
Chatbot
server
Google Maps
Knows about bus routes
Provides distances between points and address validation
Forwards text messages to our server
Has questions about bus routes
and of course lots of other options like
Python or Haskell NLP libraries, Microsoft Bot Framework, AWS Step Functions + Lex, etc etc
What the user actually types
What the user intends to communicate
bus.findStop
bus.routeBetween
bus.timeEstimate
appraisal.stupid
Data from an utterance needed to return an answer relevant to the intent. The program may need to extrapolate on the data to be useful.
Wendy's (in Elm Grove?)
Reynolds (Memorial Hospital ?)
Reynolds (Rapid Care?)
The Highlands?
none
(Transportation Center?) in downtown Wheeling
{
"name": "Corpus",
"data" : [
{
"intent": "agent.age",
"utterances": [
"your age",
"how old is your platform",
"how old are you",
"what's your age",
"I'd like to know your age",
"tell me your age"
],
"answers": [
"I'm very young",
"I was created recently",
"Age is just a number. You're only as old as you feel"
]
}
]
}
A collection of data used to teach the program what utterances correspond to which intents and how to respond to those intents.
What if we need to access part of the user's utterance?
{ "intent": "bus.findstop",
"utterances": [
"Where is the nearest stop to @address",
"Where is the nearest bus stop to @address",
"bus stop near @address",
"What is the stop closest to @address"
],
"slotFilling": {
"address": {
"mandatory": true,
"question": "What is your specific location or address?"
}
},
"answers": [
"Uhhhh, I dunno what the closest stop
to {{ address }} is. Sorry."
]
}
"entities": {
"address": {
"trim": [
{
"position": "afterLast",
"words": ["to"]
},
{
"position": "afterLast",
"words": ["near"]
}
]
}
}
describe("findClosestStop", () => {
test('can find the stop nearest to a set of coordinates', async () => {
const stop = findClosestStop(testStops, [28.965797, 41.010086])
expect(stop.coordinates).toEqual([28.973865, 41.011122])
expect(stop.name).toEqual("A")
})
})
describe.skip("fetchPlace (live test using Google Maps API)", () => {
test('can get a canonical place from the name of a location', async () => {
const resp = await fetchPlace("Reynolds Hospital")
expect(resp.name).toBe("Reynolds Memorial Hospital")
expect(resp.address).toBe("800 Wheeling Ave, Glen Dale, WV 26038, United States")
expect(resp.coordinates).toEqual([39.9461073, -80.75260469999999])
})
describe("'where is the closest stop to wendys'", () => {
test('is about finding a bus stop', async () => {
fetchPlace.mockResolvedValue({
name: "Wendy's",
address: "Elm Grove Mall",
coordinates: [28.965797, 41.010086]
})
findClosestStop.mockReturnValue({
name: "B",
location: "somewhere over the rainbow"
})
const response = await nlp.process('en', 'Where is the closest stop to wendys')
expect(response.intent).toBe('bus.findstop')
expect(response.answer).toMatch(/Wendy's \(Elm Grove Mall\)/)
expect(response.answer).toMatch(/B stop/)
expect(response.answer).toMatch(/over the rainbow/)
})
})
{ "intent": "bus.findstop",
"utterances": [
"Where is the nearest bus stop to @address",
"What is the stop closest to @address"
],
"slotFilling": {
"address": {
"mandatory": true,
"question": "What is your specific location or address?"
}
},
"answers": [
"The nearest bus stop to {{ placeName }} ({{ placeAddress}})
is the {{stopName}} stop {{ stopLocation }}."
],
"actions": [
{
"name": "handleFindStop",
"parameters": []
}
]
}
export async function handleFindStop(data) {
const address = data.context.address
if (address) {
const realAddress = await fetchPlace(address)
data.context.placeName = realAddress.name
data.context.placeAddress = realAddress.address
const closestStop =
findClosestStop(elmgrove, realAddress.coordinates)
data.context.stopLocation = closestStop.location
data.context.stopName = closestStop.name
}
return data
}
Connector
NLP
Bot
NLU manager
Entity Recognition
NLU manager
Slot manager
Stemmer
Action manager
Action manager
Sentiment
QnA
onIntent callback
Pipeline
NLG manager
Corpus
Settings
What if the program guesses incorrectly?
I tried a few options:
None of these were satisfying.
In general, NLP.js is not for a chatbot that needs to know about the state of a conversation and branch based on that.