public void ProcessRequest(HttpContext context) { SpellCheckerInput input = SpellCheckerInput.Parse(new StreamReader(context.Request.InputStream)); SpellCheckerResult suggestions = null; switch (input.Method) { case "checkWords": suggestions = CheckWords(input.Language, input.Words.ToArray()); break; case "getSuggestions": suggestions = GetSuggestions(input.Language, input.Words[0]); break; default: suggestions = new SpellCheckerResult(); break; } suggestions.id = input.Id; JavaScriptSerializer ser = new JavaScriptSerializer(); var res = ser.Serialize(suggestions); context.Response.Write(res); }
/// <summary> /// Checks all the words submitted against Google for spelling /// </summary> /// <param name="language">The language code.</param> /// <param name="words">The words to be checked.</param> /// <returns></returns> public override SpellCheckerResult CheckWords(string language, string[] words) { XmlDocument document = new XmlDocument(); string data = string.Join(" ", words); //turn them into a space-separated string as that's what google takes string xml = SendRequest(language, data); document.LoadXml(xml); var res = new SpellCheckerResult(); foreach (XmlNode node in document.SelectNodes("//c")) //go through each of the incorrectly spelt words { XmlElement element = (XmlElement)node; res.result.Add(data.Substring(Convert.ToInt32(element.GetAttribute("o")), Convert.ToInt32(element.GetAttribute("l")))); } return res; }
/// <summary> /// Checks all the words submitted against Google for spelling /// </summary> /// <param name="language">The language code.</param> /// <param name="words">The words to be checked.</param> /// <returns></returns> public override SpellCheckerResult CheckWords(string language, string[] words) { XmlDocument document = new XmlDocument(); string data = string.Join(" ", words); //turn them into a space-separated string as that's what google takes string xml = SendRequest(language, data); document.LoadXml(xml); var res = new SpellCheckerResult(); foreach (XmlNode node in document.SelectNodes("//c")) //go through each of the incorrectly spelt words { XmlElement element = (XmlElement)node; res.result.Add(data.Substring(Convert.ToInt32(element.GetAttribute("o")), Convert.ToInt32(element.GetAttribute("l")))); } return(res); }
/// <summary> /// Gets the suggested spelling for a misspelt word /// </summary> /// <param name="language">The language the word is.</param> /// <param name="word">The word that is misspelt.</param> /// <returns></returns> public override SpellCheckerResult GetSuggestions(string language, string word) { XmlDocument document = new XmlDocument(); string xml = SendRequest(language, word); document.LoadXml(xml); var res = new SpellCheckerResult(); foreach (XmlNode node in document.SelectNodes("//c")) //select each incorrectly spelt work { XmlElement element = (XmlElement)node; foreach (string s in element.InnerText.Split(new char[] { '\t' })) //they are tab-separated for suggestions { if (!string.IsNullOrEmpty(s)) { res.result.Add(s); } } } return(res); }
/// <summary> /// Gets the suggested spelling for a misspelt word /// </summary> /// <param name="language">The language the word is.</param> /// <param name="word">The word that is misspelt.</param> /// <returns></returns> public override SpellCheckerResult GetSuggestions(string language, string word) { XmlDocument document = new XmlDocument(); string xml = SendRequest(language, word); document.LoadXml(xml); var res = new SpellCheckerResult(); foreach (XmlNode node in document.SelectNodes("//c")) //select each incorrectly spelt work { XmlElement element = (XmlElement)node; foreach (string s in element.InnerText.Split(new char[] { '\t' })) //they are tab-separated for suggestions { if (!string.IsNullOrEmpty(s)) { res.result.Add(s); } } } return res; }
public void ProcessRequest(HttpContext context) { SpellCheckerInput input = SpellCheckerInput.Parse(new StreamReader(context.Request.InputStream)); SpellCheckerResult suggestions = null; switch (input.Method) { case "checkWords": suggestions = CheckWords(input.Language, input.Words.ToArray()); break; case "getSuggestions": suggestions = GetSuggestions(input.Language, input.Words[0]); break; default: suggestions = new SpellCheckerResult(); break; } suggestions.id = input.Id; JavaScriptSerializer ser = new JavaScriptSerializer(); var res = ser.Serialize(suggestions); context.Response.Write(res); }