Ejemplo n.º 1
0
        public string ResolveName(string input)
        {
            List <VariableRepresentation> variables = _variableProvider.GetVariables().ToList();
            string transliteratedInput = _transliterator.ToCodeNativeLanguage(input);
            string normalizedInput     = transliteratedInput.UpperNormalize();
            string phoneticInput       = _phoneticConverter.GetPhonetic(transliteratedInput);

            _logger.Info("=========================================");
            _logger.Info($"Input: {input}");
            _logger.Info($"Transliterated: {transliteratedInput}");
            _logger.Info($"Normalized: {normalizedInput}");
            _logger.Info($"Phonetic: {phoneticInput}");
            var varsToCompare = variables.Where(v =>
                                                Math.Abs(v.PhoneticName.Length - phoneticInput.Length) <= MaxPhoneticLenghtDifference).ToList();
            var distances = CalculatePhoneticDistances(varsToCompare, phoneticInput);
            List <KeyValuePair <VariableRepresentation, int> > sortedDistances = distances
                                                                                 .ToList().OrderBy(pair => pair.Value)
                                                                                 .ToList();

            sortedDistances.Take(5)
            .ForEach(kvp => _logger.Info($"{kvp.Key.Name}({kvp.Key.PhoneticName}) - {kvp.Value}"));
            _logger.Info("=========================================");
            List <KeyValuePair <VariableRepresentation, int> > phoneticDistancesTop = sortedDistances
                                                                                      .Where(kvp => kvp.Value == sortedDistances.Min(pair => pair.Value))
                                                                                      .ToList();

            if (phoneticDistancesTop.Count == 1)
            {
                return(phoneticDistancesTop.First().Key.Name);
            }

            var textDistances = CalculateTextDistances(phoneticDistancesTop, normalizedInput)
                                .OrderBy(pair => pair.Value).ToList();
            int textDistancesMinDiff = textDistances.Min(kvp => kvp.Value);
            var textDistancesTop     = textDistances.Where(pair => pair.Value == textDistancesMinDiff).ToList();

            _logger.Info($"Top normalized: {transliteratedInput}");
            textDistances.Take(5).ForEach(kvp => _logger.Info($"{kvp.Key.Name} ({kvp.Key.NormalizedName}) - {kvp.Value}"));
            if (textDistancesTop.Count == 1 || textDistancesTop.First().Value <= MaxPlainTextAllowedDistance)
            {
                return(textDistancesTop.First().Key.Name);
            }
            return(phoneticDistancesTop.First().Key.Name);
            //sortedDistances.First().Key.Name;
        }