【问题标题】:Google Spell Checker API谷歌拼写检查 API
【发布时间】:2014-06-26 05:46:05
【问题描述】:

有人知道吗?我通过了拼写检查“加速器”,这是一个非常好的词。我回来“加速”?当我在浏览器中打开 Google 并输入“加速器”时,它不会提示“加速器”?

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, @"<(.|\n)*?>",     string.Empty).Split('\t')[0];
                        Console.WriteLine(" word in -> " + word + " word out -> " +      retValue);
                    }
                }
            }
            catch (Exception exp)
            {

            }
            //return retValue;
        }
    }

 }

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    有趣...我运行了您的代码,如果我故意将“accelrants”作为搜索词传递,它会正确返回“accelants”。但是,如果我通过“加速器”,它会返回“加速器”。更改语言和文本编码似乎没有什么区别。

    这是可以完成相同工作的替代代码..显然需要一些错误处理,但你明白了 :)

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-15
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      相关资源
      最近更新 更多