Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

c# - Google Spell Checker API

Does anyone know? I pass spell check "accelerants" which is a perfectly good word. I get back "accelerates"? When I open Google in a browser and type "accelerants" it does NOT suggest "accelerates"?

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace SpellCheck
{
    class googel_SP
    {
        public string word;
        public void run_G()
        {
            string retValue = string.Empty;
            string Iword = "accelerants";

            try
            {
                string uri = "https://www.google.com/tbproxy/spell?lang=en:";
                using (WebClient webclient = new WebClient())
                {
                    string postData = string.Format("<?xml version="1.0"     encoding="utf-8" ?> " 
                    + "<spellrequest textalreadyclipped="0" ignoredups="0"     ignoredigits="1" "
                    + "ignoreallcaps="1"><text>{0}</text></spellrequest>", Iword);

                    webclient.Headers.Add("Content-Type", "application/x-www-form-    urlencoded");
                    byte[] bytes = Encoding.ASCII.GetBytes(postData);
                    byte[] response = webclient.UploadData(uri, "POST", bytes);
                    string data = Encoding.ASCII.GetString(response);
                    if (data != string.Empty)
                    {
                        retValue = Regex.Replace(data, @"<(.|
)*?>",     string.Empty).Split('')[0];
                        Console.WriteLine(" word in -> " + word + " word out -> " +      retValue);
                    }
                }
            }
            catch (Exception exp)
            {

            }
            //return retValue;
        }
    }

 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Interesting... I ran your code and if I deliberately pass "accelrants" as the search term, it correctly returns "accelerants". However if I pass "accelerants", it returns "accelerates". Changing the language and text encoding doesn't seem to make a difference.

Here's alternate code that will do the same job..obviously needs a bit of error handling but you get the idea :)

string retValue = string.Empty;
word = "accelerants";

string uri = string.Format( "http://www.google.com/complete/search?output=toolbar&q={0}", word );

HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create( uri );
HttpWebResponse response = ( HttpWebResponse ) request.GetResponse( );

using ( StreamReader sr = new StreamReader( response.GetResponseStream( ) ) ) {
    retValue = sr.ReadToEnd( );
}

XDocument doc = XDocument.Parse( retValue );

XAttribute attr = doc.Root.Element( "CompleteSuggestion" ).Element( "suggestion" ).Attribute( "data" );

string correctedWord = attr.Value;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...