Пример #1
0
        /// <summary>
        ///     Checks to see if the word is in the dictionary
        /// </summary>
        /// <param name="word" type="string">
        ///     <para>
        ///         The word to check
        ///     </para>
        /// </param>
        /// <returns>
        ///     Returns true if word is found in dictionary
        /// </returns>
        public bool FindWord(ref string word)
        {
            Initialize();

            TraceWriter.TraceVerbose("Find Word: {0}", word);

            if (Dictionary.Contains(word))
            {
                return(true);
            }
            string wordLower = word.ToLowerInvariant();

            if (Dictionary.Contains(wordLower))
            {
                word = wordLower;
                return(true);
            }
            return(false);
        }
Пример #2
0
        /// <summary>
        ///     Checks to see if the word is in the dictionary
        /// </summary>
        /// <param name="word" type="string">
        ///     <para>
        ///         The word to check
        ///     </para>
        /// </param>
        /// <returns>
        ///     Returns true if word is found in dictionary
        /// </returns>
        private bool TestWord(string word)
        {
            Initialize();

            TraceWriter.TraceVerbose("Testing Word: {0}", word);

            if (Dictionary.Contains(word))
            {
                return(true);
            }

            var lowerWord = word.ToLower();

            if (word != lowerWord)
            {
                return(Dictionary.Contains(lowerWord));
            }

            return(false);
        }
Пример #3
0
        private void MarkMisspelledWords(int startWordIndex, int endWordIndex)
        {
            TraceWriter.TraceVerbose("Mark Misspelled Words {0} to {1}", startWordIndex, endWordIndex);

            //optimize by disabling event messages
            int eventMask = NativeMethods.SendMessage(this.Handle,
                                                      NativeMethods.EM_SETEVENTMASK, 0, 0);

            //optimize by disabling redraw
            NativeMethods.SendMessage(base.Handle,
                                      NativeMethods.WM_SETREDRAW, 0, 0);

            //save selection
            int selectStart  = base.SelectionStart;
            int selectLength = base.SelectionLength;

            //save show dialog value
            bool dialog = this.SpellChecker.ShowDialog;

            //disable show dialog to prevent dialogs on spell check
            this.SpellChecker.ShowDialog = false;

            //spell check all words in range
            while (this.SpellChecker.SpellCheck(startWordIndex, endWordIndex))
            {
                startWordIndex++;
            }
            //restore show dialog value
            this.SpellChecker.ShowDialog = dialog;

            //restore selection
            base.Select(selectStart, selectLength);

            //restore event messages
            eventMask = NativeMethods.SendMessage(this.Handle,
                                                  NativeMethods.EM_SETEVENTMASK, 0, eventMask);
            //restore redraw
            NativeMethods.SendMessage(base.Handle,
                                      NativeMethods.WM_SETREDRAW, 1, 0);
        }
Пример #4
0
        protected override void OnTextChanged(EventArgs e)
        {
            // get change size
            int changeSize = this.Text.Length - PreviousTextLength;

            PreviousTextLength = this.Text.Length;

            // sync spell checker text with text box text
            _SpellChecker.Text = base.Text;

            int currentPosition = base.SelectionStart;

            // get indexs
            int previousWordIndex = _SpellChecker.GetWordIndexFromTextIndex(currentPosition - changeSize);

            CurrentWordIndex = _SpellChecker.GetWordIndexFromTextIndex(currentPosition);

            // set current word to spell check
            _SpellChecker.WordIndex = previousWordIndex;

            // get the end index of previous word with out white space
            int wordEndIndex = _SpellChecker.TextIndex + _SpellChecker.CurrentWord.Length;

            TraceWriter.TraceVerbose("ChangeSize:{0}; PreviousWord:{1}; CurrentWord:{2}; Position:{3}; WordEnd:{4};",
                                     changeSize, previousWordIndex, CurrentWordIndex, currentPosition, wordEndIndex);

            if (previousWordIndex != CurrentWordIndex || wordEndIndex < currentPosition)
            {
                // if word indexs not equal, spell check all words from previousWordIndex to CurrentWordIndex
                // or if word indexs equal, spell check if caret in white space
                this.MarkMisspelledWords(previousWordIndex, CurrentWordIndex);
                UncheckedWordIndex = -1;
            }
            else
            {
                UncheckedWordIndex = previousWordIndex;
            }

            base.OnTextChanged(e);
        }
Пример #5
0
        private void SpellChecker_MisspelledWord(object sender, NetSpell.SpellChecker.SpellingEventArgs args)
        {
            TraceWriter.TraceVerbose("Misspelled Word:{0}", args.Word);

            int selectionStart  = base.SelectionStart;
            int selectionLength = base.SelectionLength;

            base.Select(args.TextIndex, args.Word.Length);

            NativeMethods.CHARFORMAT2 cf = new NativeMethods.CHARFORMAT2();
            cf.cbSize          = Marshal.SizeOf(cf);
            cf.dwMask          = NativeMethods.CFM_UNDERLINETYPE;
            cf.bUnderlineType  = (byte)this.MisspelledStyle;
            cf.bUnderlineType |= (byte)this.MisspelledColor;

            int result = NativeMethods.SendMessage(base.Handle,
                                                   NativeMethods.EM_SETCHARFORMAT,
                                                   NativeMethods.SCF_SELECTION | NativeMethods.SCF_WORD,
                                                   ref cf);

            base.Select(selectionStart, selectionLength);
        }
Пример #6
0
        /// <summary>
        ///     Populates the <see cref="Suggestions"/> property with word suggestions
        ///     for the <see cref="CurrentWord"/>
        /// </summary>
        /// <remarks>
        ///		<see cref="TestWord"/> must have been called before calling this method
        /// </remarks>
        /// <seealso cref="CurrentWord"/>
        /// <seealso cref="Suggestions"/>
        /// <seealso cref="TestWord"/>
        public void Suggest()
        {
            // can't generate suggestions with out current word
            if (this.CurrentWord.Length == 0)
            {
                TraceWriter.TraceWarning("No current word");
                return;
            }

            this.Initialize();

            List <Word> tempSuggestion = new List <Word>();

            if ((_suggestionMode == SuggestionEnum.PhoneticNearMiss ||
                 _suggestionMode == SuggestionEnum.Phonetic) &&
                _dictionary.PhoneticRules.Count > 0)
            {
                // generate phonetic code for possible root word
                Hashtable codes = new Hashtable();
                foreach (string tempWord in _dictionary.PossibleBaseWords)
                {
                    string tempCode = _dictionary.PhoneticCode(tempWord);
                    if (tempCode.Length > 0 && !codes.ContainsKey(tempCode))
                    {
                        codes.Add(tempCode, tempCode);
                    }
                }

                if (codes.Count > 0)
                {
                    // search root words for phonetic codes
                    foreach (Word word in _dictionary.BaseWords.Values)
                    {
                        if (codes.ContainsKey(word.PhoneticCode))
                        {
                            List <string> words = _dictionary.ExpandWord(word);
                            // add expanded words
                            foreach (string expandedWord in words)
                            {
                                Word newWord = new Word();
                                newWord.Text         = expandedWord;
                                newWord.EditDistance = this.EditDistance(this.CurrentWord, expandedWord);
                                tempSuggestion.Add(newWord);
                            }
                        }
                    }
                }
                TraceWriter.TraceVerbose("Suggestiongs Found with Phonetic Stratagy: {0}", tempSuggestion.Count);
            }

            if (_suggestionMode == SuggestionEnum.PhoneticNearMiss ||
                _suggestionMode == SuggestionEnum.NearMiss)
            {
                // suggestions for a typical fault of spelling, that
                // differs with more, than 1 letter from the right form.
                this.ReplaceChars(ref tempSuggestion);

                // swap out each char one by one and try all the tryme
                // chars in its place to see if that makes a good word
                this.BadChar(ref tempSuggestion);

                // try omitting one char of word at a time
                this.ExtraChar(ref tempSuggestion);

                // try inserting a tryme character before every letter
                this.ForgotChar(ref tempSuggestion);

                // split the string into two pieces after every char
                // if both pieces are good words make them a suggestion
                this.TwoWords(ref tempSuggestion);

                // try swapping adjacent chars one by one
                this.SwapChar(ref tempSuggestion);
            }

            TraceWriter.TraceVerbose("Total Suggestiongs Found: {0}", tempSuggestion.Count);

            tempSuggestion.Sort();              // sorts by edit score
            _suggestions.Clear();

            for (int i = 0; i < tempSuggestion.Count; i++)
            {
                string word = ((Word)tempSuggestion[i]).Text;
                // looking for duplicates
                if (!_suggestions.Contains(word))
                {
                    // populating the suggestion list
                    _suggestions.Add(word);
                }

                if (_suggestions.Count >= _maxSuggestions && _maxSuggestions > 0)
                {
                    break;
                }
            }
        }         // suggest
        /// <summary>
        ///     Searches all contained word lists for word
        /// </summary>
        /// <param name="word" type="string">
        ///     <para>
        ///         The word to search for
        ///     </para>
        /// </param>
        /// <returns>
        ///     Returns true if word is found
        /// </returns>
        public bool Contains(string word)
        {
            // clean up possible base word list
            _possibleBaseWords.Clear();

            // Step 1 Search UserWords
            if (_userWords.ContainsKey(word))
            {
                TraceWriter.TraceVerbose("Word Found in User Dictionary: {0}", word);
                return(true);  // word found
            }

            // Step 2 Search BaseWords
            if (_baseWords.ContainsKey(word))
            {
                TraceWriter.TraceVerbose("Word Found in Base Words: {0}", word);
                return(true); // word found
            }

            // Step 3 Remove suffix, Search BaseWords

            // save suffixed words for use when removing prefix
            List <string> suffixWords = new List <string>();

            // Add word to suffix word list
            suffixWords.Add(word);

            foreach (AffixRule rule in SuffixRules.Values)
            {
                foreach (AffixEntry entry in rule.AffixEntries)
                {
                    string tempWord = AffixUtility.RemoveSuffix(word, entry);
                    if (tempWord != word)
                    {
                        if (_baseWords.ContainsKey(tempWord))
                        {
                            if (VerifyAffixKey(tempWord, rule.Name[0]))
                            {
                                TraceWriter.TraceVerbose("Word Found With Base Words: {0}; Suffix Key: {1}", tempWord, rule.Name[0]);
                                return(true); // word found
                            }
                        }

                        if (rule.AllowCombine)
                        {
                            // saving word to check if it is a word after prefix is removed
                            suffixWords.Add(tempWord);
                        }
                        else
                        {
                            // saving possible base words for use in generating suggestions
                            _possibleBaseWords.Add(tempWord);
                        }
                    }
                }
            }
            // saving possible base words for use in generating suggestions
            _possibleBaseWords.AddRange(suffixWords);

            // Step 4 Remove Prefix, Search BaseWords
            foreach (AffixRule rule in PrefixRules.Values)
            {
                foreach (AffixEntry entry in rule.AffixEntries)
                {
                    foreach (string suffixWord in suffixWords)
                    {
                        string tempWord = AffixUtility.RemovePrefix(suffixWord, entry);
                        if (tempWord != suffixWord)
                        {
                            if (_baseWords.ContainsKey(tempWord))
                            {
                                if (VerifyAffixKey(tempWord, rule.Name[0]))
                                {
                                    TraceWriter.TraceVerbose("Word Found With Base Words: {0}; Prefix Key: {1}", tempWord, rule.Name[0]);
                                    return(true); // word found
                                }
                            }

                            // saving possible base words for use in generating suggestions
                            _possibleBaseWords.Add(tempWord);
                        }
                    } // suffix word
                }     // prefix rule entry
            }         // prefix rule
            // word not found
            TraceWriter.TraceVerbose("Possible Base Words: {0}", _possibleBaseWords.Count);
            return(false);
        }
Пример #8
0
        /// <summary>
        ///     Populates the <see cref="Suggestions"/> property with word suggestions
        ///     for the <see cref="CurrentWord"/>
        /// </summary>
        /// <remarks>
        ///     <see cref="TestWord()"/> or <see cref="TestWord(string)"/> must have been called before calling this method
        /// </remarks>
        /// <seealso cref="CurrentWord"/>
        /// <seealso cref="Suggestions"/>
        /// <seealso cref="TestWord()"/>
        /// <seealso cref="TestWord(string)"/>
        public void Suggest()
        {
            // can't generate suggestions with out current word
            if (CurrentWord.Length == 0)
            {
                TraceWriter.TraceWarning("No current word");
                return;
            }

            Initialize();

            var tempSuggestion = new List <Word>();

            if ((SuggestionMode == SuggestionEnum.PhoneticNearMiss ||
                 SuggestionMode == SuggestionEnum.Phonetic) &&
                _dictionary.PhoneticRules.Count > 0)
            {
                // generate phonetic code for possible root word
                Dictionary <string, string> codes = new Dictionary <string, string>();
                foreach (string tempWord in _dictionary.PossibleBaseWords)
                {
                    string tempCode = _dictionary.PhoneticCode(tempWord);
                    if (tempCode.Length > 0 && !codes.ContainsKey(tempCode))
                    {
                        codes.Add(tempCode, tempCode);
                    }
                }

                if (codes.Count > 0)
                {
                    // search root words for phonetic codes
                    foreach (Word word in _dictionary.BaseWords.Values)
                    {
                        if (codes.ContainsKey(word.PhoneticCode))
                        {
                            List <string> words = _dictionary.ExpandWord(word);

                            // add expanded words
                            foreach (string expandedWord in words)
                            {
                                SuggestWord(expandedWord, tempSuggestion);
                            }
                        }
                    }
                }

                TraceWriter.TraceVerbose("Suggestions Found with Phonetic Strategy: {0}", tempSuggestion.Count);
            }

            if (SuggestionMode == SuggestionEnum.PhoneticNearMiss ||
                SuggestionMode == SuggestionEnum.NearMiss)
            {
                // suggestions for a typical fault of spelling, that
                // differs with more, than 1 letter from the right form.
                ReplaceChars(tempSuggestion);

                // swap out each char one by one and try all the tryme
                // chars in its place to see if that makes a good word
                BadChar(tempSuggestion);

                // try omitting one char of word at a time
                ExtraChar(tempSuggestion);

                // try inserting a tryme character before every letter
                ForgotChar(tempSuggestion);

                // split the string into two pieces after every char
                // if both pieces are good words make them a suggestion
                TwoWords(tempSuggestion);

                // try swapping adjacent chars one by one
                SwapChar(tempSuggestion);
            }

            TraceWriter.TraceVerbose("Total Suggestions Found: {0}", tempSuggestion.Count);

            tempSuggestion.Sort();  // sorts by edit score
            Suggestions.Clear();

            foreach (var suggestion in tempSuggestion)
            {
                string word = suggestion.Text;

                // looking for duplicates
                if (!Suggestions.Contains(word))
                {
                    // populating the suggestion list
                    Suggestions.Add(word);
                }

                if (Suggestions.Count >= MaxSuggestions && MaxSuggestions > 0)
                {
                    break;
                }
            }
        }