function PromptChoice()

in Node/core/lib/dialogs/PromptChoice.js [24:136]


    function PromptChoice(features) {
        var _this = _super.call(this, {
            defaultRetryPrompt: 'default_choice',
            defaultRetryNamespace: consts.Library.system,
            recognizeNumbers: true,
            recognizeOrdinals: true,
            recognizeChoices: true,
            defaultListStyle: Prompt_1.ListStyle.list,
            inlineListCount: 3,
            minScore: 0.4
        }) || this;
        _this._onChoices = [];
        _this.updateFeatures(features);
        _this.onRecognize(function (context, cb) {
            if (context.message.text && !_this.features.disableRecognizer) {
                _this.findChoices(context, true, function (err, choices) {
                    if (err || !choices || !choices.length) {
                        return cb(err, 0.0);
                    }
                    var topScore = 0.0;
                    var topMatch = null;
                    var utterance = context.message.text.trim();
                    if (_this.features.recognizeChoices) {
                        var options = { allowPartialMatches: true };
                        var match = PromptRecognizers_1.PromptRecognizers.findTopEntity(PromptRecognizers_1.PromptRecognizers.recognizeChoices(utterance, choices, options));
                        if (match) {
                            topScore = match.score;
                            topMatch = match.entity;
                        }
                    }
                    if (_this.features.recognizeNumbers) {
                        var options = { minValue: 1, maxValue: choices.length, integerOnly: true };
                        var match = PromptRecognizers_1.PromptRecognizers.findTopEntity(PromptRecognizers_1.PromptRecognizers.recognizeNumbers(context, options));
                        if (match && match.score > topScore) {
                            var index = Math.floor(match.entity - 1);
                            topScore = match.score;
                            topMatch = {
                                score: match.score,
                                index: index,
                                entity: choices[index].value
                            };
                        }
                    }
                    if (_this.features.recognizeOrdinals) {
                        var match = PromptRecognizers_1.PromptRecognizers.findTopEntity(PromptRecognizers_1.PromptRecognizers.recognizeOrdinals(context));
                        if (match && match.score > topScore) {
                            var index = match.entity > 0 ? match.entity - 1 : choices.length + match.entity;
                            if (index >= 0 && index < choices.length) {
                                topScore = match.score;
                                topMatch = {
                                    score: match.score,
                                    index: index,
                                    entity: choices[index].value
                                };
                            }
                        }
                    }
                    if (topScore >= _this.features.minScore && topScore > 0) {
                        cb(null, topScore, topMatch);
                    }
                    else {
                        cb(null, 0.0);
                    }
                });
            }
            else {
                cb(null, 0.0);
            }
        });
        _this.onFormatMessage(function (session, text, speak, callback) {
            var context = session.dialogData;
            var options = context.options;
            _this.findChoices(session.toRecognizeContext(), false, function (err, choices) {
                var msg;
                if (!err && choices) {
                    var sendChoices = context.turns === 0 || context.isReprompt;
                    var listStyle = options.listStyle;
                    if (listStyle === undefined || listStyle === null || listStyle === Prompt_1.ListStyle.auto) {
                        var maxTitleLength_1 = 0;
                        choices.forEach(function (choice) {
                            var l = choice.action && choice.action.title ? choice.action.title.length : choice.value.length;
                            if (l > maxTitleLength_1) {
                                maxTitleLength_1 = l;
                            }
                        });
                        var supportsKeyboards = Channel.supportsKeyboards(session, choices.length);
                        var supportsCardActions = Channel.supportsCardActions(session, choices.length);
                        var maxActionTitleLength = Channel.maxActionTitleLength(session);
                        var hasMessageFeed = Channel.hasMessageFeed(session);
                        if (maxTitleLength_1 <= maxActionTitleLength &&
                            (supportsKeyboards || (!hasMessageFeed && supportsCardActions))) {
                            listStyle = Prompt_1.ListStyle.button;
                            sendChoices = true;
                        }
                        else {
                            listStyle = _this.features.defaultListStyle;
                            var inlineListCount = _this.features.inlineListCount;
                            if (listStyle === Prompt_1.ListStyle.list && inlineListCount > 0 && choices.length <= inlineListCount) {
                                listStyle = Prompt_1.ListStyle.inline;
                            }
                        }
                    }
                    msg = PromptChoice.formatMessage(session, listStyle, text, speak, sendChoices ? choices : null);
                }
                callback(err, msg);
            });
        });
        _this.matches(consts.Intents.Repeat, function (session) {
            session.dialogData.turns = 0;
            _this.sendPrompt(session);
        });
        return _this;
    }