Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const list = (choices || []).map((choice, index) => typeof choice === 'string' ? { value: choice } : choice).filter((choice) => choice);
// Try finding choices by text search first
// - 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 = options && options.locale ? options.locale : 'en-us';
let matched = findChoices_1.findChoices(utterance, list, options);
if (matched.length === 0) {
// Next try finding by ordinal
const ordinals = Recognizers.recognizeOrdinal(utterance, locale);
if (ordinals.length > 0) {
ordinals.forEach(matchChoiceByIndex);
}
else {
// Finally try by numerical index
Recognizers.recognizeNumber(utterance, locale).forEach(matchChoiceByIndex);
}
// Sort any found matches by their position within the utterance.
// - The results from findChoices() are already properly sorted so we just need this
// for ordinal & numerical lookups.
matched = matched.sort((a, b) => a.start - b.start);
}
return matched;
}
exports.recognizeChoices = recognizeChoices;
);
// Try finding choices by text search first
// - 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[] = findChoices(utterance, list, options);
if (matched.length === 0) {
// Next try finding by ordinal
const ordinals: ModelResult[] = Recognizers.recognizeOrdinal(utterance, locale);
if (ordinals.length > 0) {
ordinals.forEach(matchChoiceByIndex);
} else {
// Finally try by numerical index
Recognizers.recognizeNumber(utterance, locale).forEach(matchChoiceByIndex);
}
// Sort any found matches by their position within the utterance.
// - The results from findChoices() are already properly sorted so we just need this
// for ordinal & numerical lookups.
matched = matched.sort((a: ModelResult, b: ModelResult) => a.start - b.start);
}
return matched;
}
recognize: function recognize(context) {
const request = context.activity || {};
const utterance = request.text || '';
const locale = request.locale || defaultLocale || 'en-us';
const results = Recognizers.recognizeNumber(utterance, locale);
const value = results.length > 0 && results[0].resolution ? parseFloat(results[0].resolution.value) : undefined;
return Promise.resolve(validator ? validator(context, value) : value);
}
};
return __awaiter(this, void 0, void 0, function* () {
const result = { succeeded: false };
const activity = context.activity;
const utterance = activity.text;
const locale = activity.locale || this.defaultLocale || 'en-us';
const results = Recognizers.recognizeNumber(utterance, locale);
if (results.length > 0 && results[0].resolution) {
result.succeeded = true;
result.value = parseFloat(results[0].resolution.value);
}
return result;
});
}
protected async onRecognizeUtterance(planning: PlanningContext, utterance: string, locale: string): Promise|undefined> {
if (utterance && utterance.length > 0) {
const recognized: any = Recognizers.recognizeNumber(utterance, locale);
if (recognized.length > 0 && recognized[0].resolution) {
const value = parseFloat(recognized[0].resolution.value);
return { succeeded: true, value: value, score: 1.0 };
}
}
return { succeeded: false };
}
recognize: function recognize(context) {
const request = context.activity || {};
const utterance = request.text || '';
const locale = request.locale || defaultLocale || 'en-us';
const results = Recognizers.recognizeNumber(utterance, locale);
const value = results.length > 0 && results[0].resolution ? parseFloat(results[0].resolution.value) : undefined;
return Promise.resolve(validator ? validator(context, value) : value as any);
}
};
protected async onRecognize(context: TurnContext, state: any, options: PromptOptions): Promise> {
const result: PromptRecognizerResult = { succeeded: false };
const activity: Activity = context.activity;
const utterance: string = activity.text;
const locale: string = activity.locale || this.defaultLocale || 'en-us';
const results: any = Recognizers.recognizeNumber(utterance, locale);
if (results.length > 0 && results[0].resolution) {
result.succeeded = true;
const culture = this.getCultureFormattedForGlobalize(locale);
const parser = Globalize(culture).numberParser();
result.value = parser(results[0].resolution.value);
}
return result;
}