Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Multiple convolution neural networks approach for online handwriting recognition

, 9 Apr 2013
The research focuses on the presentation of word recognition technique for an online handwriting recognition system which uses multiple component neural networks (MCNN) as the exchangeable parts of the classifier.
capital_letter_v2.zip
digit_v2.zip
lower_case_letter_v2.zip
UNIPENviewer_source_code-noexe.zip
UNIPENviewer
DocToolkit
DocToolkit.csproj.user
DocToolkit.snk
obj
Debug
Release
DrawTools
about.bmp
App.ico
bin
Debug
Release
DrawTools.csproj.user
ellipse.bmp
Ellipse.cur
line.bmp
Line.cur
new.bmp
obj
Debug
DrawTools.DrawArea.resources
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
Release
DrawTools.DrawArea.resources
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
open.bmp
pencil.bmp
Pencil.cur
pointer.bmp
PolyHandle.cur
rectangle.bmp
Rectangle.cur
save.bmp
NNControl
bin
Debug
Common
NNTesting
NNTraining
obj
Debug
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
NNControl.Common.UPTemplateControl.resources
NNControl.FlashForm.resources
NNControl.NNTesting.NNTestingControl.resources
NNControl.NNTesting.TextSpellControl.resources
NNControl.NNTraining.ConvolutionForm.resources
NNControl.NNTraining.CreateNetworkForm.resources
NNControl.NNTraining.FullConnectedForm.resources
NNControl.NNTraining.InputLayerForm.resources
NNControl.NNTraining.OutputLayerForm.resources
NNControl.NNTraining.UP_NNTrainingControl.resources
NNControl.Properties.Resources.resources
NNControl.TrainingParametersForm.resources
NNControl.UPViewer.UpImageViewer.resources
UPControl.Common.BaseControl.resources
UPControl.Common.UPTemplateControl.resources
UPControl.FlashForm.resources
UPControl.NNTraining.UP_NNTrainingControl.resources
UPControl.TrainingParametersForm.resources
UPControl.UPViewer.UpImageViewer.resources
UP_NeuralTraining.FlashForm.resources
UP_NeuralTraining.TrainingParametersForm.resources
UP_NeuralTraining.UP_NNTrainingControl.resources
Release
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
NNControl.Common.UPTemplateControl.resources
NNControl.FlashForm.resources
NNControl.NNTesting.NNTestingControl.resources
NNControl.NNTraining.ConvolutionForm.resources
NNControl.NNTraining.CreateNetworkForm.resources
NNControl.NNTraining.FullConnectedForm.resources
NNControl.NNTraining.InputLayerForm.resources
NNControl.NNTraining.OutputLayerForm.resources
NNControl.NNTraining.UP_NNTrainingControl.resources
NNControl.Properties.Resources.resources
NNControl.TrainingParametersForm.resources
NNControl.UPViewer.UpImageViewer.resources
Properties
Resources
btnBack.png
btnDrag.png
btnFitToScreen.png
btnNext.png
btnOpen.png
btnPreview.png
btnRotate270.png
btnRotate90.png
btnSelect.png
btnZoomIn.png
btnZoomOut.png
circle.png
clear.png
color_line.png
cry.png
document-new.png
Drag.cur
draw_line.png
ellipse.png
export.png
file.png
fingerprint-recognition.png
folder-open.png
folder.png
folders_explorer.png
Grab.cur
home.png
label-link.png
pointer.png
rectangle.png
save_accept.png
script_(stop).gif
smile.png
stock_draw-line.png
Stop sign.png
Upload.png
user-group-new.png
UPViewer
Perceptron
ActivationFunction
ArchiveSerialization
bin
Debug
Common
Connections
Layers
Network
Neurons
obj
Debug
ANN.Perceptron.Common.BaseControl.resources
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
Neurons.BaseControl.resources
Release
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
Neurons.BaseControl.resources
Properties
Weights
SpellChecker
bin
Debug
Controls
Dictionary
Affix
Dictionary.bmp
Phonetic
doc
NetSpell.ndoc
Forms
Interactive.bmp
obj
Debug
ResGen.read.1.tlog
ResGen.write.1.tlog
SpellChecker.Dictionary.WordDictionary.resources
SpellChecker.Forms.OptionForm.resources
SpellChecker.MultipleSpelling.resources
SpellChecker.NewSpelling.resources
Spell.snk
Spelling.bmp
UNIPENviewer.suo
UNIPENviewer.v11.suo
UNIPENviewer
bin
Debug
Config
de-DE.dic
en-US.dic
fr-FR.dic
it-IT.dic
UNIPENviewer.vshost.exe.manifest
Release
Config
obj
Debug
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
UNIPENviewer.MainForm.resources
UNIPENviewer.Properties.Resources.resources
Release
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
UNIPENviewer.MainForm.resources
UNIPENviewer.Properties.Resources.resources
x86
Debug
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
UNIPENviewer.Form1.resources
UNIPENviewer.Properties.Resources.resources
Properties
Settings.settings
UPImage
Common
Data
FileFormat
obj
Debug
Release
Properties
UNIPENviewer_source_code.zip
bin
Debug
DocToolkit.dll
DocToolkit.pdb
Release
DocToolkit.dll
DocToolkit.csproj.user
DocToolkit.snk
DesignTimeResolveAssemblyReferencesInput.cache
DocToolkit.dll
DocToolkit.pdb
TempPE
DesignTimeResolveAssemblyReferencesInput.cache
DocToolkit.dll
about.bmp
App.ico
DocToolkit.dll
DocToolkit.pdb
DrawTools.dll
DrawTools.pdb
DocToolkit.dll
DrawTools.dll
DrawTools.csproj.user
ellipse.bmp
Ellipse.cur
line.bmp
Line.cur
new.bmp
DesignTimeResolveAssemblyReferencesInput.cache
DrawTools.csproj.GenerateResource.Cache
DrawTools.csprojResolveAssemblyReference.cache
DrawTools.dll
DrawTools.DrawArea.resources
DrawTools.pdb
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
ResolveAssemblyReference.cache
TempPE
DesignTimeResolveAssemblyReferencesInput.cache
DrawTools.dll
DrawTools.DrawArea.resources
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
ResolveAssemblyReference.cache
open.bmp
pencil.bmp
Pencil.cur
pointer.bmp
PolyHandle.cur
rectangle.bmp
Rectangle.cur
Resources
save.bmp
DocToolkit.dll
DocToolkit.pdb
DrawTools-LinhLam.dll
DrawTools-LinhLam.pdb
DrawTools.dll
DrawTools.pdb
NNControl-LinhLam.dll
NNControl-LinhLam.pdb
NNControl.dll
NNControl.pdb
Perceptron.dll
Perceptron.pdb
SpellChecker.dll
SpellChecker.pdb
UP-NeuralTraining.dll
UP-NeuralTraining.pdb
UPImage-LinhLam.dll
UpImage.dll
UpImage.pdb
Release
DocToolkit.dll
DrawTools.dll
Neurons.dll
Neurons.pdb
NNControl.dll
NNControl.pdb
UPImage.dll
UPImage.pdb
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
NNControl-LinhLam.dll
NNControl-LinhLam.pdb
NNControl.Common.UPTemplateControl.resources
NNControl.csproj.GenerateResource.Cache
NNControl.csprojResolveAssemblyReference.cache
NNControl.dll
NNControl.FlashForm.resources
NNControl.NNTesting.NNTestingControl.resources
NNControl.NNTesting.TextSpellControl.resources
NNControl.NNTraining.ConvolutionForm.resources
NNControl.NNTraining.CreateNetworkForm.resources
NNControl.NNTraining.FullConnectedForm.resources
NNControl.NNTraining.InputLayerForm.resources
NNControl.NNTraining.OutputLayerForm.resources
NNControl.NNTraining.UP_NNTrainingControl.resources
NNControl.pdb
NNControl.Properties.Resources.resources
NNControl.TrainingParametersForm.resources
NNControl.UPViewer.UpImageViewer.resources
ResolveAssemblyReference.cache
TempPE
Properties.Resources.Designer.cs.dll
UP-NeuralTraining.dll
UP-NeuralTraining.pdb
UPControl.Common.BaseControl.resources
UPControl.Common.UPTemplateControl.resources
UPControl.FlashForm.resources
UPControl.NNTraining.UP_NNTrainingControl.resources
UPControl.TrainingParametersForm.resources
UPControl.UPViewer.UpImageViewer.resources
UP_NeuralTraining.FlashForm.resources
UP_NeuralTraining.TrainingParametersForm.resources
UP_NeuralTraining.UP_NNTrainingControl.resources
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
NNControl.Common.UPTemplateControl.resources
NNControl.dll
NNControl.FlashForm.resources
NNControl.NNTesting.NNTestingControl.resources
NNControl.NNTraining.ConvolutionForm.resources
NNControl.NNTraining.CreateNetworkForm.resources
NNControl.NNTraining.FullConnectedForm.resources
NNControl.NNTraining.InputLayerForm.resources
NNControl.NNTraining.OutputLayerForm.resources
NNControl.NNTraining.UP_NNTrainingControl.resources
NNControl.pdb
NNControl.Properties.Resources.resources
NNControl.TrainingParametersForm.resources
NNControl.UPViewer.UpImageViewer.resources
ResolveAssemblyReference.cache
TempPE
Properties.Resources.Designer.cs.dll
btnBack.png
btnDrag.png
btnFitToScreen.png
btnNext.png
btnOpen.png
btnPreview.png
btnRotate270.png
btnRotate90.png
btnSelect.png
btnZoomIn.png
btnZoomOut.png
circle.png
clear.png
color_line.png
cry.png
document-new.png
Drag.cur
draw_line.png
ellipse.png
export.png
file.png
fingerprint-recognition.png
folder-open.png
folder.png
folders_explorer.png
Grab.cur
home.png
label-link.png
pointer.png
rectangle.png
save_accept.png
script_(stop).gif
smile.png
stock_draw-line.png
Stop sign.png
Upload.png
user-group-new.png
AForgeLibrary.dll
AForgeLibrary.pdb
NeuralNetworkLibrary.dll
NeuralNetworkLibrary.pdb
Perceptron.dll
Perceptron.pdb
SpellChecker.dll
SpellChecker.pdb
UpImage.dll
UpImage.pdb
Release
NeuralNetworkLibrary.dll
NeuralNetworkLibrary.pdb
Neurons.dll
Neurons.pdb
UPImage.dll
UPImage.pdb
ANN.Perceptron.Common.BaseControl.resources
ANN.Perceptron.csproj.GenerateResource.Cache
ANN.Perceptron.csprojResolveAssemblyReference.cache
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
NeuralNetworkLibrary.dll
NeuralNetworkLibrary.pdb
Neurons.BaseControl.resources
Perceptron.dll
Perceptron.pdb
ResolveAssemblyReference.cache
TempPE
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
NeuralNetworkLibrary.dll
NeuralNetworkLibrary.pdb
Neurons.BaseControl.resources
Neurons.dll
Neurons.pdb
ResolveAssemblyReference.cache
Service References
SpellChecker.dll
SpellChecker.pdb
Release
Dictionary.bmp
NetSpell.ndoc
Interactive.bmp
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
NetSpell.SpellChecker.dll
NetSpell.SpellChecker.pdb
ResGen.read.1.tlog
ResGen.write.1.tlog
SpellChecker.csproj.GenerateResource.Cache
SpellChecker.csprojResolveAssemblyReference.cache
SpellChecker.Dictionary.WordDictionary.resources
SpellChecker.dll
SpellChecker.Forms.OptionForm.resources
SpellChecker.MultipleSpelling.resources
SpellChecker.NewSpelling.resources
SpellChecker.pdb
TempPE
Spell.snk
Spelling.bmp
UNIPENviewer.suo
UNIPENviewer.v11.suo
de-DE.dic
DocToolkit.dll
DocToolkit.pdb
DrawTools-LinhLam.dll
DrawTools-LinhLam.pdb
DrawTools.dll
DrawTools.pdb
en-US.dic
fr-FR.dic
it-IT.dic
NNControl-LinhLam.dll
NNControl.dll
NNControl.pdb
Perceptron.dll
Perceptron.pdb
SpellChecker.dll
SpellChecker.pdb
UNIPENviewer-LinhLam.exe
UNIPENviewer-LinhLam.pdb
UNIPENviewer.exe
UNIPENviewer.pdb
UNIPENviewer.vshost.exe
UNIPENviewer.vshost.exe.manifest
UPImage-LinhLam.dll
UPImage-LinhLam.pdb
UpImage.dll
UpImage.pdb
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
ResolveAssemblyReference.cache
TempPE
Properties.Resources.Designer.cs.dll
UNIPENviewer-LinhLam.exe
UNIPENviewer-LinhLam.pdb
UNIPENviewer.csproj.GenerateResource.Cache
UNIPENviewer.csprojResolveAssemblyReference.cache
UNIPENviewer.exe
UNIPENviewer.MainForm.resources
UNIPENviewer.pdb
UNIPENviewer.Properties.Resources.resources
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
ResolveAssemblyReference.cache
UNIPENviewer.exe
UNIPENviewer.MainForm.resources
UNIPENviewer.pdb
UNIPENviewer.Properties.Resources.resources
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
ResolveAssemblyReference.cache
UNIPENviewer.exe
UNIPENviewer.Form1.resources
UNIPENviewer.pdb
UNIPENviewer.Properties.Resources.resources
Settings.settings
bin
Debug
AForgeLibrary.dll
AForgeLibrary.pdb
UPDatabase.dll
UPDatabase.pdb
UpImage-LinhLam.dll
UpImage-LinhLam.pdb
UpImage.dll
UpImage.pdb
UPUnipenLib.dll
UPUnipenLib.pdb
Release
UPImage.dll
UPImage.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
UPDatabase.dll
UPDatabase.pdb
UpImage-LinhLam-2.pdb
UpImage-LinhLam.dll
UPImage-LinhLam.pdb
UpImage.dll
UpImage.pdb
UPUnipenLib.dll
UPUnipenLib.pdb
DesignTimeResolveAssemblyReferencesInput.cache
UPImage.dll
UPImage.pdb
Word_samples.zip
Word samples
beach.dtl
Bengal.dtl
best.dtl
Brower.dtl
Buy.dtl
Byte byte.dtl
Change.dtl
Color.dtl
Company.dtl
Complet.dtl
copy.dtl
cup.dtl
draw tool.dtl
Eastern.dtl
Eat.dtl
eldest.dtl
Emergency.dtl
English.dtl
File.dtl
Finist.dtl
found nothing.dtl
France.dtl
Future.dtl
help me now.dtl
Hey hello.dtl
Hobby.dtl
How are you.dtl
Huck.dtl
icon.dtl
inker.dtl
Internet.dtl
Link.dtl
load.dtl
Local.dtl
Lonely.dtl
loosen.dtl
love you.dtl
Madden.dtl
Main gate.dtl
Mercy.dtl
Module.dtl
monday.dtl
Moon.dtl
mouse.dtl
my turn.dtl
net spell.dtl
network.dtl
never.dtl
newest.dtl
noted.dtl
Novel.dtl
oldest.dtl
Option.dtl
Pencil.dtl
petro.dtl
Pink.dtl
quick and slow.dtl
Rock.dtl
save.dtl
Sell.dtl
slam.dtl
smart phone.dtl
Strong.dtl
Strongest.dtl
success.dtl
Summer.dtl
Take.dtl
Text.dtl
Took.dtl
Train.dtl
Tuesday.dtl
Valence.dtl
Victory.dtl
viewer.dtl
vintage.dtl
Volume.dtl
water.dtl
Weak.dtl
Window.dtl
Windy.dtl
word expert.dtl
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Design;
using System.Globalization;

using SpellChecker.Forms;
using SpellChecker.Dictionary;
using SpellChecker.Dictionary.Affix;
using SpellChecker.Dictionary.Phonetic;

namespace SpellChecker
{
    /// <summary>
    ///		The Spelling class encapsulates the functions necessary to check
    ///		the spelling of inputted text.
    /// </summary>
    [ToolboxBitmap(typeof(SpellChecker.AutoSpelling), "Spelling.bmp")]
    public partial class AutoSpelling : Component
    {
        #region Global Regex
        // Regex are class scope and compiled to improve performance on reuse
        private Regex _digitRegex = new Regex(@"^\d", RegexOptions.Compiled);
        private Regex _htmlRegex = new Regex(@"</[c-g\d]+>|</[i-o\d]+>|</[a\d]+>|</[q-z\d]+>|<[cg]+[^>]*>|<[i-o]+[^>]*>|<[q-z]+[^>]*>|<[a]+[^>]*>|<(\[^\]*\|'[^']*'|[^'\>])*>", RegexOptions.IgnoreCase & RegexOptions.Compiled);
        private MatchCollection _htmlTags;
        private Regex _letterRegex = new Regex(@"\D", RegexOptions.Compiled);
        private Regex _upperRegex = new Regex(@"[^A-Z]", RegexOptions.Compiled);
        private Regex _wordEx = new Regex(@"\b[A-Za-z0-9_'À-ÿ]+\b", RegexOptions.Compiled);
        private MatchCollection _words;
        #endregion

        #region public properties
        private WordDictionary _dictionary;
        private bool _ignoreAllCapsWords = true;
        private bool _ignoreHtml = true;
        private bool _ignoreWordsWithDigits = false;
        private Hashtable _replaceList = new Hashtable();
        private string _replacementWord = "";
        private SuggestionEnum _suggestionMode = SuggestionEnum.PhoneticNearMiss;
        private StringBuilder _text = new StringBuilder();
        private int _wordIndex = 0;
        /// <summary>
        ///     The suggestion strategy to use when generating suggestions
        /// </summary>
        public enum SuggestionEnum
        {
            /// <summary>
            ///     Combines the phonetic and near miss strategies
            /// </summary>
            PhoneticNearMiss,
            /// <summary>
            ///     The phonetic strategy generates suggestions by word sound
            /// </summary>
            /// <remarks>
            ///		This technique was developed by the open source project ASpell.net
            /// </remarks>
            Phonetic,
            /// <summary>
            ///     The near miss strategy generates suggestion by replacing, 
            ///     removing, adding chars to make words
            /// </summary>
            /// <remarks>
            ///     This technique was developed by the open source spell checker ISpell
            /// </remarks>
            NearMiss
        }
        /// <summary>
        ///     The current word being spell checked from the text property
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string CurrentWord
        {
            get
            {
                if (_words == null || _words.Count == 0)
                    return string.Empty;
                else
                    return _words[this.WordIndex].Value;
            }
        }

        /// <summary>
        ///     The WordDictionary object to use when spell checking
        /// </summary>
        [Browsable(true)]
        [CategoryAttribute("Dictionary")]
        [Description("The WordDictionary object to use when spell checking")]
        public WordDictionary Dictionary
        {
            get
            {
                if (!base.DesignMode && _dictionary == null)
                    _dictionary = new WordDictionary();

                return _dictionary;
            }
            set
            {
                if (value != null)
                    _dictionary = value;
            }
        }
        /// <summary>
        ///     Ignore words with all capital letters when spell checking
        /// </summary>
        [DefaultValue(true)]
        [CategoryAttribute("Options")]
        [Description("Ignore words with all capital letters when spell checking")]
        public bool IgnoreAllCapsWords
        {
            get { return _ignoreAllCapsWords; }
            set { _ignoreAllCapsWords = value; }
        }
        /// <summary>
        ///     Ignore html tags when spell checking
        /// </summary>
        [DefaultValue(true)]
        [CategoryAttribute("Options")]
        [Description("Ignore html tags when spell checking")]
        public bool IgnoreHtml
        {
            get { return _ignoreHtml; }
            set { _ignoreHtml = value; }
        }
        /// <summary>
        ///     Ignore words with digits when spell checking
        /// </summary>
        [DefaultValue(false)]
        [CategoryAttribute("Options")]
        [Description("Ignore words with digits when spell checking")]
        public bool IgnoreWordsWithDigits
        {
            get { return _ignoreWordsWithDigits; }
            set { _ignoreWordsWithDigits = value; }
        }

      
        /// <summary>
        ///     List of words and replacement values to automatically replace
        /// </summary>
        /// <remarks>
        ///		When <see cref="ReplaceAllWord"/> is clicked, the <see cref="CurrentWord"/> is added to this list
        /// </remarks>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Hashtable ReplaceList
        {
            get { return _replaceList; }
        }
        /// <summary>
        ///     The suggestion strategy to use when generating suggestions
        /// </summary>
        [DefaultValue(SuggestionEnum.PhoneticNearMiss)]
        [CategoryAttribute("Options")]
        [Description("The suggestion strategy to use when generating suggestions")]
        public SuggestionEnum SuggestionMode
        {
            get { return _suggestionMode; }
            set { _suggestionMode = value; }
        }
        /// <summary>
        ///     The text to spell check
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string Text
        {
            get { return _text.ToString(); }
        }
        /// <summary>
        ///     TextIndex is the index of the current text being spell checked
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int TextIndex
        {
            get
            {
                if (_words == null || _words.Count == 0)
                    return 0;

                return _words[this.WordIndex].Index;
            }
        }
        /// <summary>
        ///     The number of words being spell checked
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int WordCount
        {
            get
            {
                if (_words == null)
                    return 0;

                return _words.Count;
            }
        }

        /// <summary>
        ///     WordIndex is the index of the current word being spell checked
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int WordIndex
        {
            get
            {
                if (_words == null)
                    return 0;

                // make sure word index can't be higher then word count
                return Math.Max(0, Math.Min(_wordIndex, (this.WordCount - 1)));
            }
            set
            {
                _wordIndex = value;
            }
        }

        #endregion

        #region Constructors
        public AutoSpelling()
        {
            InitializeComponent();
        }

        public AutoSpelling(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }
        #endregion

        #region private methods
        /// <summary>
        ///     Calculates the words from the Text property
        /// </summary>
        private void CalculateWords()
        {
            // splits the text into words
            _words = _wordEx.Matches(_text.ToString());

            // remark html
            this.MarkHtml();
        }
        /// <summary>
        ///     Determines if the string should be spell checked
        /// </summary>
        /// <param name="characters" type="string">
        ///     <para>
        ///         The Characters to check
        ///     </para>
        /// </param>
        /// <returns>
        ///     Returns true if the string should be spell checked
        /// </returns>
        private bool CheckString(string characters)
        {
            if (_ignoreAllCapsWords && !_upperRegex.IsMatch(characters))
            {
                return false;
            }
            if (_ignoreWordsWithDigits && _digitRegex.IsMatch(characters))
            {
                return false;
            }
            if (!_letterRegex.IsMatch(characters))
            {
                return false;
            }
            if (_ignoreHtml)
            {
                int startIndex = _words[this.WordIndex].Index;

                foreach (Match item in _htmlTags)
                {
                    if (startIndex >= item.Index && startIndex <= item.Index + item.Length - 1)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        private void Initialize()
        {
            if (_dictionary == null)
                _dictionary = new WordDictionary();

            if (!_dictionary.Initialized)
                _dictionary.Initialize();

        }
        /// <summary>
        ///     Calculates the position of html tags in the Text property
        /// </summary>
        private void MarkHtml()
        {
            // splits the text into words
            _htmlTags = _htmlRegex.Matches(_text.ToString());
        }

        /// <summary>
        ///     Resets the public properties
        /// </summary>
        private void Reset()
        {
            _wordIndex = 0; // reset word index
        }

        /// <summary>
        ///     Deletes the CurrentWord from the Text Property
        /// </summary>
        /// <remarks>
        ///		Note, calling ReplaceWord with the ReplacementWord property set to 
        ///		an empty string has the same behavior as DeleteWord.
        /// </remarks>
        private void DeleteWord()
        {
            if (_words == null || _words.Count == 0)
            {
                TraceWriter.TraceWarning("No Words to Delete");
                return;
            }
            string replacedWord = this.CurrentWord;
            int replacedIndex = this.WordIndex;

            int index = _words[replacedIndex].Index;
            int length = _words[replacedIndex].Length;

            // adjust length to remove extra white space after first word
            if (index == 0
                && index + length < _text.Length
                && _text[index + length] == ' ')
            {
                length++; //removing trailing space
            }
            // adjust length to remove double white space
            else if (index > 0
                && index + length < _text.Length
                && _text[index - 1] == ' '
                && _text[index + length] == ' ')
            {
                length++; //removing trailing space
            }
            // adjust index to remove extra white space before punctuation
            else if (index > 0
                && index + length < _text.Length
                && _text[index - 1] == ' '
                && char.IsPunctuation(_text[index + length]))
            {
                index--;
                length++;
            }
            // adjust index to remove extra white space before last word
            else if (index > 0
                && index + length == _text.Length
                && _text[index - 1] == ' ')
            {
                index--;
                length++;
            }

            string deletedWord = _text.ToString(index, length);
            _text.Remove(index, length);

            this.CalculateWords();
        }
        /// <summary>
        /// GetWordIndexFromTextIndex
        /// </summary>
        /// <param name="textIndex"></param>
        /// <returns></returns>
        private int GetWordIndexFromTextIndex(int textIndex)
        {
            if (_words == null || _words.Count == 0 || textIndex < 1)
            {
                TraceWriter.TraceWarning("No words to get text index from.");
                return 0;
            }

            if (_words.Count == 1)
                return 0;

            int low = 0;
            int high = _words.Count - 1;

            // binary search
            while (low <= high)
            {
                int mid = (low + high) / 2;
                int wordStartIndex = _words[mid].Index;
                int wordEndIndex = _words[mid].Index + _words[mid].Length - 1;

                // add white space to end of word by finding the start of the next word
                if ((mid + 1) < _words.Count)
                    wordEndIndex = _words[mid + 1].Index - 1;

                if (textIndex < wordStartIndex)
                    high = mid - 1;
                else if (textIndex > wordEndIndex)
                    low = mid + 1;
                else if (wordStartIndex <= textIndex && textIndex <= wordEndIndex)
                    return mid;
            }

            // return last word if not found
            return _words.Count - 1;
        }
        /// <summary>
        ///     add CurrentWord and in _replacementWord to the _replaceList
        /// </summary>
        private void ReplaceAllWord()
        {
            if (this.CurrentWord.Length == 0)
            {
                TraceWriter.TraceWarning("No current word");
                return;
            }

            // if not in list and replacement word has length
            if (!_replaceList.ContainsKey(this.CurrentWord) && _replacementWord.Length > 0)
            {
                _replaceList.Add(this.CurrentWord, _replacementWord);
            }

            this.ReplaceWord();
        }
        /// <summary>
        ///     Replaces all instances of the CurrentWord in the Text Property
        /// </summary>
        /// <param name="replacementWord" type="string">
        ///     <para>
        ///         The word to replace the CurrentWord with
        ///     </para>
        /// </param>
        private void ReplaceAllWord(string replacementWord)
        {
            _replacementWord = replacementWord;
            this.ReplaceAllWord();
        }
        /// <summary>
        ///     Replaces the instances of the CurrentWord in the Text Property
        /// </summary>
        private void ReplaceWord()
        {
            if (_words == null || _words.Count == 0 || this.CurrentWord.Length == 0)
            {
                TraceWriter.TraceWarning("No text or current word");
                return;
            }

            if (_replacementWord.Length == 0)
            {
                this.DeleteWord();
                return;
            }
            string replacedWord = this.CurrentWord;
            int replacedIndex = this.WordIndex;

            int index = _words[replacedIndex].Index;
            int length = _words[replacedIndex].Length;

            _text.Remove(index, length);
            // if first letter upper case, match case for replacement word
            if (char.IsUpper(_words[replacedIndex].ToString(), 0))
            {
                _replacementWord = _replacementWord.Substring(0, 1).ToUpper(CultureInfo.CurrentUICulture)
                    + _replacementWord.Substring(1);
            }
            _text.Insert(index, _replacementWord);

            this.CalculateWords();

        }

        /// <summary>
        ///     Replaces the instances of the CurrentWord in the Text Property
        /// </summary>
        /// <param name="replacementWord" type="string">
        ///     <para>
        ///         The word to replace the CurrentWord with
        ///     </para>
        /// </param>
        private void ReplaceWord(string replacementWord)
        {
            _replacementWord = replacementWord;
            this.ReplaceWord();
        }
        #endregion

        #region public methods
        /// <summary>
        /// auto spelling check to text
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public bool AutoSpelingCheck(string text)
        {
            bool misspelledWord = false;
            _text = new StringBuilder(text);
            this.CalculateWords();
            this.Reset();
            //automatically spelling check to text
            if (_words != null && WordCount > 0)
            {
                string currentWord = "";


                for (int i = 0; i <= WordCount-1; i++)
                {
                    _wordIndex = i;		// saving the current word index 
                    currentWord = this.CurrentWord;
                    if (CheckString(currentWord))
                    {
                        if (!this.TestWord(currentWord))
                        {
                            if(_replaceList.ContainsKey(currentWord)) 
						    {
                                _replacementWord = _replaceList[currentWord].ToString();
							    this.ReplaceWord();
						    }
                            else
                            {
                                misspelledWord = true;
                                AutoSpelingCheck();
                            }
                        }
                       
                    }
                        
                }
            }
            return misspelledWord;
            
        }
        /// <summary>
        /// 
        /// </summary>
        private void AutoSpelingCheck()
        {


            // can't generate suggestions with out current word
            if (this.CurrentWord.Length == 0)
            {
                TraceWriter.TraceWarning("No current word");
                return;
            }

            this.Initialize();
            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.
                String result;
               
                if (this.ReplaceChars(out result))
                {
                    ReplaceAllWord(result);
                }
                else if (this.BadChar(out result))
                {
                    ReplaceAllWord(result);
                }
                else if (this.ForgotChar(out result))
                {
                    ReplaceAllWord(result);
                }
                else if (this.SwapChar(out result))
                {
                    ReplaceAllWord(result);
                }
                else if (this.ExtraChar(out result))
                {
                    ReplaceAllWord(result);
                }
                else if (this.TwoWords(out result))
                {
                    ReplaceAllWord(result);
                }
            }

           
        } // suggest

        /// <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 TestWord(string word)
        {
            this.Initialize();

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

            if (this.Dictionary.Contains(word))
            {
                return true;
            }
            else if (this.Dictionary.Contains(word.ToLower()))
            {
                return true;
            }
            return false;
        }

        #endregion

        #region ISpell Near Miss Suggetion methods

        /// <summary>
        ///		swap out each char one by one and try all the tryme
        ///		chars in its place to see if that makes a good word
        /// </summary>
        private bool BadChar(out String result)
        {
            result = "";
            bool isFoundWord = false;
            for (int i = 0; i < this.CurrentWord.Length; i++)
            {
                StringBuilder tempWord = new StringBuilder(this.CurrentWord);
                char[] tryme = this.Dictionary.TryCharacters.ToCharArray();
                for (int x = 0; x < tryme.Length; x++)
                {
                    tempWord[i] = tryme[x];
                    if (this.TestWord(tempWord.ToString()))
                    {
                        result = tempWord.ToString();
                        isFoundWord = true;
                        break;
                    }
                }
            }
            return isFoundWord;
        }

        /// <summary>
        ///     try omitting one char of word at a time
        /// </summary>
        private bool ExtraChar(out String result)
        {
            result = "";
            bool isFoundWord = false;
            if (this.CurrentWord.Length > 1)
            {
                for (int i = 0; i < this.CurrentWord.Length; i++)
                {
                    StringBuilder tempWord = new StringBuilder(this.CurrentWord);
                    tempWord.Remove(i, 1);

                    if (this.TestWord(tempWord.ToString()))
                    {
                        result = tempWord.ToString();
                        isFoundWord = true;
                        break;
                    }

                }
            }
            return isFoundWord;
        }

        /// <summary>
        ///     try inserting a tryme character before every letter
        /// </summary>
        private bool ForgotChar(out String result)
        {
            result = "";
            bool isFoundWord = false;
            char[] tryme = this.Dictionary.TryCharacters.ToCharArray();

            for (int i = 0; i <= this.CurrentWord.Length; i++)
            {
                for (int x = 0; x < tryme.Length; x++)
                {
                    StringBuilder tempWord = new StringBuilder(this.CurrentWord);

                    tempWord.Insert(i, tryme[x]);
                    if (this.TestWord(tempWord.ToString()))
                    {

                        result = tempWord.ToString();
                        isFoundWord = true;
                        return isFoundWord;
                    }
                }
            }
            return isFoundWord;
        }

        /// <summary>
        ///     suggestions for a typical fault of spelling, that
        ///		differs with more, than 1 letter from the right form.
        /// </summary>
        private bool ReplaceChars(out String result)
        {
            result = "";
            bool isFoundWord = false;
            ArrayList replacementChars = this.Dictionary.ReplaceCharacters;
            for (int i = 0; i < replacementChars.Count; i++)
            {
                int split = ((string)replacementChars[i]).IndexOf(' ');
                string key = ((string)replacementChars[i]).Substring(0, split);
                string replacement = ((string)replacementChars[i]).Substring(split + 1);

                int pos = this.CurrentWord.IndexOf(key);
                while (pos > -1)
                {
                    string tempWord = this.CurrentWord.Substring(0, pos);
                    tempWord += replacement;
                    tempWord += this.CurrentWord.Substring(pos + key.Length);

                    if (this.TestWord(tempWord))
                    {

                        result = tempWord.ToString();
                        isFoundWord = true;
                        return isFoundWord;
                    }
                    pos = this.CurrentWord.IndexOf(key, pos + 1);
                }
            }
            return isFoundWord;
        }

        /// <summary>
        ///     try swapping adjacent chars one by one
        /// </summary>
        private bool SwapChar(out String result)
        {
            result = "";
            bool isFoundWord = false;
            for (int i = 0; i < this.CurrentWord.Length - 1; i++)
            {
                StringBuilder tempWord = new StringBuilder(this.CurrentWord);

                char swap = tempWord[i];
                tempWord[i] = tempWord[i + 1];
                tempWord[i + 1] = swap;

                if (this.TestWord(tempWord.ToString()))
                {

                    result = tempWord.ToString();
                    isFoundWord = true;
                    break;
                }
            }
            return isFoundWord;
        }

        /// <summary>
        ///     split the string into two pieces after every char
        ///		if both pieces are good words make them a suggestion
        /// </summary>
        private bool TwoWords(out String result)
        {
            result = "";
            bool isFoundWord = false;
            for (int i = 1; i < this.CurrentWord.Length - 1; i++)
            {
                string firstWord = this.CurrentWord.Substring(0, i);
                string secondWord = this.CurrentWord.Substring(i);

                if (this.TestWord(firstWord) && this.TestWord(secondWord))
                {
                    string tempWord = firstWord + " " + secondWord;
                    result = tempWord;
                    isFoundWord = true;
                    break;
                }
            }
            return isFoundWord;
        }

        #endregion

    }
}

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)

Share

About the Author

Vietdungiitb
Vietnam Maritime University
Vietnam Vietnam
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 10 Apr 2013
Article Copyright 2013 by Vietdungiitb
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid