字词出现频率统计

来源:互联网 发布:static修饰数组 编辑:程序博客网 时间:2024/03/28 19:05
//本文章编程语言为网络编程的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++;}?>
0 0
原创粉丝点击