C# .NET - how to develop word verification?

Asked By chinna samy on 29-Oct-08 03:13 AM
how to develop word verification?

Solution

san san replied to chinna samy on 29-Oct-08 04:58 AM
Hi

There are may tools available for word verification or checking spelling as you type...
Or You can write your own stuff.

Use dll that comes with MS word.
Use word.dll. The bellow code shows how to use that.
 

About Word.dll:

Word.dll has one public class and two public methods

1. Public Class SpellCheck

   Include "using Word.dll" at the top of file for the object reference.

   SpellCheck word = new SpellCheck();

2. Public Method CheckSpelling

   This method will check the word and return true or false. If the word is correct then it will return true otherwise false.

   bool status = false;

   status = word.CheckSpelling("a word");

3. Public Method GetSpellingSuggestions

   This method will return the collection of suggested words.

 foreach(string suggestion in word.GetSpellingSuggestions("a word"))
 {
 System.Console.WriteLine( suggestion );
 }

 
Parser Technique:

1. Connect to spellcheck.net site and pass the word.

2. Look for the word "correctly." in html file, if found return true

3. else look for the word "misspelled.", if found return false.

    regular expression pattern @"(correctly.)|(misspelled.)"

4. If the word misspelled found in html then look for the word "suggestions:"

   regular expression pattern  @"(suggestions:)"

5. and parse the string between <blockquote>

   regular expression pattern  @"<blockquote>(?:\s*([^<]+) \s*)+ </blockquote>"

6. and finally return the collection of suggested words.

 
C# code:
-----------------

Calling Word.dll wrapper class:

This is how you would call this wrapper class in your application.

using System;
//Word.dll
using Word;
///
/// Test Harness for SpellCheck Class
///

class TestHarness
{
///
/// testing Word Class
///

[STAThread]
static void Main(string[] args)
{
SpellCheck word = new SpellCheck();

bool status = false;
string s = "youes";

Console.WriteLine("Checking for word : " + s );

// check to see if the word is not correct 
// return the bool (true|false)

status = word.CheckSpelling(s);

if (status == false)
{
Console.WriteLine("This word is misspelled : " + s);
Console.WriteLine("Here are some suggestions");
Console.WriteLine("-------------------------");

foreach( string suggestion in word.GetSpellingSuggestions(s) )
{
System.Console.WriteLine( suggestion );
}
}
else if (status == true)
{
Console.WriteLine("This word is correct : " + s );
}

}
}

--------------------
Hope this helps
SAN

How to develop word verification?

Kalit Sikka replied to chinna samy on 29-Oct-08 07:08 AM
public string CreateRandomCode(int codeCount)
{ 
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{ 
if (temp != -1)
{ 
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(36);
if (temp != -1 && temp == t)
{ 
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}  
Then we generate verification code character string as exported image
private void CreateImage(string checkCode)
{ 
System.Drawing.Bitmap image = new System.Drawing.Bitmap(Convert.ToInt32(Math.Ceiling((decimal)(checkCode.Length * 14))), 22);
Graphics g = Graphics.FromImage(image);
try
{ 
Random random = new Random();
g.Clear(Color.AliceBlue);
for (int i = 0; i < 25; i++)
{ 
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Comic Sans MS", 12, System.Drawing.FontStyle.Bold);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, new SolidBrush(Color.Red), 2, 2);
for (int i = 0; i < 100; i++)
{ 
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{ 
g.Dispose();
image.Dispose();
}
}  
Transfer function on Page_load
protected void Page_Load(object sender, EventArgs e)
{ 
string checkCode = this.CreateRandomCode(6);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
}

using bool method to return an instance.

Andy Hirst replied to chinna samy on 13-Jul-09 05:09 AM
Hi, I'm wanting to using a simple bool method to validate a textbox to see if it contains a "@" symbol before deciding whether to save the record or not. So far I have the following however I'm having issues with it. Any help in where I'm going wrong would be greatly appreciated.


//Part of a method
             if (ValidateEmail() = true)
            {
                settings.AdminEmail = txtEmailAddress.Text;
            }
            settings.Save(); 


    public bool ValidateEmail()
    {
        if (txtEmailAddress.Text.Contains("@"))
        {
            return true;
        }
        else
        {
            lblInfo.Text = "Invalid email address";
            return false;
        }           
    }