Exemplo n.º 1
0
        public string SpellCheck(string requestId, string lang, string[] words)
        {
            //ArrayList misspelledWords = new ArrayList();
            StringBuilder json = new StringBuilder();

            json.Append("{");
            json.Append("\"id\":\"" + requestId + "\",\"result\":{");

            string result, wordsStr;

            // Send request to google
            wordsStr = String.Join(" ", words);
            result   = this.SendRequest(lang, wordsStr);


            // Parse json response
            var js         = new JavaScriptSerializer();
            var resultData = js.Deserialize <GoogleResponse>(result);

            // Get list of misspelled words
            if (resultData.result != null && resultData.result.spellingCheckResponse != null)
            {
                string comma = string.Empty;
                foreach (GoogleResponseMisspelling misspelling in resultData.result.spellingCheckResponse.misspellings)
                {
                    json.Append(comma);
                    GoogleMisspellingResult      resultItem = new GoogleMisspellingResult();
                    GoogleResponseMisspelledWord w          = new GoogleResponseMisspelledWord();
                    w.word                 = wordsStr.Substring(misspelling.charStart, misspelling.charLength);
                    resultItem.word        = w;
                    resultItem.suggestions = misspelling.suggestions;
                    //misspelledWords.Add(wordsStr.Substring(misspelling.charStart, misspelling.charLength));
                    //misspelledWords.Add(resultItem);
                    resultItem.ToJsonFragment(json);
                    comma = ",";
                }
            }

            json.Append("}}");

            return(json.ToString());
            //return js.Serialize(misspelledWords);
            //return (string[])misspelledWords.ToArray(typeof(string));
        }
Exemplo n.º 2
0
        public string SpellCheck(string requestId, string lang, string[] words)
        {
            //ArrayList misspelledWords = new ArrayList();
            StringBuilder json = new StringBuilder();
            json.Append("{");
            json.Append("\"id\":\"" + requestId + "\",\"result\":{");

            string result, wordsStr;

            // Send request to google
            wordsStr = String.Join(" ", words);
            result = this.SendRequest(lang, wordsStr);

            // Parse json response
            var js = new JavaScriptSerializer();
            var resultData = js.Deserialize<GoogleResponse>(result);

            // Get list of misspelled words
            if (resultData.result != null && resultData.result.spellingCheckResponse != null)
            {
                string comma = string.Empty;
                foreach (GoogleResponseMisspelling misspelling in resultData.result.spellingCheckResponse.misspellings)
                {
                    json.Append(comma);
                    GoogleMisspellingResult resultItem = new GoogleMisspellingResult();
                    GoogleResponseMisspelledWord w = new GoogleResponseMisspelledWord();
                    w.word = wordsStr.Substring(misspelling.charStart, misspelling.charLength);
                    resultItem.word = w;
                    resultItem.suggestions = misspelling.suggestions;
                    //misspelledWords.Add(wordsStr.Substring(misspelling.charStart, misspelling.charLength));
                    //misspelledWords.Add(resultItem);
                    resultItem.ToJsonFragment(json);
                    comma = ",";
                }
            }

            json.Append("}}");

            return json.ToString();
            //return js.Serialize(misspelledWords);
            //return (string[])misspelledWords.ToArray(typeof(string));
        }