Reputation: 49
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;
}
}
}
Upvotes: 5
Views: 9092
Reputation: 122
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;
Upvotes: 3