Пример #1
0
 /// <summary>
 /// Record the location of a doubled word
 /// </summary>
 /// <param name="sender">The sender of the event</param>
 /// <param name="e">The event arguments</param>
 private void speller_DoubledWord(object sender, SpellingEventArgs e)
 {
     issues.Add(new SpellingIssue
     {
         Word = e.Word,
         Location = e.Position,
     });
 }
Пример #2
0
 /// <summary>
 /// Record the location of a misspelled word
 /// </summary>
 private void speller_MisspelledWord(object sender, SpellingEventArgs e)
 {
     issues.Add(new SpellingIssue
     {
         IsMisspelling = true,
         Word = e.Word,
         Location = e.Position,
         Suggestions = dictionary.SuggestCorrections(e.Word).ToList()
     });
 }
Пример #3
0
        /// <summary>
        /// Spell check the given text
        /// </summary>
        /// <param name="text">The text to spell check</param>
        /// <param name="location">The location of the text being spell checked</param>
        /// <returns>False if it was canceled or True if it completed.</returns>
        private bool SpellCheckInternal(string text, TextLocation location)
        {
            SpellingEventArgs se = null;
            List<Match> xmlTags = null;
            TextLocation priorWord = new TextLocation();
            string currentWord;
            int textIdx;

            // Signal the start and allow it to be canceled
            CancelEventArgs ce = new CancelEventArgs();
            this.OnSpellCheckTextStarting(ce);

            if(ce.Cancel)
            {
                this.OnSpellCheckTextCancelled(EventArgs.Empty);
                return false;
            }

            // Note the location of all XML elements if needed
            if(SpellCheckerConfiguration.IgnoreXmlElementsInText)
                xmlTags = reXml.Matches(text).OfType<Match>().ToList();

            // Spell check each word in the given text
            foreach(var word in GetWordsInText(text))
            {
                currentWord = text.Substring(word.Start, word.Length);
                textIdx = word.Start;

                if(!IsProbablyARealWord(currentWord) || (xmlTags != null && xmlTags.Count != 0 &&
                  xmlTags.Any(match => textIdx >= match.Index && textIdx <= match.Index + match.Length - 1)))
                    continue;

                if(!dictionary.ShouldIgnoreWord(currentWord) && !dictionary.IsSpelledCorrectly(currentWord))
                {
                    // Sometimes it flags a word as misspelled if it ends with "'s".  Try checking the word
                    // without the "'s".  If ignored or correct without it, don't flag it.  This appears to be
                    // caused by the definitions in the dictionary rather than Hunspell.
                    if(currentWord.EndsWith("'s", StringComparison.OrdinalIgnoreCase))
                    {
                        currentWord = currentWord.Substring(0, currentWord.Length - 2);

                        if(dictionary.ShouldIgnoreWord(currentWord) || dictionary.IsSpelledCorrectly(currentWord))
                            continue;

                        currentWord += "'s";
                    }

                    se = new SpellingEventArgs(currentWord, location.ToPoint(text, textIdx));
                    this.OnMisspelledWord(se);

                    if(se.Cancel)
                        break;
                }
                else
                    if(priorWord.Length != 0 && String.Compare(text.Substring(priorWord.Start, priorWord.Length),
                      currentWord, StringComparison.OrdinalIgnoreCase) == 0 &&
                      IsAllWhitespace(text, priorWord.Start + priorWord.Length, word.Start - 1))
                    {
                        se = new SpellingEventArgs(currentWord, location.ToPoint(text, textIdx));
                        this.OnDoubledWord(se);

                        if(se.Cancel)
                            break;
                    }

                priorWord = word;
            }

            if(se != null && se.Cancel)
            {
                this.OnSpellCheckTextCancelled(EventArgs.Empty);
                return false;
            }

            this.OnSpellCheckTextCompleted(EventArgs.Empty);
            return true;
        }
Пример #4
0
        /// <summary>
        /// This raises the <see cref="MisspelledWord" /> event
        /// </summary>
        protected virtual void OnMisspelledWord(SpellingEventArgs e)
        {
            EventHandler<SpellingEventArgs> handler = MisspelledWord;

            if(handler != null)
                handler(this, e);
        }
Пример #5
0
        /// <summary>
        /// Spell check the given text
        /// </summary>
        /// <param name="text">The text to spell check</param>
        /// <param name="location">The location of the text being spell checked</param>
        /// <returns>False if it was canceled or True if it completed.</returns>
        private bool SpellCheckInternal(string text, TextLocation location)
        {
            SpellingEventArgs se        = null;
            List <Match>      xmlTags   = null;
            TextLocation      priorWord = new TextLocation();
            string            currentWord;
            int textIdx;

            // Signal the start and allow it to be canceled
            CancelEventArgs ce = new CancelEventArgs();

            this.OnSpellCheckTextStarting(ce);

            if (ce.Cancel)
            {
                this.OnSpellCheckTextCancelled(EventArgs.Empty);
                return(false);
            }

            // Note the location of all XML elements if needed
            if (SpellCheckerConfiguration.IgnoreXmlElementsInText)
            {
                xmlTags = reXml.Matches(text).OfType <Match>().ToList();
            }

            // Spell check each word in the given text
            foreach (var word in GetWordsInText(text))
            {
                currentWord = text.Substring(word.Start, word.Length);
                textIdx     = word.Start;

                if (!IsProbablyARealWord(currentWord) || (xmlTags != null && xmlTags.Count != 0 &&
                                                          xmlTags.Any(match => textIdx >= match.Index && textIdx <= match.Index + match.Length - 1)))
                {
                    continue;
                }

                if (!dictionary.ShouldIgnoreWord(currentWord) && !dictionary.IsSpelledCorrectly(currentWord))
                {
                    // Sometimes it flags a word as misspelled if it ends with "'s".  Try checking the word
                    // without the "'s".  If ignored or correct without it, don't flag it.  This appears to be
                    // caused by the definitions in the dictionary rather than Hunspell.
                    if (currentWord.EndsWith("'s", StringComparison.OrdinalIgnoreCase))
                    {
                        currentWord = currentWord.Substring(0, currentWord.Length - 2);

                        if (dictionary.ShouldIgnoreWord(currentWord) || dictionary.IsSpelledCorrectly(currentWord))
                        {
                            continue;
                        }

                        currentWord += "'s";
                    }

                    se = new SpellingEventArgs(currentWord, location.ToPoint(text, textIdx));
                    this.OnMisspelledWord(se);

                    if (se.Cancel)
                    {
                        break;
                    }
                }
                else
                if (priorWord.Length != 0 && String.Compare(text.Substring(priorWord.Start, priorWord.Length),
                                                            currentWord, StringComparison.OrdinalIgnoreCase) == 0 &&
                    IsAllWhitespace(text, priorWord.Start + priorWord.Length, word.Start - 1))
                {
                    se = new SpellingEventArgs(currentWord, location.ToPoint(text, textIdx));
                    this.OnDoubledWord(se);

                    if (se.Cancel)
                    {
                        break;
                    }
                }

                priorWord = word;
            }

            if (se != null && se.Cancel)
            {
                this.OnSpellCheckTextCancelled(EventArgs.Empty);
                return(false);
            }

            this.OnSpellCheckTextCompleted(EventArgs.Empty);
            return(true);
        }