<commit_before>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Microsoft.Bot.Builder.Form.Advanced
{
    /// <summary>
    /// Simple value table with explicitly added values.
    /// </summary>
    public class EnumeratedRecognizer<T> : IRecognizer<T>
        where T : class, new()
    {
        public delegate string DescriptionDelegate(object value);
        public delegate IEnumerable<string> TermsDelegate(object value);

        public EnumeratedRecognizer(IField<T> field)
        {
            var configuration = field.Form().Configuration();
            _description = field.Description();
            _terms = field.Terms();
            _values = field.Values();
            _valueDescriptions = field.ValueDescriptions();
            _descriptionDelegate = (value) => field.ValueDescription(value);
            _termsDelegate = (value) => field.Terms(value);
            _helpFormat = field.Template(field.AllowNumbers()
                ? (field.AllowsMultiple() ? TemplateUsage.HelpManyNumber : TemplateUsage.HelpOneNumber)
                : (field.AllowsMultiple() ? TemplateUsage.HelpManyWord : TemplateUsage.HelpOneWord));
            _helpFormat.ApplyDefaults(configuration.DefaultPrompt);
            _noPreference = field.Optional() ? configuration.NoPreference : null;
            _currentChoice = configuration.CurrentChoice.FirstOrDefault();
            BuildPerValueMatcher(field.AllowNumbers(), configuration.NoPreference, configuration.CurrentChoice);
        }

        public EnumeratedRecognizer(string description,
            IEnumerable<object> terms,
            IEnumerable<object> values,
            DescriptionDelegate descriptionDelegate,
            TermsDelegate termsDelegate,
            bool allowNumbers,
            Template helpFormat,
            IEnumerable<string> noPreference = null,
            IEnumerable<string> currentChoice = null)
        {
            _values = values;
            _descriptionDelegate = descriptionDelegate;
            _termsDelegate = termsDelegate;
            _valueDescriptions = (from value in values select _descriptionDelegate(value)).ToArray();
            _helpFormat = helpFormat;
            _noPreference = noPreference;
            if (currentChoice != null)
            {
                _currentChoice = currentChoice.FirstOrDefault();
            }

            BuildPerValueMatcher(allowNumbers, noPreference, currentChoice);
        }

        public IEnumerable<object> Values()
        {
            return _values;
        }

        public IEnumerable<string> ValueDescriptions()
        {
            return _valueDescriptions;
        }

        public string ValueDescription(object value)
        {
            return _descriptionDelegate(value);
        }

        public IEnumerable<string> ValidInputs(object value)
        {
            return _termsDelegate(value);
        }

        public string Help(T state, object defaultValue)
        {
            var values = _valueDescriptions;
            if (defaultValue != null && _currentChoice != null)
            {
                values = values.Union(new string[] { _currentChoice + " or 'c'" });
            }
            if (_noPreference != null)
            {
                values = values.Union(new string[] { _noPreference.First() });
            }
            return string.Format(_helpFormat.Pattern(), 1, _max,
                Language.BuildList(values, _helpFormat.Separator, _helpFormat.LastSeparator));
        }

        public IEnumerable<TermMatch> Matches(string input, object defaultValue)
        {
            // if the user hit enter on an optional prompt, then consider taking the current choice as a low confidence option
            bool userSkippedPrompt = string.IsNullOrWhiteSpace(input) && _noPreference != null;
            if (userSkippedPrompt)
            {<commit_msg>Make sure current choice is added to help for default no preference.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Microsoft.Bot.Builder.Form.Advanced
{
    /// <summary>
    /// Simple value table with explicitly added values.
    /// </summary>
    public class EnumeratedRecognizer<T> : IRecognizer<T>
        where T : class, new()
    {
        public delegate string DescriptionDelegate(object value);
        public delegate IEnumerable<string> TermsDelegate(object value);

        public EnumeratedRecognizer(IField<T> field)
        {
            var configuration = field.Form().Configuration();
            _description = field.Description();
            _terms = field.Terms();
            _values = field.Values();
            _valueDescriptions = field.ValueDescriptions();
            _descriptionDelegate = (value) => field.ValueDescription(value);
            _termsDelegate = (value) => field.Terms(value);
            _helpFormat = field.Template(field.AllowNumbers()
                ? (field.AllowsMultiple() ? TemplateUsage.HelpManyNumber : TemplateUsage.HelpOneNumber)
                : (field.AllowsMultiple() ? TemplateUsage.HelpManyWord : TemplateUsage.HelpOneWord));
            _helpFormat.ApplyDefaults(configuration.DefaultPrompt);
            _noPreference = field.Optional() ? configuration.NoPreference : null;
            _currentChoice = configuration.CurrentChoice.FirstOrDefault();
            BuildPerValueMatcher(field.AllowNumbers(), configuration.NoPreference, configuration.CurrentChoice);
        }

        public EnumeratedRecognizer(string description,
            IEnumerable<object> terms,
            IEnumerable<object> values,
            DescriptionDelegate descriptionDelegate,
            TermsDelegate termsDelegate,
            bool allowNumbers,
            Template helpFormat,
            IEnumerable<string> noPreference = null,
            IEnumerable<string> currentChoice = null)
        {
            _values = values;
            _descriptionDelegate = descriptionDelegate;
            _termsDelegate = termsDelegate;
            _valueDescriptions = (from value in values select _descriptionDelegate(value)).ToArray();
            _helpFormat = helpFormat;
            _noPreference = noPreference;
            if (currentChoice != null)
            {
                _currentChoice = currentChoice.FirstOrDefault();
            }

            BuildPerValueMatcher(allowNumbers, noPreference, currentChoice);
        }

        public IEnumerable<object> Values()
        {
            return _values;
        }

        public IEnumerable<string> ValueDescriptions()
        {
            return _valueDescriptions;
        }

        public string ValueDescription(object value)
        {
            return _descriptionDelegate(value);
        }

        public IEnumerable<string> ValidInputs(object value)
        {
            return _termsDelegate(value);
        }

        public string Help(T state, object defaultValue)
        {
            var values = _valueDescriptions;
            if ((defaultValue != null || _noPreference != null)  && _currentChoice != null)
            {
                values = values.Union(new string[] { _currentChoice + " or 'c'" });
            }
            if (_noPreference != null)
            {
                values = values.Union(new string[] { _noPreference.First() });
            }
            return string.Format(_helpFormat.Pattern(), 1, _max,
                Language.BuildList(values, _helpFormat.Separator, _helpFormat.LastSeparator));
        }

        public IEnumerable<TermMatch> Matches(string input, object defaultValue)
        {
            // if the user hit enter on an optional prompt, then consider taking the current choice as a low confidence option
            bool userSkippedPrompt = string.IsNullOrWhiteSpace(input) && _noPreference != null;
            if (userSkippedPrompt)
            {
{'fraction_non_alphanumeric': 0.08485526152317058, 'fraction_numerical': 0.00024847807181016274, 'mean_word_length': 2.235530546623794, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3930803', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1880, 'n_words': 441}