public function cache_stats($id = null) { if (!$id) { global $post; if (!$post->ID) { return null; } $id = $post->ID; } $the_post = get_post($id); if (strlen($the_post->post_content) !== 0) { $no_shortcodes = strip_shortcodes($the_post->post_content); $charset = get_bloginfo('charset'); $all_text = bst_strip_html($no_shortcodes, $charset); if ($all_text) { $stats = bst_split_text($all_text); $total_alphanumeric = mb_strlen($stats['alphanumeric']); # mb_strlen = multibyte strlen $total_sentences = count($stats['sentences']); $total_words = count($stats['words']); $word_array = $stats['words']; $all_text = $stats['text']; if ($total_words > 0 && $total_sentences > 0) { # No divisions by zero, thanks. $chars_per_word = intval($total_alphanumeric / $total_words); $chars_per_sentence = intval($total_alphanumeric / $total_sentences); $words_per_sentence = intval($total_words / $total_sentences); $ARI = max(round(4.71 * ($total_alphanumeric / $total_words) + 0.5 * ($total_words / $total_sentences) - 21.43, 1), 0); $CLI = max(round(5.88 * ($total_alphanumeric / $total_words) - 29.6 * ($total_sentences / $total_words) - 15.8, 1), 0); $LIXlongwords = 0; for ($i = 0; $i < count($word_array); $i = $i + 1) { if (mb_strlen($word_array[$i]) > 6) { $LIXlongwords++; } } $temp = preg_split('/[,;\\.\\(\\:]/', $all_text); $LIX = max(round($total_words / count($temp) + $LIXlongwords * 100 / $total_words, 1), 0); } else { $ARI = $CLI = $LIX = '0'; } } else { $ARI = $CLI = $LIX = '0'; } # Remove ignored keywords $ignore = Word_Stats_Core::get_ignored_keywords(); $keywords = bst_regfilter_keyword_counts(bst_keywords($the_post->post_content, 3, get_bloginfo('charset')), $ignore); } else { $ARI = 0; $CLI = 0; $LIX = 0; $total_words = 0; $keywords = array(); } # Cache the stats update_post_meta($id, 'readability_ARI', $ARI); update_post_meta($id, 'readability_CLI', $CLI); update_post_meta($id, 'readability_LIX', $LIX); update_post_meta($id, 'word_stats_word_count', $total_words); update_post_meta($id, 'word_stats_keywords', serialize($keywords)); update_post_meta($id, 'word_stats_cached', true); }
function bst_keywords($text, $minimum = 0, $charset = 'UTF-8') { # No funky divisions if ($ratio < 0) { $ratio = 0; } if (!$text) { return false; } if (!$ignore) { $ignore = array(); } $text = bst_strip_html($text, $charset); $text = bst_strip_shortcodes($text); $word_hash = array(); $top_word_count = 0; $stats = bst_split_text($text); $word_array = $stats['words']; $total_words = count($word_array); # Count keywords foreach ($word_array as $word) { $word = trim(strtolower($word)); if (strlen($word) > 3) { if (!$word_hash[$word]) { $word_hash[$word] = 0; } $word_hash[$word]++; if ($word_hash[$word] > $top_word_count) { $top_word_count = $word_hash[$word]; } } } unset($word_array); # Not needed anymore. $filtered_result = array(); $purged_result = array(); # We want a ratio if ($ratio && $minimum) { # Filter foreach ($word_hash as $keyword => $appareances) { if (intval($appareances / ($total_words / $ratio)) >= $minimum) { $filtered_result[$keyword] = $appareances; } } # Only minimum } elseif (!$ratio && $minimum) { # Filter foreach ($word_hash as $keyword => $appareances) { if ($appareances >= $minimum) { $filtered_result[$keyword] = $appareances; } } } else { $filtered_result = $word_hash; } return $filtered_result; }