當前位置:編程學習大全網 - 網站源碼 - 編寫壹個文本分析程序.要求:可以統計字詞的頻率

編寫壹個文本分析程序.要求:可以統計字詞的頻率

統計字詞的頻率文本分析程序如下

//編程語言為網絡編程的php腳本語言

header("content-type:text/html; charset=utf-8");

$filePath="txt.txt";

$chars=2;

$wordArray =array();

$file=fopen($filePath,"r");

while(!FEOF($file)){

//讀出壹行

$singleLine=trim(fgets($file));

//數字、英文、標點、空格過濾

$singleLine=preg_replace("/[0-9]{1}/", "", $singleLine);

$singleLine=preg_replace("/[a-zA-Z]{1}/", "", $singleLine);

$singleLine=preg_replace("/[ '.,:;*?~`!@#$%^&+=\-)(<>{}]|\]|\[|\/|\\\|\"|\|/", "", $singleLine);

$singleLine=str_replace(" ", "", $singleLine);

//只處理字數多於2的行

if (strlen($singleLine)>2){

for($i=0;$i<strlen($singleLine)-$chars*3;$i=$i+3){//壹個漢字在utf-8下算三個字符

$word=substr($singleLine,$i,$chars*3);

$wordArray[]=$word;

//echo $word;

}

}

}

//關閉文件

fclose($file);

//對頻數進行統計

$wordArrayOut=array_count_values($wordArray);

//根據統計次數降序排列

arsort($wordArrayOut);

//輸出結果

$i=1;

foreach($wordArrayOut as $key=>$value){

$rankNo=$i<10?"0".$i:$i;

echo "$key $value<br />";

$i++;

}

  • 上一篇:如何定位winCE下的異常“數據中止”(當錯誤未打印時,PC指針偏移)
  • 下一篇:免費源代碼棋牌
  • copyright 2024編程學習大全網