/// <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));
                    }
                }
            }
        }
        /// <summary>
        /// Returns dictionary for the passed culture.
        /// </summary>
        /// <param name="cultName">e.g., "en-US", or "ru-RU"</param>
        /// <returns>null, if there's no such dictionary</returns>
        public SpellDictionary GetDictionary(string cultName)
        {
            SpellDictionary dict= null;
            cultName = cultName.ToLower ();
            if (dictionaries.ContainsKey (cultName))
            {
                dict = dictionaries[cultName];
            }
            else
            {
                try
                {
                    dict = new SpellDictionary (cultName);

                    string path = System.IO.Path.Combine (DictionariesBaseFolder, cultName);
                    dict.Load (path);

                    dictionaries.Add (cultName, dict);
                }
                catch (Exception)
                {
                    dict= null;
                }
            }

            return dict;
        }
示例#4
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));
                }
            }
        }
示例#5
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));
                }
            }
        }
示例#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));
                }
            }
        }
        /// <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));
                    }
                }
            }
        }
示例#8
0
 public static void LoadDictionary(TestContext testContext)
 {
     dic = new SpellDictionary ("test");
     string path = System.IO.Path.Combine (testContext.TestDeploymentDir, @"dics\test");
     dic.Load (path);
 }
示例#9
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="word">source word</param>
 /// <param name="suggestedWords">generated suggested words will be placed here</param>
 /// <param name="dic"></param>
 public abstract void Apply(string word, Dictionary<string, SpellSuggestion> suggestedWords, SpellDictionary dic);