相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
PHP編程計算文件或數(shù)組中單詞出現(xiàn)頻率的方法
本文實例講述了PHP編程計算文件或數(shù)組中單詞出現(xiàn)頻率的方法。分享給大家供大家參考,具體如下:
如果是小文件,可以一次性讀入到數(shù)組中,使用方便的數(shù)組計數(shù)函數(shù)進(jìn)行詞頻統(tǒng)計(假設(shè)文件中內(nèi)容都是空格隔開的單詞):
<?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)