You are here

function alchemy_get_elements in Alchemy 6

Same name and namespace in other branches
  1. 7 alchemy.module \alchemy_get_elements()
3 calls to alchemy_get_elements()
alchemy_autotagging_autotagging_api_insert in modules/alchemy_autotagging/alchemy_autotagging.module
implementation of hook_autotagging_api_insert
alchemy_contentanalysis_analyzer in modules/alchemy_contentanalysis/alchemy_contentanalysis.module
Implementation of hook_analyzer() via custom define callback
alchemy_get_elements_from_node in ./alchemy.module

File

./alchemy.module, line 80
DO NOT COMMIT TO DRUPAL CONTRIB! THIS MODULE IS FOR DEVELOPMENT ONLY.

Code

function alchemy_get_elements($text, $type = 'keywords', $output = 'normalized', $cid = 0, $use_cache = FALSE) {

  //dsm("alchemy_get_elements($text, $type, $output, $cid, $use_cache)");
  if ($use_cache && $cid) {
    $resultstr = alchemy_cache_get($cid, $type);

    //watchdog('alchemy', 'fectch from cache: ' . $resultstr);
  }
  if (!$resultstr) {
    $alchemyObj = alchemy_new_alchemy_obj();

    // Extract topic keywords from a text string.
    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':

      // TODO

      //print_r($result);
      $elms = array();
      break;
    case 'concepts':
      $elms = $result->concepts->concept;
      break;
    case 'keywords':
      $elms = $result->keywords->keyword;
      break;
  }

  //print_r($elms);
  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;
}