Hear before you say¶
A spoken request spends most of its time waiting: for the recogniser to decide the person has stopped speaking, and then for the answer. A device that hears before it says moves the surface while the person is still speaking, and tries words it is not sure of without anything being kept.
Two calls¶
| Call | Changes the surface | Use it for |
|---|---|---|
say(text, utterance) |
when understood | the words so far, and the final words |
hear(text) |
never | an alternative the recogniser offers, to see whether the interpreter would understand it |
An utterance is one stretch of speech, named by the device that hears it. Every say that names
the same utterance as the rail's newest entry replaces that entry instead of adding one, so saying
"new", then "newcastle", then "newcastle this weekend" leaves one request on the rail, not three.
An ear¶
The package ships no recogniser. It exports the shape of one, so a consumer wraps the browser's speech recognition or a streaming service of its own:
type Hearing =
| { kind: "partial"; text: string }
| { kind: "final"; text: string; alternatives: string[] }
| { kind: "end" };
type Ear = { start(): void; stop(): void; abort(): void };
Act on each hearing¶
This handler says each partial under the utterance, says the final words, and when the interpreter does not understand them, hears each alternative and says the first it understands:
async function onHearing(model: SurfaceModel, utterance: string, hearing: Hearing): Promise<void> {
if (hearing.kind === "partial") {
await model.say(hearing.text, utterance);
} else if (hearing.kind === "final") {
const said = await model.say(hearing.text, utterance);
if (said.understood) return;
for (const alternative of hearing.alternatives) {
const heard = await model.hear(alternative);
if (heard.understood) {
await model.say(alternative, utterance);
return;
}
}
}
}
Hearing "new", then a final "new castle" with the alternatives "new car sale" and "newcastle", it sends:
POST /say {"text":"new","utterance":"u-1"}
POST /say {"text":"new castle","utterance":"u-1"} not understood
POST /hear {"text":"new car sale"} not understood, nothing kept
POST /hear {"text":"newcastle"} understood, nothing kept
POST /say {"text":"newcastle","utterance":"u-1"} the one rail entry for the utterance
A new utterance id for each stretch of speech is the device's to make; any string unique to the device will do.
What the server answers¶
hear answers the interpreter's move as it made it, with nothing persisted:
{"understood":true,"reason":null,"move":{"shape":"shelf","anchor":{"kind":"theme","id":"newcastle"}}}
A device can show that move as a preview, by applying it to a copy of the surface with the package's
applyMove, before deciding to say it.
The newest say wins¶
Partial says go out faster than their answers come back, and the answers can arrive out of order. The model holds the answer to the newest say only; an older answer arriving later is returned to its caller and not held. Nothing waits for an answer before the next say goes.
The interpreter must be safe on a prefix¶
A device that says partial words is relying on the interpreter: each prefix of what a person says should move the surface a step towards where the whole would, or not move it at all. An interpreter that jumps to a different shape on "new" than on "newcastle" makes the surface flicker on every device showing it. Write an interpreter has the rest of its rules.