Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we run jquery spellchecker by badsyntax using asp.net web services?

I have been researching for quite a while now for an appropriate spell checker for textbox and multiline textbox.

I can see that this functionality is built in for firefox but its not present in chrome.

I was checking out all the demos for spellcheck which is provided by jquery spellchecker - badsyntax and i find it really nice and useful.

here is the link http://jquery-spellchecker.badsyntax.co/

But my problem here is- spellchecker makes use of php webservices and i want to use it in my ASP.net Webapplication.

is there any work around so that i can run it using asp.net web services?

Please provide me with a solution.

like image 708
Veerendra Prabhu Avatar asked Dec 29 '25 14:12

Veerendra Prabhu


2 Answers

I'm the author of the plugin and would really like to incorporate some other webservice implementations.

This popped up recently in my google alerts, I can't verify it works though:

  • http://www.yodotnet.com/post/2013/02/24/Jquery-Spellchecker-for-NET.aspx
  • https://github.com/AnthonyTerra/ASP.MVC-Jquery-Spellchecker
like image 97
badsyntax Avatar answered Jan 02 '26 01:01

badsyntax


Hi Guys: Veerendra Prabhu, badsyntax; I've integrated Nhunspell and asp.net Web Service (.asmx) and currently I'm trying to integrate jquery spellchecker - badsyntax to the proyect, I'm jquery spellchecker now conects with my web services but Im still dealing whith the returned data type of my web service to allow jquery spellcheker does its magic, but I thinks its something.

I took ideas from:

deepinthecode.com

Here are some ideas that I used:

I used NHusnpell that helps to get the incorrect words within a passed text and look into the dictionary (open office downloaded as .oxt but you have to chante to zip to get en_US.aff, and en_US.dic) this files needs to be at bin directory.

At Glabal.asax file I created a static instance of NHuspell that does all the work.

public class Global : System.Web.HttpApplication
{
    static SpellEngine spellEngine;
    static public SpellEngine SpellEngine { get { return spellEngine; } }

    protected void Application_Start(object sender, EventArgs e)
    {
        try
        {
            string dictionaryPath = Server.MapPath("Bin") + "\\";
            Hunspell.NativeDllPath = dictionaryPath;

            spellEngine = new SpellEngine();
            LanguageConfig enConfig = new LanguageConfig();
            enConfig.LanguageCode = "en";
            enConfig.HunspellAffFile = dictionaryPath + "en_us.aff";
            enConfig.HunspellDictFile = dictionaryPath + "en_us.dic";
            enConfig.HunspellKey = "";
            //enConfig.HyphenDictFile = dictionaryPath + "hyph_en_us.dic";
            spellEngine.AddLanguage(enConfig);
        }
        catch (Exception ex)
        {
            if (spellEngine != null)
                spellEngine.Dispose();
        }
    }

    ...

    protected void Application_End(object sender, EventArgs e)
    {
        if (spellEngine != null)
            spellEngine.Dispose();
        spellEngine = null;

    }
}

Then I created an ASMX web service for the Get_Incorrect_Words and Get_Suggestions methods

 /// <summary>
/// Summary description for SpellCheckerService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    
[ScriptService()]
public class SpellCheckerService : System.Web.Services.WebService
{      

    [WebMethod]
    //[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string get_incorrect_words(string words)
    {
        Dictionary<string, string> IncorrectWords = new Dictionary<string, string>();

        List<string> wrongWords = new List<string>();

        var palabras = words.Split(' ');

        // Check spelling of each word that has been passed to the method
        foreach (string word in palabras)
        {
            bool correct = Global.SpellEngine["en"].Spell(word);

            if (!correct){

                wrongWords.Add(word);
            }
        }

        IncorrectWords.Add("data", wrongWords[0]);

        return wrongWords[0];
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<string> get_suggestions(string word)
    {
        List<string> suggestions = new List<string>();
        suggestions = Global.SpellEngine["en"].Suggest(word);
        suggestions.Sort();
        return suggestions;
    }

And Finally I modified the calls to the get_incorrect_words and get_suggestions in jquery.Spellchecker.js

 /* Config
   *************************/

  var defaultConfig = {
    lang: 'en',
    webservice: {
        path: 'SpellCheckerService.asmx/get_incorrect_words'
      //,driver: 'LabNET'
    },
    local: {
      requestError: 'There was an error processing the request.',
      ignoreWord: 'Ignore word',
      ignoreAll: 'Ignore all',
      ignoreForever: 'Add to dictionary',
      loading: 'Loading...',
      noSuggestions: '(No suggestions)'
    },
    suggestBox: {
      numWords: 5,
      position: 'above',
      offset: 2,
      appendTo: null
    },
    incorrectWords: {
      container: 'body', //selector
      position: null //function
    }
  };

  var pluginName = 'spellchecker';

...

     /* Spellchecker web service
   *************************/

  var WebService = function(config) {

    this.config = config;

    this.defaultConfig = {
      url: config.webservice.path,
      //contentType: "application/json; charset=utf-8",
      type: 'POST',
      dataType: 'text',
      cache: false,
      data: JSON.stringify({
        lang: config.lang,
        driver: config.webservice.driver
      }, null,2) ,
      error: function() {
        alert(config.local.requestError);
      }.bind(this)
    };
  };

  WebService.prototype.makeRequest = function(config) {

    var defaultConfig = $.extend(true, {}, this.defaultConfig);

    return $.ajax($.extend(true, defaultConfig, config));
  };

  WebService.prototype.checkWords = function (text, callback) {
      //action: 'get_incorrect_words',
      //JSON.stringify({

      //    text: text
      //}, null, 2)

      return this.makeRequest(
          {
              data: { words: text },
              success: callback
    });
  };

  WebService.prototype.getSuggestions = function (word, callback) {
      //action: 'get_suggestions',
    return this.makeRequest({
        data: JSON.stringify({

        word: word
      }, null, 2),
      success: callback
    });
  };
like image 40
Victor R Hdez Avatar answered Jan 02 '26 01:01

Victor R Hdez