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));
}

Utility Functions

Here is the list of some of the important Utility functions.

I have this function as a member function in my Utility Class.


function mb_unserialize($serial_str) {
$out = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str );
return unserialize($out);
}


function getValidFileName($str)
{
return preg_replace('/[^0-9a-z?-????\`\~\!\@\#\$\%\^\*\(\)\; \,\.\'\/\_\-]/i', ' ',$str);
}


function XML2Array ( $xml , $recursive = false )
{
if ( ! $recursive )
{
$array = simplexml_load_string ( $xml ) ;
}
else
{
$array = $xml ;
}

$newArray = array () ;
$array = ( array ) $array ;
foreach ( $array as $key => $value )
{
$value = ( array ) $value ;
if ( isset ( $value [ 0 ] ) )
{
$newArray [ $key ] = trim ( $value [ 0 ] ) ;
}
else
{
$newArray [ $key ] = XML2Array ( $value , true ) ;
}
}
return $newArray ;
}


function set_flash($text,$type) {
$flash['type'] = $type;
$flash['text'] = $text;
$_SESSION['flash'] = serialize($flash);
}

function get_flash() {
if ($flash = $_SESSION['flash']) {
$_SESSION['flash'] = array();
return unserialize($flash);
} else {
return "";
}
}


function smartslashes($text) {
$magic_quotes_gpc = (bool) ini_get('magic_quotes_gpc');
if (!$magic_quotes_gpc) {
return addslashes($text);
} else {
return $text;
}
}

function check_email_address($email_address)
{
//returns 1 if valid email address (only numeric string), 0 if not
if (eregi("^[\+_a-z0-9-]+(\.[\+_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email_address))
return 1;
else
return 0;
}