Exemplo n.º 1
0
        private SpellingWord CheckWordSpelling(string initialWord)
        {
            SpellingWord word = new SpellingWord(initialWord);

            this.SetPossibleCorrections(word);

            return(word);
        }
Exemplo n.º 2
0
        public string CheckText(string text)
        {
            string[] words = text.Split(new string[] { " ", Environment.NewLine }, StringSplitOptions.None);
            for (int i = 0; i < words.Length; i++)
            {
                if (string.IsNullOrWhiteSpace(words[i]))
                {
                    continue;
                }

                SpellingWord fixedWord = this.CheckWordSpelling(words[i]);
                text = text.Replace(words[i], fixedWord.ToString());
            }

            return(text);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Добавляет варианты исправления слова
        /// </summary>
        /// <param name="word">Слово для проверки</param>
        private void SetPossibleCorrections(SpellingWord word)
        {
            foreach (string dictWord in this.Dictionary)
            {
                int diff = this.LevenshteinDistance(dictWord, word.Word);

                //пропускаем слова с большой разницой в длине
                if (Math.Abs(word.Word.Length - dictWord.Length) >= 2)
                {
                    continue;
                }

                //пропускаем слова с расстоянием больше 2, либо если в вариантах есть слова с расстоянем 1, то пропускаем > 1
                if ((word.Corrections.Any(_ => _.Diff == 1) && diff > 1) || diff > 2)
                {
                    continue;
                }

                word.Corrections.Add(new Correction
                {
                    Diff = diff,
                    Word = dictWord
                });

                //если есть иделальное совпадение, то удаляем все остальные варианты
                if (diff == 0)
                {
                    word.Corrections.RemoveAll(_ => _.Word != dictWord);

                    break;
                }

                //если есть вариант с расстоянием 1, то удаляем > 1
                if (diff == 1)
                {
                    word.Corrections.RemoveAll(_ => _.Diff > 1);
                }
            }
        }