谷歌拼写检查器API [英] Google Spell Checker API

查看:192
本文介绍了谷歌拼写检查器API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道吗?我通过拼写检查促进剂,这是一个非常好的词。我回来的加快?当我在浏览器中打开谷歌键入促进剂,它并不意味着加速?

 使用系统;
使用System.Net;
使用System.Text;
使用System.Text.RegularEx pressions;命名空间拼写检查
{
    类googel_SP
    {
        公共字符串字;
        公共无效run_G()
        {
            字符串retValue =的String.Empty;
            字符串Iword =促进剂;            尝试
            {
                字符串URI =htt​​ps://www.google.com/tbproxy/spell?lang=en:
                使用(Web客户端Web客户端=新的WebClient())
                {
                    字符串POSTDATA =的String.Format(< XML版本= \\1.0 \\编码= \\UTF-8 \\>?
                    +< spellrequest textalreadyclipped = \\0 \\ignoredups = \\0 \\ignoredigits = \\1 \\
                    +ignoreallcaps = \\1 \\><文字和GT; {0}< /文本>< / spellrequest>中,Iword);                    webclient.Headers.Add(内容类型,应用程序/ X WWW的形式 - urlen codeD);
                    字节[]字节= Encoding.ASCII.GetBytes(POSTDATA);
                    字节[] =响应webclient.UploadData(URI,POST,字节);
                    字符串数据= Encoding.ASCII.GetString(响应);
                    如果(数据!=的String.Empty)
                    {
                        retValue = Regex.Replace(数据,@≤(| \\ n)*方式>?的String.Empty).Split('\\ t')[0];
                        Console.WriteLine(字 - >中字+ +这个词 - >中+ retValue);
                    }
                }
            }
            赶上(例外EXP)
            {            }
            //返回retValue;
        }
    } }


解决方案

有趣......我跑你code,如果我故意通过accelrants作为搜索词,它正确返回促进剂。但是,如果我通过促进剂,则返回的加速。更改语言和文字编码似乎不有所作为。

下面的备用code,会做同样的job..obviously需要一些错误处理,但你的想法:)

 字符串retValue =的String.Empty;
字=促进剂;字符串URI =的String.Format(http://www.google.com/complete/search?output=toolbar&q={0}字);HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(URI);
HttpWebResponse响应=(HttpWebResponse)request.GetResponse();使用(StreamReader的SR =新的StreamReader(response.GetResponseStream())){
    retValue = sr.ReadToEnd();
}DOC的XDocument = XDocument.Parse(retValue);XAttribute ATTR = doc.Root.Element(CompleteSuggestion).Element(建议),属性约(数据);字符串correctedWord = attr.Value;

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

            }
            //return retValue;
        }
    }

 }

解决方案

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;

这篇关于谷歌拼写检查器API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆