Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 118,481 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 960 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Spell Checker Help

 
Reply to this topicStart new topic

Spell Checker Help, Spell Checker Help Needed

gbertoli3
post 6 Jul, 2008 - 12:28 PM
Post #1


DIC at Heart + Code

Group Icon
Joined: 23 Jun, 2008
Posts: 804



Thanked 11 times

Dream Kudos: 700
My Contributions


Here are some things you should know before I ask my question.

There is a function called SpellChecker, here is the code.

CODE

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;

namespace SpellCheck
{
    public class SpellChecker
    {
        // Class declarations
        private StringCollection scDictionary = new StringCollection();
        private StringCollection scNotFound = new StringCollection();
        private string strMarkedText;
        private string strStatus;

        // Properties
        public StringCollection Dictionary { get { return scDictionary; } }
        public StringCollection NotFound { get { return scNotFound; } }
        public string MarkedText { get { return strMarkedText; } }
        public string Status { get { return strStatus; } }

        // Constructor
        public SpellChecker(string Text, string DictionaryFile, string Prefix, string Suffix)
        {
            strStatus = "";
            // Load Dictionary file
            ReadDictionaryFile(DictionaryFile);
            if (strStatus == "")
            {
                // Split the provided text into a string array of words
                string[] strWords = SplitWords(Text);
                // Build collection of "not found" words
                CheckWords(strWords);
                // Create a new string with potentially incorrect words marked
                strMarkedText = MarkText(Text, Prefix, Suffix);
            }
        }

        // Load dictionary
        private void ReadDictionaryFile(string strFilename)
        {
            string strLine;
            StreamReader srDictionary = null;
            System.Diagnostics.Debug.WriteLine("Reading dictionary");
            try
            {
                if (File.Exists(strFilename))
                {
                    // Create new StreamReader for dictionary file
                    srDictionary = new StreamReader(strFilename);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("Not Found:" + strFilename);
                    return;
                }


                // Get initial word
                strLine = srDictionary.ReadLine();

                // Read while not EOF
                while (strLine != null)
                {
                    // Add line to StringCollection
                    scDictionary.Add(strLine.ToLower());
                    // Get next word
                    strLine = srDictionary.ReadLine();
                }
                System.Diagnostics.Debug.WriteLine("Done reading dictionary");
            }
            catch (Exception ex)
            {
                strStatus = ex.Message;
                System.Diagnostics.Debug.WriteLine(strStatus);
            }
            finally
            {
                if(srDictionary != null)
                    srDictionary.Close();
            }
        }

        // Split text into individual words
        private string[] SplitWords(string strText)
        {
            System.Diagnostics.Debug.WriteLine("Splitting words");
            // Define string delimiters
            char[] cDelimiters = { ' ', ',', '.', '?',
                ':', '/', '=', '~', '+', '-', '\"',
                '\t', '\r', '\n', '“', '”', '`', '’',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                '#', '$', '&', '%', '@', '!', '(', ')' };

            // Split the provided text into individual words
            string[] straWords = strText.Split(cDelimiters);
            System.Diagnostics.Debug.WriteLine("Done splitting words");
            return straWords;
        }

        // Determine whether each word exists in the dictionary
        private void CheckWords(string[] strWords)
        {
            System.Diagnostics.Debug.WriteLine("Building word list");
            // Build a list of words that weren't found in the dictionary
            foreach (string Word in strWords)
            {
                // If word lengh = 0, it may be a control character, so skip it
                // Otherwise, go ahead and try to add it to the missing word list
                if (!scDictionary.Contains(Word.ToLower()) && Word.Length > 0)
                {
                    // If the missing word list doesn't already have this word, add it
                    if (!scNotFound.Contains(Word.ToLower()))
                        scNotFound.Add(Word);
                }
            }
            System.Diagnostics.Debug.WriteLine("Done building word list");
        }

        // Mark original text with provided prefix and suffix strings
        private string MarkText(string Text, string strPrefix, string strSuffix)
        {
            System.Diagnostics.Debug.WriteLine("Marking text");
            string strText = Text;
            string strReplacement = "";
            // Mark not-found words in original text
            foreach (string Word in scNotFound)
            {
                strReplacement = (strPrefix + Word + strSuffix);
                strText = Regex.Replace(strText, Word, strReplacement);
            }
            System.Diagnostics.Debug.WriteLine("Done marking text");
            return strText;
        }
    }
}


Ok now that you have the spellchecker code I can ask my question.

My question is...

How can I check all of the text in a richTextBox(txtBox) for any errors?

I also have a text document that contains common words called "words.txt" it is like a mini dictionary.

This is how I use the spellchecker.
CODE

SpellCheck.SpellChecker spellcheck = new SpellCheck.SpellChecker(txtBox.Text, "words.txt", "", "");
User is offlineProfile CardPM

Go to the top of the page


zakary
post 7 Jul, 2008 - 04:46 AM
Post #2


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 385



Thanked 4 times

Dream Kudos: 175
My Contributions


what you can use a StreamReader to open your words.txt file and check to see if that word is in the list.
User is offlineProfile CardPM

Go to the top of the page

gbertoli3
post 18 Jul, 2008 - 12:02 PM
Post #3


DIC at Heart + Code

Group Icon
Joined: 23 Jun, 2008
Posts: 804



Thanked 11 times

Dream Kudos: 700
My Contributions


QUOTE(zakary @ 7 Jul, 2008 - 04:46 AM) *

what you can use a StreamReader to open your words.txt file and check to see if that word is in the list.


Sorry to respond so late but what do you mean by a StreamReader. Because I tried that here is my code...
CODE

            System.IO.StreamReader reader = new System.IO.StreamReader("words.txt");
            SpellCheck.SpellChecker spellcheck = new SpellCheck.SpellChecker(reader.ReadLine(), "words.txt", "", "");
            reader.Close();


what would I put between the spellcheck and the reader.Close().

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/11/08 06:08AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month