You are here

function apachesolr_stats_report_frequent_keywords in Apache Solr Statistics 6.3

Same name and namespace in other branches
  1. 6 apachesolr_stats.module \apachesolr_stats_report_frequent_keywords()
  2. 7 apachesolr_stats.module \apachesolr_stats_report_frequent_keywords()

Recieves an array of keyword => count and reports the top-used terms.

Parameters

string $page_id: ID of the search page.

array $keywords: array of keyword => count pairs.

array $keywords_noresults: array containing keywords that returned 0 results.

string $class: CSS class for each link.

int $number: Number of terms to show.

Return value

string

1 call to apachesolr_stats_report_frequent_keywords()
apachesolr_stats_generate_report_elements in ./apachesolr_stats.module
Generates report elements for the given granularity.

File

./apachesolr_stats.module, line 826
Keeps and reports statistics about Apache Solr usage and performance.

Code

function apachesolr_stats_report_frequent_keywords($page_id, $keywords, $keywords_noresults, $class = '', $number = 25) {
  $search_page = apachesolr_search_page_load($page_id);
  $search_path = $search_page['search_path'];
  if (empty($keywords)) {
    return '';
  }
  arsort($keywords);

  // Final elements are the most frequent, get $number elements off the array
  $slice = array_slice($keywords, 0, $number);

  // Calculate font size for display
  $min = 1000000000.0;
  $max = -1000000000.0;
  $steps = 6;
  $weighted_slice = array();
  foreach ($slice as $word => $count) {
    $min = min($min, $count);
    $max = max($max, $count);
    $weighted_slice[$word] = array(
      'count' => $count,
      'log_count' => log($count),
    );
  }

  // Note: we need to ensure the range is slightly too large to make sure even
  // the largest element is rounded down.
  $range = max(0.01, $max - $min) * 1.0001;

  // Add "weight"
  foreach ($weighted_slice as $word => $data) {
    $weighted_slice[$word]['weight'] = floor($steps * ($data['count'] - $min) / $range);
  }
  $items = array();
  foreach ($weighted_slice as $word => $data) {
    if (isset($keywords_noresults[$word])) {
      $class = "error";
    }
    $font_size_pct = 80 + $data['weight'] * 12;
    $items[] = l($word, $search_path . '/' . $word, array(
      'attributes' => array(
        'class' => $class,
        'style' => 'font-size:' . sprintf("%d%%", $font_size_pct),
      ),
      'absolute' => TRUE,
    )) . " (" . $data["count"] . ")";
  }
  return implode(", ", $items);
}