Published
Edited
Mar 23, 2022
6 stars
Insert cell
Insert cell
Insert cell
Insert cell
{
//'simon says' chat-bot
function myBot(input){
let doc = nlp(input)
//look for known-templates
if(doc.has('^simon says .')){
let todo = doc.after('^simon says')
let action = todo.verbs(0).normalize() //doing what?
return `OK, ${action.conjugate()[0].Gerund} now` //'read' -> 'reading'
} else {
return 'doing nothing.'
}
}
return myBot('simon says shoot the puck') // 'shooting'
//return myBot('shoot the puck') // 'do nothing'
}
Insert cell
Insert cell
{
function elizaBot(input){
let doc = nlp(input)
//our canned-templates
if(doc.has('i #Adverb? (am|feel) #Adverb? #Adjective')){
let feeling = doc.match('i #Adverb? am #Adverb? [#Adjective]').out('normal')
return `When did you become ${feeling}?`
} else if(doc.has('(father|mother|dad|mom)')){
let whichParent = doc.match('(father|mother|dad|mom)').out('normal')
return `Why don't you tell me about your ${whichParent}.`
} else {
return 'can you elaborate on that?'
}
}
// return elizaBot(`well, i'm kinda upset about it.`)
return elizaBot(`well, i don't really talk with my dad much anymore...`)
}
Insert cell
Insert cell
{
const myPlugin = {
//add words to the internal lexicon
words:{
'facebook': 'Software',
'google': 'Software',
'salesforce': 'Software'
},
compute: {
loginTypes:(doc)=>{
doc.match("how do? i (can|should)? (log|sign|get) in to my? #Software").tag('LoginIssue')
doc.match("i can't (log|sign|get) in to my? #Software").tag( 'LoginIssue')
doc.match("#Software won't let me (log|sign|get) in").tag('LoginIssue')
}
}
}
nlp.plugin(myPlugin)

//ok, now this should be tagged as a LoginIssue.
let doc=nlp('umm, do you know how I should sign-in to my salesforce account?')

//respond appropriately
if(doc.has('#LoginIssue')){
return 'OK, press the Log-in button.'
} else {
return 'talk to an operator.'
}
}
Insert cell
Insert cell
{
let state = {}
function doReply(input){
//parse the input
let doc = nlp(input, {hi:'Greeting', hey:'Greeting', yo:'Greeting'})
//build-up our state object from the parsed text
let name = doc.match(`i'm [#Person+]`)
//or try these versions of it:
name = name.found ? name : doc.match('my name is [#Person+]')
name = name.found ? name : doc.match('they call me [#Person+]')

if(name.found === true){
state.name = name.normalize().toTitleCase().out('text')
}
//begin input-based branching..
if(doc.has('#Greeting')){
//some state-based branching..
if(!state.name){
return `Hi! What's your name?`
}else{
return `oh hey ${state.name}`
}
}
}
return doReply(`oh hey, I'm maurice. They call me a space cowboy.`)
//return doReply(`hello, is this thing on?`) // "Hi! What's your name?"
}
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more