Click here to Skip to main content
15,881,757 members
Articles / DevOps / Load Testing

NHunspell Component for Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.90/5 (31 votes)
1 Sep 2013CPOL6 min read 113.1K   4.1K   46  
NHunspell component example for WindowsForms
using System;
using System.Text.RegularExpressions;

namespace NHunspellComponent.Spelling
{
    internal struct Word
    {
        private const string PATTERN_IS_DEVANAGARI = @"(\p{IsDevanagari}+)";
        public int Start;
        public int Length;
        private string text;

        public string Text
        {
            get { return text; }
            set
            {
                text = value;
                int l = text.Length;
                text = text.TrimStart(' ');
                Start = Start - (l - text.Length);
                text = text.Trim();
                Length = text.Length;
            }
        }

        public override string ToString()
        {
            return String.Format("{0} ({1}){2}", Text, Start, Length);
        }

        public static int GetWordStart(ref string Text, int position)
        {
            if (!(Text.Length > 0)) return 0;
            if (position == Text.Length) position--;
            int wordStart = position;
            if (!char.IsLetterOrDigit(Text[wordStart]) && wordStart > 0) wordStart--;
            for (int i = wordStart; i >= 0; i--)
            {
                char ch = Text[i];
                //if (ch == ' ' || ch == '\n')
                //if (char.IsSeparator(ch) || char.IsPunctuation(ch))
                if (!CheckCharBelongsToWord(ch))
                {
                    break;
                }
                wordStart = i;
            }
            return wordStart;
        }

        public static int GetWordEnd(ref string Text, int position)
        {
            int wordEnd = Text.Length;
            for (int i = position; i < Text.Length && Text.Length > 0; i++)
            {
                char ch = Text[i];
                //if (ch == ' ' || ch == '\n')
                //if (char.IsSeparator(ch) || char.IsPunctuation(ch))
                if (!CheckCharBelongsToWord(ch))
                {
                    wordEnd = i;
                    break;
                }
            }
            return wordEnd;
        }


        private static bool CheckCharBelongsToWord(char ch)
        {
            return char.IsLetterOrDigit(ch) && !char.IsPunctuation(ch) ||
                System.Text.RegularExpressions.Regex.IsMatch(
                ch.ToString(
                  System.Threading.Thread.CurrentThread.CurrentCulture),
                  PATTERN_IS_DEVANAGARI
                  );
        }
        //return !char.IsSeparator(ch) && !char.IsPunctuation(ch) && !char.IsSeparator(ch);

        public static Word GetWordFromPosition(string Text, int position)
        {
            Word result = new Word();
            result.Start = Word.GetWordStart(ref Text, position);
            result.Length = Word.GetWordEnd(ref Text, result.Start) - result.Start;
            result.Text = Text.Substring(result.Start, result.Length).Replace("\n", "");
            return result;
        }

        internal void Reset()
        {
            Start = 0;
            Length = 0;
            text = "";
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ukraine Ukraine
"a man can do no more than he can" (proverb)
Don't you think there are a lot of things you couldn't do couple of years before but now you can? Things didn't change, but the only thing that changed is belief in yourself.

Comments and Discussions