Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libraries/botbuilder-dialogs/src/choices/findChoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ export interface FindChoicesOptions extends FindValuesOptions {
* Defaults to `false`.
*/
noAction?: boolean;

/**
* (Optional) Default is `true`. If `false`, the Number Model will not be used to check the utterance for numbers.
*/
recognizeNumbers?: boolean;

/**
* (Optional) Default is `true`. If `false`, the Ordinal Model will not be used to check the utterance for ordinal numbers.
*/
recognizeOrdinals?: boolean;
}

/**
Expand Down
20 changes: 14 additions & 6 deletions libraries/botbuilder-dialogs/src/choices/recognizeChoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ export function recognizeChoices(utterance: string, choices: (string|Choice)[],
// TODO: Should this log an error or do something?
}
}

// Initialize options
options = Object.assign({
locale: 'en-us',
recognizeNumbers: true,
recognizeOrdinals: true
} as FindChoicesOptions, options);

// Normalize choices
const list: Choice[] = (choices || []).map(
Expand All @@ -76,16 +83,17 @@ export function recognizeChoices(utterance: string, choices: (string|Choice)[],
// - We only want to use a single strategy for returning results to avoid issues where utterances
// like the "the third one" or "the red one" or "the first division book" would miss-recognize as
// a numerical index or ordinal as well.
const locale: string = options && options.locale ? options.locale : 'en-us';
let matched: ModelResult<FoundChoice>[] = findChoices(utterance, list, options);
if (matched.length === 0) {
// Next try finding by ordinal
const ordinals: ModelResult[] = Recognizers.recognizeOrdinal(utterance, locale);
if (ordinals.length > 0) {
if (options.recognizeOrdinals) {
const ordinals: ModelResult[] = Recognizers.recognizeOrdinal(utterance, options.locale);
ordinals.forEach(matchChoiceByIndex);
} else {
// Finally try by numerical index
Recognizers.recognizeNumber(utterance, locale).forEach(matchChoiceByIndex);
}

// Finally try by numerical index
if (matched.length === 0 && options.recognizeNumbers) {
Recognizers.recognizeNumber(utterance, options.locale).forEach(matchChoiceByIndex);
}

// Sort any found matches by their position within the utterance.
Expand Down
18 changes: 18 additions & 0 deletions libraries/botbuilder-dialogs/tests/choices_recognizers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,22 @@ describe('recognizeChoices()', function() {
assertChoice(found[1], 'blue', 2, 1.0);
done();
});

it('should not find a choice if recognizeOrdinals option disabled.', function (done) {
const found = recognizeChoices(`first`, colorChoices, { recognizeOrdinals: false });
assert(found.length === 0, `Invalid token count of '${found.length}' returned.`);
done();
});

it('should not find a choice if recognizeNumbers option disabled.', function (done) {
const found = recognizeChoices(`1`, colorChoices, { recognizeNumbers: false });
assert(found.length === 0, `Invalid token count of '${found.length}' returned.`);
done();
});

it('should not find a choice if both recognizeOrdinals and recognizeNumbers options are disabled.', function (done) {
const found = recognizeChoices(`the first and third one please.`, colorChoices, { recognizeOrdinals: false, recognizeNumbers: false });
assert(found.length === 0, `Invalid token count of '${found.length}' returned.`);
done();
});
});