public void GetLanguageCode_Should_ReturnExpectedResult(string actual, string expected)
        {
            // Arrange > Act
            var result = LanguageUtils.GetLanguageCode(actual);

            // Assert
            Assert.Equal(expected, result);
        }
Beispiel #2
0
        public Task <GrammarCheckResult> GetCorrections(string text)
        {
            string language;

            if (SelectedLanguage == SupportedLanguages.Auto)
            {
                var languageInfo = _languageService.IdentifyLanguage(text);

                language = languageInfo.TwoLetterISOLanguageName;
            }
            else
            {
                language = LanguageUtils.GetLanguageCode(SelectedLanguage.GetDescription());
            }

            var corrections = new List <GrammarCorrection>();

            var words = text.Split(" ");

            var dictionary = GetDictionaryBasedOnWords(words, language);

            foreach (var item in words)
            {
                if (string.IsNullOrWhiteSpace(item) || !char.IsLetter(item[0]))
                {
                    continue;
                }

                // Remove special characters
                var word = StringUtils.RemoveSpecialCharacters(item).ToLower();

                if (string.IsNullOrWhiteSpace(word))
                {
                    continue;
                }

                var wordFound = dictionary.Contains(word);

                if (!wordFound)
                {
                    var possibleCorrections = dictionary.Where(v => _stringDiffService.IsInComparableRange(v, word) && _stringDiffService.ComputeDistance(v, word) < Defaults.StringComparableRange);

                    if (possibleCorrections.Any())
                    {
                        var correction = new GrammarCorrection
                        {
                            WrongWord            = item,
                            PossibleReplacements = possibleCorrections,
                            Message = GetCorrectionMessage(item, language)
                        };
                        corrections.Add(correction);
                    }
                }
            }

            return(Task.FromResult(new GrammarCheckResult(corrections)));
        }
Beispiel #3
0
        private string GetCorrectionMessage(string word, string language)
        {
            if (language == LanguageUtils.GetLanguageCode(SupportedLanguages.English.GetDescription()))
            {
                return($"The word \"{word}\" doesn't exist or isn't in the internal dictionary.");
            }

            return($"La palabra \"{word}\" no existe o no está en el diccionario interno.");
        }