Пример #1
0
        /// <summary>
        /// Tries to replace every char of the passed word to chars from the TryCharacters of the dictionary.
        /// </summary>
        /// <param name="word">source word</param>
        /// <param name="suggestedWords">generated suggested words will be placed here</param>
        /// <param name="dic"></param>
        public override void Apply(string word, Dictionary <string, SpellSuggestion> suggestedWords, SpellDictionary dic)
        {
            int    srcWordLen  = word.Length;
            string tryChars    = dic.TryCharacters;
            int    tryCharsNum = tryChars.Length;

            for (int i = 0; i < srcWordLen; ++i)
            {
                StringBuilder s = new StringBuilder(word);
                for (int k = 0; k < tryCharsNum; ++k)
                {
                    char ch = tryChars[k];
                    if (s[i] == ch)
                    {
                        continue;
                    }

                    s[i] = ch;
                    string newWord = s.ToString();
                    if (dic.IsWordCorrect(newWord))
                    {
                        if (!suggestedWords.ContainsKey(newWord))
                        {
                            suggestedWords.Add(newWord, new SpellSuggestion(newWord, spellChecker.EditDistanceWeights.ReplaceCharWeight));
                        }
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Applies replace patterns of the passed dictionary.
        /// </summary>
        /// <param name="word">source word</param>
        /// <param name="suggestedWords">generated suggested words will be placed here</param>
        /// <param name="dic"></param>
        public override void Apply(string word, Dictionary <string, SpellSuggestion> suggestedWords, SpellDictionary dic)
        {
            var replacePatterns = dic.ReplacePatterns;
            int n = replacePatterns.Count;
            int p;

            for (int i = 0; i < n; ++i)
            {
                var    replacePattern = replacePatterns[i];
                string searchChars    = replacePattern.SearchChars;
                string replaceChars   = replacePattern.ReplaceChars;
                int    editDistance   = 0;

                string newWord = word;
                p = 0;
                while ((p = newWord.IndexOf(searchChars, p)) != -1)
                {
                    newWord       = newWord.Substring(0, p) + replaceChars + newWord.Substring(p + searchChars.Length);
                    p            += replaceChars.Length;
                    editDistance += spellChecker.EditDistanceWeights.ReplaceCharWeight * Math.Max(searchChars.Length, replaceChars.Length);

                    if (dic.IsWordCorrect(newWord))
                    {
                        if (!suggestedWords.ContainsKey(newWord))
                        {
                            suggestedWords.Add(newWord, new SpellSuggestion(newWord, editDistance));
                        }
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Try to split the passed word onto 2 words.
        /// </summary>
        /// <param name="word">source word</param>
        /// <param name="suggestedWords">generated suggested words will be placed here</param>
        /// <param name="dic"></param>
        public override void Apply(string word, Dictionary <string, SpellSuggestion> suggestedWords, SpellDictionary dic)
        {
            int wordLen = word.Length;

            if (wordLen < 2)
            {
                return;
            }

            for (int i = 1; i < wordLen; ++i)
            {
                string word1 = word.Substring(0, i);
                string word2 = word.Substring(i);

                if (dic.IsWordCorrect(word1) && dic.IsWordCorrect(word2))
                {
                    string newText = word1 + ' ' + word2;
                    if (!suggestedWords.ContainsKey(newText))
                    {
                        suggestedWords.Add(newText, new SpellSuggestion(newText, spellChecker.EditDistanceWeights.InsertCharWeight));
                    }
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Try to insert a character from TryCharacters into an every position.
        /// </summary>
        /// <param name="word">source word</param>
        /// <param name="suggestedWords">generated suggested words will be placed here</param>
        /// <param name="dic"></param>
        public override void Apply(string word, Dictionary <string, SpellSuggestion> suggestedWords, SpellDictionary dic)
        {
            int    wordLen     = word.Length;
            string tryChars    = dic.TryCharacters;
            int    tryCharsNum = tryChars.Length;

            for (int i = 0; i <= wordLen; ++i)
            {
                for (int k = 0; k < tryCharsNum; ++k)
                {
                    string newWord = word.Insert(i, tryChars[k].ToString());

                    if (dic.IsWordCorrect(newWord))
                    {
                        if (!suggestedWords.ContainsKey(newWord))
                        {
                            suggestedWords.Add(newWord, new SpellSuggestion(newWord, spellChecker.EditDistanceWeights.InsertCharWeight));
                        }
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Tries to omit one by one char.
        /// </summary>
        /// <param name="word">source word</param>
        /// <param name="suggestedWords">generated suggested words will be placed here</param>
        /// <param name="dic"></param>
        public override void Apply(string word, Dictionary <string, SpellSuggestion> suggestedWords, SpellDictionary dic)
        {
            int wordLen = word.Length;

            if (wordLen <= 1)
            {
                return;
            }

            for (int i = 0; i < wordLen; ++i)
            {
                string newWord = word.Remove(i, 1);

                if (dic.IsWordCorrect(newWord))
                {
                    if (!suggestedWords.ContainsKey(newWord))
                    {
                        suggestedWords.Add(newWord, new SpellSuggestion(newWord, spellChecker.EditDistanceWeights.DeleteCharWeight));
                    }
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Tries to swap every pair of adjacent chars.
        /// </summary>
        /// <param name="word">source word</param>
        /// <param name="suggestedWords">generated suggested words will be placed here</param>
        /// <param name="dic"></param>
        public override void Apply(string word, Dictionary <string, SpellSuggestion> suggestedWords, SpellDictionary dic)
        {
            int wordLen = word.Length;

            for (int i = 0; i < wordLen - 1; ++i)
            {
                // swapping...
                StringBuilder s = new StringBuilder(word);
                char          t = s[i];
                s[i]     = s[i + 1];
                s[i + 1] = t;
                string newWord = s.ToString();

                if (dic.IsWordCorrect(newWord))
                {
                    if (!suggestedWords.ContainsKey(newWord))
                    {
                        suggestedWords.Add(newWord, new SpellSuggestion(newWord, spellChecker.EditDistanceWeights.SwapCharWeight));
                    }
                }
            }
        }