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", "", "");