public SettingFilter(SettingList settingList)
        {
            this.settingList      = settingList;
            this.settingMatcher   = new SettingMatcher(this.settingList);
            this.numberNormalizer = new NumberNormalizer();

            // Supporting setting files are stored as embeddded resources
            Assembly resourceAssembly = typeof(SettingFilter).Assembly;

            var amountNormalizerFile = resourceAssembly
                                       .GetManifestResourceNames()
                                       .Where(x => x.Contains("amount_percentage.tsv"))
                                       .First();

            var typeNormalizerFile = resourceAssembly
                                     .GetManifestResourceNames()
                                     .Where(x => x.Contains("amount_type.tsv"))
                                     .First();

            var unitNormalizerFile = resourceAssembly
                                     .GetManifestResourceNames()
                                     .Where(x => x.Contains("amount_unit.tsv"))
                                     .First();

            this.amountNormalizer = new EntityNormalizer(resourceAssembly, amountNormalizerFile);
            this.typeNormalizer   = new EntityNormalizer(resourceAssembly, typeNormalizerFile);
            this.unitNormalizer   = new EntityNormalizer(resourceAssembly, unitNormalizerFile);
        }
예제 #2
0
        /// <summary>
        /// Apply the user's selection to the setting names.
        /// </summary>
        /// <typeparam name="T">The type.</typeparam>
        /// <param name="state">State object.</param>
        /// <param name="settingEntities">List of entity values.</param>
        /// <param name="changesOrStatuses">The SettingChanges or SettingStatuses to select from.</param>
        /// <returns>The selected SettingChanges or SettingStatuses or null if no selection was made.</returns>
        public IList <T> ApplySelectionToSettings <T>(AutomotiveSkillState state, List <string> settingEntities, IList <T> changesOrStatuses)
            where T : SettingOperation
        {
            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            if (settingEntities == null)
            {
                throw new ArgumentNullException(nameof(settingEntities));
            }

            if (changesOrStatuses == null)
            {
                throw new ArgumentNullException(nameof(changesOrStatuses));
            }

            var settingNames = state.GetUniqueSettingNames();

            ISet <string> selectedSettingNames = new HashSet <string>();

            if (settingEntities.Any() && settingNames.Any())
            {
                IList <AvailableSetting> resolvedSettings = new List <AvailableSetting>();
                foreach (var settingName in settingNames)
                {
                    var setting = this.settingList.FindSetting(settingName);
                    if (setting != null)
                    {
                        resolvedSettings.Add(setting);
                    }
                    else
                    {
                        setting = new AvailableSetting
                        {
                            CanonicalName = settingName
                        };
                        resolvedSettings.Add(setting);
                    }
                }

                IList <AvailableSetting> settings_to_select_from = Util.CopyList(resolvedSettings);
                foreach (var setting in resolvedSettings)
                {
                    if (setting.IncludedSettings != null)
                    {
                        foreach (var included_setting_name in setting.IncludedSettings)
                        {
                            if (!settingNames.Contains(included_setting_name))
                            {
                                var included_setting = this.settingList.FindSetting(included_setting_name);
                                if (included_setting == null)
                                {
                                    // Unreachable.
                                    throw new Exception("The included settings of setting \"" + setting.CanonicalName
                                                        + "\" must be canonical names of other settings, but \"" + included_setting_name
                                                        + "\" is not and this should already have been checked when loading the SettingList.");
                                }

                                settings_to_select_from.Add(included_setting);
                            }
                        }
                    }
                }

                var setting_matcher   = new SettingMatcher(this.settingList.CreateSubList(settings_to_select_from));
                var selected_settings = setting_matcher.MatchSettingNamesExactly(settingEntities.First());

                if (!selected_settings.Any())
                {
                    selected_settings = setting_matcher.MatchSettingNames(
                        settingEntities,
                        SettingNameScoreThreshold, SettingNameAntonymDisambPercentageOfMax, true);
                }

                foreach (var setting_info in selected_settings)
                {
                    selectedSettingNames.Add(setting_info.CanonicalName);
                }
            }

            IList <T>     newCandidates     = new List <T>();
            ISet <string> addedSettingNames = new HashSet <string>();

            foreach (var candidate in changesOrStatuses)
            {
                if (candidate == null)
                {
                    continue;
                }

                if (selectedSettingNames.Contains(candidate.SettingName))
                {
                    newCandidates.Add(candidate);
                    addedSettingNames.Add(candidate.SettingName);
                }
            }

            // If NLP tells us to select something that isn't on the list,
            // it's because it's included in one of the settings on the list.
            foreach (var selectedName in selectedSettingNames)
            {
                if (!addedSettingNames.Contains(selectedName))
                {
                    // This search is inefficient, but the lists will be short, so it doesn't matter.
                    foreach (var candidate in changesOrStatuses)
                    {
                        var supportedSetting = settingList.FindSetting(candidate.SettingName);
                        if (supportedSetting != null && supportedSetting.IncludedSettings != null && supportedSetting.IncludedSettings.Contains(selectedName))
                        {
                            var newCandidate = (T)candidate.Clone();
                            newCandidate.SettingName = selectedName;
                            newCandidates.Add(newCandidate);
                            break;
                        }
                    }
                }
            }

            if (!Util.IsNullOrEmpty(newCandidates))
            {
                return(newCandidates);
            }

            return(null);
        }