Sunday, March 25, 2007

Automatic Keyword Generation

I posted a while back about the Automatic Keyword Generator PHP class by Ver Pangonilo. This works pretty well, but can only find keywords based on the text you input. It cannot generate related terms, synonyms, etc. However, the Yahoo Content Analysis Web Service can do all these things and more.

You can generate these keywords using the following function:

function implodeAssoc($array, $inner_glue='=', $outer_glue='&') {
$output = array();
foreach( $array as $key => $item ){
$output[] = $key . $inner_glue . $item;
}
return implode($outer_glue, $output);
}

function getKeywords($text) {
$baseurl = 'http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction';
$params = array('appid'=>'YahooDemo',
'context'=>urlencode($text),
'output'=>'php');

$request = $baseurl . '?' . implodeAssoc($params);
$xml = file_get_contents($request);
$result = unserialize($xml);
$keywords_array = $result['ResultSet']['Result'];
$keywords = implode(",",$keywords_array);
return($keywords);
}

echo getKeywords("Bill Gates to Finally Receive His Harvard Degree. It's not like he needs it to beef up his resume, but the world's richest college dropout is finally getting his degree. Bill Gates, chairman of Microsoft Corp.");


This will output:
bill gates,harvard degree,microsoft corp,college dropout,resume

No comments: