Pages

Thursday, August 26, 2010

Spell checker API

Function below uses Yahoo API.

function yahoo_spell_check ($query)
{
// Substitute this application ID with your own application ID provided by Yahoo!.
$appID = "APP_ID";

// URI used for making REST call. Each Web Service uses a unique URL.
$request = "http://search.yahooapis.com/WebSearchService/V1/spellingSuggestion? appid=$appID&query=".urlencode($query);

// Initialize the session by passing the request as a parameter
$session = curl_init($request);

// Set curl options by passing session and flags
// CURLOPT_HEADER allows us to receive the HTTP header
curl_setopt($session, CURLOPT_HEADER, true);

// CURLOPT_RETURNTRANSFER will return the response
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// Make the request
$response = curl_exec($session);

// Close the curl session
curl_close($session);

// Get the XML from the response, bypassing the header
if (!($xml = strstr($response, 'Result;

if($data[0] == '')
{
return $query;
}

return $data[0];
}






Function below uses Google's API.

function google_spell_check($query)
{
$url="https://www.google.com/tbproxy/spell?lang=en";// . $_GET['lang'];
$ignoredigits = 1;
$ignorecaps = 1;

$text = urldecode($query);

$body = '';
$body .= '';
$body .= '' . $text . '';
$body .= '
';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);

/* $ttt = XML2Array($contents);
//print_r($ttt);
//foreach($ttt as $key=>$val)
//echo "$key => $val\n";
extract($ttt);
$cc = explode(" ", trim($c));
return $cc[0];
*/


$xml = simplexml_load_string($contents);
$return_str = "";

foreach($xml->children() as $child)
{
$child_arr = preg_split("/[\s]+/", $child);
$return_str .= $child_arr[0] . " ";
}

if($return_str == '')
{
return strtolower($query);
}

return strtolower(trim($return_str));
}

No comments:

Post a Comment