You are here

function alchemy_contentanalysis_analyzer in Alchemy 7

Same name and namespace in other branches
  1. 6 modules/alchemy_contentanalysis/alchemy_contentanalysis.module \alchemy_contentanalysis_analyzer()

Implements hook_analyzer() via custom define callback().

Performs the analysis. callback is defined in hook_contentanalysis_analyzers ['callback']

Parameters

unknown_type $context: Array context format defined by contentanalysis.module

unknown_type $analysis: Array analysis format defined by contentanalysis.module

unknown_type $params: Array customer defined paramters

1 string reference to 'alchemy_contentanalysis_analyzer'
alchemy_contentanalysis_contentanalysis_analyzer_info in modules/alchemy_contentanalysis/alchemy_contentanalysis.module
Implentation of hook_contentanalysis_analyzers() register kwanalysis with contentanalysis analyzers registry

File

modules/alchemy_contentanalysis/alchemy_contentanalysis.module, line 116

Code

function alchemy_contentanalysis_analyzer($context, $analysis, $params) {
  if (!alchemy_include_alchemy_class()) {
    $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.');
    $analysis['messages'][] = contentanalysis_format_message($msg, 'error');
    return $analysis;
  }
  if (!alchemy_get_apikey()) {
    $analysis['messages'][] = contentanalysis_format_message(alchemy_get_message_apimissing(), 'error');
    return $analysis;
  }
  $text = $context['body_notags'];
  $nid = $context['nid'];
  $types = explode(',', $context['inputs']['analyzer_options']['alchemy']['types']);
  $usecache = (int) $context['inputs']['analyzer_options']['alchemy']['usecache'];
  $weight = -10;

  // keywords
  if (in_array('keywords', $types)) {
    $keywords = alchemy_get_elements($text, 'keywords', 'normalized', $nid, ALCHEMY_DEBUG || $usecache);
    $rows = array();
    $header = array(
      array(
        'data' => t('Term'),
      ),
      array(
        'data' => t('Relevance'),
      ),
    );
    if (is_array($keywords)) {
      foreach ($keywords as $v) {
        $rows[] = array(
          "<span class=\"kwresearch_keyword\">" . $v['term'] . "</span>",
          number_format($v['relevance'] * 100, 1) . "%",
        );
      }
    }
    if (!$rows) {
      $rows[] = array(
        array(
          'data' => t('No keywords available.'),
          'colspan' => count($header),
        ),
      );
    }
    $output = theme('table', array(
      'header' => $header,
      'rows' => $rows,
    ));
    $analysis['content'][] = contentanalysis_format_content(t('Keywords'), $weight++, TRUE);
    $analysis['content'][] = contentanalysis_format_content($output, $weight++);
  }

  // concepts
  if (in_array('concepts', $types)) {
    $keywords = alchemy_get_elements($text, 'concepts', 'normalized', $nid, ALCHEMY_DEBUG || $usecache);
    $rows = array();
    $header = array(
      array(
        'data' => t('Term'),
      ),
      array(
        'data' => t('Relevance'),
      ),
    );
    if (is_array($keywords)) {
      foreach ($keywords as $v) {
        $rows[] = array(
          "<span class=\"kwresearch_keyword\">" . $v['term'] . "</span>",
          number_format($v['relevance'] * 100, 1) . "%",
        );
      }
    }
    if (!$rows) {
      $rows[] = array(
        array(
          'data' => t('No concepts available.'),
          'colspan' => count($header),
        ),
      );
    }
    $output = theme('table', array(
      'header' => $header,
      'rows' => $rows,
    ));
    $analysis['content'][] = contentanalysis_format_content(t('Concepts'), $weight++, TRUE);
    $analysis['content'][] = contentanalysis_format_content($output, $weight++);
  }

  //entities
  if (in_array('entities', $types)) {
    $keywords = alchemy_get_elements($text, 'entities', 'normalized', $nid, ALCHEMY_DEBUG || $usecache);
    $rows = array();
    $header = array(
      array(
        'data' => t('Term'),
      ),
      array(
        'data' => t('Type'),
      ),
      array(
        'data' => t('Relevance'),
      ),
      array(
        'data' => t('Count'),
      ),
    );
    if (is_array($keywords)) {
      foreach ($keywords as $v) {
        $rows[] = array(
          "<span class=\"kwresearch_keyword\">" . $v['term'] . "</span>",
          $v['type'],
          number_format($v['relevance'] * 100, 1) . "%",
          $v['count'],
        );
      }
    }
    if (!$rows) {
      $rows[] = array(
        array(
          'data' => t('No entities available.'),
          'colspan' => count($header),
        ),
      );
    }
    $output = theme('table', array(
      'header' => $header,
      'rows' => $rows,
      'attributes' => array(
        'id' => 'kwresearch-site-keywords',
      ),
    ));
    $analysis['content'][] = contentanalysis_format_content(t('Entities'), $weight++, TRUE);
    $analysis['content'][] = contentanalysis_format_content($output, $weight++);
  }
  return $analysis;
}