现在的位置: 首页 > 综合 > 正文

PHP:计算文件或数组中单词出现频率

2014年04月02日 ⁄ 综合 ⁄ 共 675字 ⁄ 字号 评论关闭

如果是小文件,可以一次性读入到数组中,使用方便的数组计数函数进行词频统计(假设文件中内容都是空格隔开的单词):

<?php
$str = file_get_contents("/path/to/file.txt"); //get string from file
preg_match_all("/\b(\w+[-]\w+)|(\w+)\b/",$str,$r); //place words into array $r - this includes hyphenated words
$words = array_count_values(array_map("strtolower",$r[0])); //create new array - with case-insensitive count
arsort($words); //order from high to low 
print_r($words)

如果是大文件,读入内存就不合适了,可以采用如下方法:

<?php
$filename = "/path/to/file.txt";
$handle = fopen($filename,"r");
if ($handle === false) {
  exit;
  }
$word = "";
while (false !== ($letter = fgetc($handle))) {
  if ($letter == ' ') {
    $results[$word]++;
    $word = "";
    }
  else {
    $word .= $letter;
    }
}
fclose($handle);
print_r($results);

对于大文件,第二种方法比较快比较安全,不会引起内存异常。

by iefreer

抱歉!评论已关闭.