Jun 26 2007

Use NetSpell in your ASP.NET 2.0 application

Posted by admin under ASP.NET 2.0

This simple beginners tutorial will show you how to start using the free spellchecking component NetSpell from Loresoft from within your ASP.NET 2.0 application. When you download it

http://sourceforge.net/projects/netspell/

you will see there is a full featured ASP.NET forms example - however since I was in need of something much simpler (actually just take a string and from that list all valid english words) I will give you just that.

a) download the Netspell project and unzip.   You will now see there is a lot of code - and the best thing to do would be to recompile it under 2.0 - but we will take the easy way out and use the existing 1.1 NetSpell.SpellChecker.dll from the bin directory.

Just add a reference to it from your 2.0 application and we are ready to go. Next - we need some sort of dictionary. In the dic folder we can find the file en-US.dic. The component is "smart" enough to use the currentculture to find out the users langage and pick the "correct" dictionary file - but we want this to be a hardwired so we take the file and copy it to a /dict/ directory in the web folder.

First lets look at how I want the interface to be:

Now, we are ready to code:

ASPX file:



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SpellTest._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Enter text:
    <br />
    <asp:TextBox ID="txtText" runat="server" TextMode="MultiLine" Rows="5">
    </asp:TextBox>
    <br />
    <asp:Button ID="btnRun" runat="server" Text="Get english words" OnClick="btnRun_Click" />
    <br />
    Contains these valid english words:
    <br />
    <asp:TextBox ID="txtWords" runat="server" TextMode="MultiLine" Rows="5">
    </asp:TextBox>
    </div>
    </form>
</body>
</html>



and the codebehind:



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace SpellTest
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnRun_Click(object sender, EventArgs e)
        {

            NetSpell.SpellChecker.Dictionary.WordDictionary oDict = new NetSpell.SpellChecker.Dictionary.WordDictionary();

            oDict.DictionaryFile = MapPath(System.IO.Path.Combine(Request.ApplicationPath, "/dict/")) + "/en-US.dic";
            //load and initialize the dictionary
            oDict.Initialize();

            NetSpell.SpellChecker.Spelling oSpell = new NetSpell.SpellChecker.Spelling();
            
            oSpell.Dictionary = oDict;
            char []chDelims = {' ','\n', '\t', '\r'};
            foreach (string s in txtText.Text.Split(chDelims))
            {
                if (s.Length > 0 && oSpell.TestWord(s))
                {
                    if (txtWords.Text.Length > 0)
                        txtWords.Text += Environment.NewLine;
                    txtWords.Text += s;
                }
            }
        }
    }
}



 Now at least it's working. As you might agree there are some bad things about this solution.

a) we are using 1.1 dll from 2.0 app.

For that please look here

http://www.codeproject.com/cs/library/netspell.asp?df=100&forumid=25085&select=1831195#xx1831195xx

b) Next - why are we dragging in the whole component when all we want to verify words? I don't need suggestions etc - all I need is checking if a word is valid or not. This is where I stand right now. I will look into it and post some updates/alternative ways when I found something out.

c) it doesn't work under Medium trust. That's a big issue. You will probably get a "That assembly does not allow partially trusted callers." error. If you recompile it you can add a

using System.Security; [assembly: AllowPartiallyTrustedCallers()]

and that will be taken cared of.

 

 

 

Links