Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

Commit

Permalink
Fix #471, prioritize intent matches that have typed slots
Browse files Browse the repository at this point in the history
  • Loading branch information
ianb committed Oct 25, 2019
1 parent 51e4de8 commit 65eacdb
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions extension/background/intentParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ this.intentParser = (function() {
const Matcher = (exports.Matcher = class Matcher {
constructor(phrase) {
this.phrase = phrase;
const { slots, regex, parameters } = this._phraseToRegex(phrase);
const { slots, regex, parameters, slotTypes } = this._phraseToRegex(
phrase
);
this.slots = slots;
this.slotTypes = slotTypes;
this.parameters = parameters;
this.regexString = regex;
this.regex = new RegExp("^" + regex + "$", "i");
Expand All @@ -41,6 +44,7 @@ this.intentParser = (function() {
}
const result = {
slots: {},
slotTypes: this.slotTypes,
utterance,
regex: this.regexString,
parameters: Object.assign({}, this.parameters),
Expand All @@ -63,6 +67,7 @@ this.intentParser = (function() {

_phraseToRegex(toParse) {
const slots = [];
const slotTypes = {};
const parameters = {};
let regex = "";
while (toParse) {
Expand All @@ -80,6 +85,7 @@ this.intentParser = (function() {
if (!ENTITY_TYPES[entityName]) {
throw new Error(`No entity type by the name ${entityName}`);
}
slotTypes[parts[0].trim()] = parts[1].trim();
const entityRegex = ENTITY_TYPES[entityName]
.map(e => (e ? " " + e : e))
.join("|");
Expand All @@ -101,7 +107,7 @@ this.intentParser = (function() {
}
// Implements the {s} optional strings:
regex = regex.replace(/\{(.*?)\}/g, "(?:$1)?");
return { slots, parameters, regex };
return { slots, parameters, regex, slotTypes };
}

_getAlternatives(phrase) {
Expand Down Expand Up @@ -236,7 +242,14 @@ this.intentParser = (function() {
if (match) {
match.name = name;
match.fallback = false;
const slotChars = Object.values(match.slots).join("").length;
let slotChars = 0;
for (const slotName in match.slots) {
if (match.slotTypes[slotName]) {
slotChars += 1;
} else {
slotChars += match.slots[slotName].length;
}
}
if (bestMatch === undefined || bestChars > slotChars) {
bestMatch = match;
bestChars = slotChars;
Expand Down

0 comments on commit 65eacdb

Please sign in to comment.