View source
<?php
define('ALCHEMY_DEBUG', FALSE);
function alchemy_menu() {
$items = array();
$items['alchemy/util'] = array(
'title' => t('Alchemy Util'),
'page callback' => 'alchemy_util',
'access callback' => 'user_access',
'access arguments' => array(
'access alchemy',
),
'type' => MENU_LOCAL_TASK,
);
$items['alchemy'] = array(
'title' => t('Alchemy Keywords'),
'page callback' => 'alchemy_get_elements_from_node',
'access callback' => 'user_access',
'access arguments' => array(
'access alchemy',
),
'type' => MENU_LOCAL_TASK,
);
$items['admin/settings/alchemy'] = array(
'title' => t('Alchemy'),
'description' => 'Settings for Alchemy.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'alchemy_admin_settings',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'alchemy.admin.inc',
);
if (function_exists('alchemy_contentanalysis_menu') || function_exists('alchemy_tagging_suggest_menu')) {
$items['admin/settings/alchemy/general'] = $items['admin/settings/alchemy'];
$items['admin/settings/alchemy/general']['title'] = t('General');
$items['admin/settings/alchemy/general']['type'] = MENU_LOCAL_TASK;
$items['admin/settings/alchemy/general']['weight'] = -10;
}
return $items;
}
function alchemy_util($node = NULL, $type = NULL) {
if (!$node) {
$node = 1;
}
if (!$type) {
$type = 'keywords';
}
$r = alchemy_get_elements_from_node($node, $type);
dsm($r);
print_r($r);
return 'OK';
}
function alchemy_get_elements_from_node($node, $type = 'keywords', $output = 'normalized', $use_cache = FALSE) {
if (!is_object($node) && is_numeric($node)) {
$node = node_load($node);
}
if (!$node) {
return array();
}
$text = strip_tags($node->title . " " . $node->body);
$elements = alchemy_get_elements($text, $type, $output = 'normalized', $node->nid, TRUE);
return $elements;
}
function alchemy_get_elements($text, $type = 'keywords', $output = 'normalized', $cid = 0, $use_cache = FALSE) {
if ($use_cache && $cid) {
$resultstr = alchemy_cache_get($cid, $type);
}
if (!$resultstr) {
$alchemyObj = alchemy_new_alchemy_obj();
switch ($type) {
case 'entities':
$resultstr = $alchemyObj
->TextGetRankedNamedEntities($text);
break;
case 'categories':
$resultstr = $alchemyObj
->TextGetCategory($text);
break;
case 'concepts':
$resultstr = $alchemyObj
->TextGetRankedConcepts($text);
break;
case 'keywords':
$resultstr = $alchemyObj
->TextGetRankedKeywords($text);
break;
}
if ($cid) {
alchemy_cache_set($cid, $type, $resultstr);
}
}
if ($output == 'xml') {
return $resultstr;
}
$result = alchemy_xml2array($resultstr);
if ($output == 'array') {
return $result;
}
$elements = array();
switch ($type) {
case 'entities':
$elms = $result->entities->entity;
break;
case 'categories':
$elms = array();
break;
case 'concepts':
$elms = $result->concepts->concept;
break;
case 'keywords':
$elms = $result->keywords->keyword;
break;
}
if (is_object($elms)) {
foreach ($elms as $v) {
$hash = (string) $v->text;
$elements[$hash] = array(
'term' => (string) $v->text,
'relevance' => (double) $v->relevance,
);
if ($v->type) {
$elements[$hash]['type'] = (string) $v->type;
}
if ($v->count) {
$elements[$hash]['count'] = (string) $v->count;
}
if ($v->disambiguated) {
$elements[$hash]['disambiguated'] = $v->disambiguated;
}
}
}
return $elements;
}
function alchemy_include_alchemy_class() {
$file = './' . drupal_get_path('module', 'alchemy') . '/AlchemyAPI/module/AlchemyAPI.php';
if (!file_exists($file)) {
$msg = t('The Alchemy module requires the Alchemy SDK. Use the PHP version of the SDK');
$msg .= l(t('Download the SDK here.'), 'http://www.alchemyapi.com/tools/', array(
'attributes' => array(
'target' => 'alchemy',
),
));
$msg .= "<br><br>";
$msg .= t(' Download the files and place them in a folder named "AlchemyAPI" under the alchemy module directory.');
if ($analysis) {
$analysis['messages'][] = contentanalysis_format_message($msg, 'error');
}
else {
drupal_set_message($msg, 'error');
}
return FALSE;
}
require_once $file;
return TRUE;
}
function alchemy_new_alchemy_obj() {
static $alchemyObj;
if ($alchemyObj) {
return $alchemyObj;
}
alchemy_include_alchemy_class();
$alchemyObj = new AlchemyAPI();
$apikey = alchemy_get_apikey();
if (!$apikey) {
return FALSE;
}
$alchemyObj
->setAPIKey($apikey);
return $alchemyObj;
}
function alchemy_get_message_apimissing() {
$msg = t('You must set the Alchemy API key in order to use Alchemy. !settings_link.', array(
'!settings_link' => l(t('Set key here'), 'admin/settings/alchemy', array(
'attributes' => array(
'target',
'settings',
),
)),
));
return $msg;
}
function alchemy_get_apikey() {
$apikey = variable_get('alchemy_apikey', '');
if (!$apikey) {
$msg = alchemy_get_message_apimissing();
drupal_set_message($msg);
return false;
}
return $apikey;
}
function alchemy_xml2array($xmlstr) {
$xmlarr = new SimpleXMLElement($xmlstr);
return $xmlarr;
}
function alchemy_cache_get($cid, $command) {
$sql = '
SELECT *
FROM {alchemy_cache}
WHERE nid = %d AND command = "%s"
';
if (!($row = db_fetch_object(db_query($sql, $cid, $command)))) {
return FALSE;
}
return $row->data;
}
function alchemy_cache_set($cid, $command, $data) {
if (is_numeric($cid)) {
$sql = '
DELETE FROM {alchemy_cache}
WHERE nid = %d AND command = "%s"
';
db_query($sql, $cid, $command);
$sql = '
INSERT INTO {alchemy_cache}
(nid, path, created, command, data)
VALUES (%d, "%s", %d, "%s","%s")
';
db_query($sql, $cid, "", time(), $command, $data);
}
return TRUE;
}