Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab 2, 3, 4, 5 submission #8

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions Code/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ dist-ssr
*.njsproj
*.sln
*.sw?

# don't leak keys to github
azure.js
284 changes: 245 additions & 39 deletions Code/dm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// -*- js-indent-level: 2 -*-
import { assign, createActor, setup } from "xstate";
import { speechstate } from "speechstate";
import { createBrowserInspector } from "@statelyai/inspect";
Expand All @@ -24,10 +25,24 @@ const grammar = {
vlad: { person: "Vladislav Maraev" },
aya: { person: "Nayat Astaiza Soriano" },
rasmus: { person: "Rasmus Blanck" },
andreas: { person: "Andreas Henriksson" },

monday: { day: "Monday" },
tuesday: { day: "Tuesday" },
wednesday: { day: "Wednesday" },
thursday: { day: "Thursday" },
friday: { day: "Friday" },
saturday: { day: "Saturday" },
sunday: { day: "Sunday" },

tomorrow: { day: "tomorrow" },
"the day after tomorrow": { day: "The day after tomorrow" },

"10": { time: "10:00" },
"11": { time: "11:00" },
"12": { time: "12:00" },
"13": { time: "13:00" },
// ...
};

/* Helper functions */
Expand All @@ -39,13 +54,52 @@ function getPerson(utterance) {
return (grammar[utterance.toLowerCase()] || {}).person;
}

function getDay(utterance) {
return (grammar[utterance.toLowerCase()] || {}).day;
}

function getTime(utterance) {
return (grammar[utterance.toLowerCase()] || {}).time;
}


const yesGrammar = new Set([
"yes", "yeah", "of course", "sure", "yup",
]);
const noGrammar = new Set([
"no", "nope", "no way", "nah",
]);

function isYes(utterance) {
return yesGrammar.has(utterance.toLowerCase());
}

function isNo(utterance) {
return noGrammar.has(utterance.toLowerCase());
}


const dmMachine = setup({
actions: {
/* define your actions here */
say: ({ context }, params) =>
context.ssRef.send({
type: "SPEAK",
value: {
utterance: params,
},
}),
listen: ({ context }, params) =>
context.ssRef.send({
type: "LISTEN",
value: {}, // workaround for some incompatibility I encountered
}),
},
}).createMachine({
context: {
count: 0,
name: undefined,
date: undefined,
take_whole_day: undefined,
time: undefined,
},
id: "DM",
initial: "Prepare",
Expand All @@ -61,51 +115,203 @@ const dmMachine = setup({
},
WaitToStart: {
on: {
CLICK: "PromptAndAsk",
CLICK: "ReceiveGreeting",
},
},
PromptAndAsk: {
initial: "Prompt",
states: {
Prompt: {
entry: ({ context }) =>
context.ssRef.send({
type: "SPEAK",
value: {
utterance: `Hello world!`,
},
}),
on: { SPEAK_COMPLETE: "Ask" },
},
Ask: {
entry: ({ context }) =>
context.ssRef.send({
type: "LISTEN",
}),
on: {
RECOGNISED: {
actions: ({ context, event }) =>
context.ssRef.send({
type: "SPEAK",
value: {
utterance: `You just said: ${
event.value[0].utterance
}. And it ${
isInGrammar(event.value[0].utterance) ? "is" : "is not"
} in the grammar.`,
},
}),
},
SPEAK_COMPLETE: "#DM.Done",
ReceiveGreeting: {
entry: ["listen"],
on: {
// move on to the next state after any utterance or when no input was received
RECOGNISED: "Greet",
ASR_NOINPUT: "Greet",
},
},
Greet: {
entry: [{type: 'say', params: "Let's create an appointment"}],
on: { SPEAK_COMPLETE: "AskName" },
},
AskName: {
entry: [
{type: 'say', params: "Who are you meeting with?"},
],
on: {
SPEAK_COMPLETE: { actions: ["listen"] },
RECOGNISED: [
{
guard: ({ context, event }) => getPerson(event.value[0].utterance) !== undefined,
actions: [
({ context, event }) => { context.name = getPerson(event.value[0].utterance) },
],
target: "AskDay",
},
},
"UnknownAskName",
],
ASR_NOINPUT: "ResetAskName",
},
},
ResetAskName: {
entry: [{ type: 'say', params: "I didn't hear you" }],
on: { SPEAK_COMPLETE: "AskName" },
},
UnknownAskName: {
entry: [{ type: 'say', params: "I didn't understand" }],
on: { SPEAK_COMPLETE: "AskName" },
},

AskDay: {
entry: [
{type: 'say', params: "On which day is your meeting?"},
],
on: {
SPEAK_COMPLETE: { actions: ["listen"] },
RECOGNISED: [
{
guard: ({ context, event }) => getDay(event.value[0].utterance) !== undefined,
actions: [
({ context, event }) => { context.date = getDay(event.value[0].utterance) },
],
target: "AskTakeWholeDay",
},
"UnknownAskDay",
],
ASR_NOINPUT: "ResetAskDay",
},
},
ResetAskDay: {
entry: [{ type: 'say', params: "I didn't hear you" }],
on: { SPEAK_COMPLETE: "AskDay" },
},
UnknownAskDay: {
entry: [{ type: 'say', params: "I didn't understand" }],
on: { SPEAK_COMPLETE: "AskDay" },
},

AskTakeWholeDay: {
entry: [
{type: 'say', params: "Will it take the whole day?"},
],
on: {
SPEAK_COMPLETE: { actions: ["listen"] },
RECOGNISED: [
{
guard: ({ context, event }) => isYes(event.value[0].utterance),
target: "BookDay",
},
{
guard: ({ context, event }) => isNo(event.value[0].utterance),
target: "AskTime",
},
"UnknownAskTakeWholeDay",
],
ASR_NOINPUT: "ResetAskTakeWholeDay",
},
},
Done: {
ResetAskTakeWholeDay: {
entry: [{ type: 'say', params: "I didn't hear you" }],
on: { SPEAK_COMPLETE: "AskTakeWholeDay" },
},
UnknownAskTakeWholeDay: {
entry: [{ type: 'say', params: "I didn't understand" }],
on: { SPEAK_COMPLETE: "AskTakeWholeDay" },
},

AskTime: {
entry: [
{type: 'say', params: "What time is your meeting?"},
],
on: {
CLICK: "PromptAndAsk",
SPEAK_COMPLETE: { actions: ["listen"] },
RECOGNISED: [
{
guard: ({ context, event }) => getTime(event.value[0].utterance) !== undefined,
actions: [
({ context, event }) => { context.time = getTime(event.value[0].utterance) },
],
target: "BookTime",
},
"UnknownAskTime",
],
ASR_NOINPUT: "ResetAskTime",
},
},
ResetAskTime: {
entry: [{ type: 'say', params: "I didn't hear you" }],
on: { SPEAK_COMPLETE: "AskTime" },
},
UnknownAskTime: {
entry: [{ type: 'say', params: "I didn't understand" }],
on: { SPEAK_COMPLETE: "AskTime" },
},

BookTime: {
entry: [{
type: 'say',
params: ({ context }) => `Do you want me to create an appointment with ${context.name} on ${context.date} at ${context.time}?`,
}],
on: {
SPEAK_COMPLETE: { actions: ["listen"] },
RECOGNISED: [
{
guard: ({ context, event }) => isYes(event.value[0].utterance),
target: "Finalize",
},
{
guard: ({ context, event }) => isNo(event.value[0].utterance),
target: "AskName",
},
"UnknownBookTime",
],
ASR_NOINPUT: "ResetBookTime",
},
},
ResetBookTime: {
entry: [{ type: 'say', params: "I didn't hear you" }],
on: { SPEAK_COMPLETE: "BookTime" },
},
UnknownBookTime: {
entry: [{ type: 'say', params: "I didn't understand" }],
on: { SPEAK_COMPLETE: "BookTime" },
},

BookDay: {
entry: [{
type: 'say',
params: ({ context }) => `Do you want me to create an appointment with ${context.name} on ${context.date} for the whole day?`,
}],
on: {
SPEAK_COMPLETE: { actions: ["listen"] },
RECOGNISED: [
{
guard: ({ context, event }) => isYes(event.value[0].utterance),
target: "Finalize",
},
{
guard: ({ context, event }) => isNo(event.value[0].utterance),
target: "AskName",
},
"UnknownBookDay",
],
ASR_NOINPUT: "ResetBookDay",
},
},
ResetBookDay: {
entry: [{ type: 'say', params: "I didn't hear you" }],
on: { SPEAK_COMPLETE: "BookDay" },
},
UnknownBookDay: {
entry: [{ type: 'say', params: "I didn't understand" }],
on: { SPEAK_COMPLETE: "BookDay" },
},

Finalize: {
entry: [{
type: 'say',
params: ({ context }) => "Your appointment has been created!",
}],
on: {
SPEAK_COMPLETE: "#DM.WaitToStart",
}
},
},
});

Expand Down
Loading