国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

PHP函數(shù)實(shí)現(xiàn)從一個(gè)文本字符串中提取關(guān)鍵字的方法

2024-05-04 23:37:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這篇文章主要介紹了PHP函數(shù)實(shí)現(xiàn)從一個(gè)文本字符串中提取關(guān)鍵字的方法,涉及php針對(duì)字符串的遍歷與查找等操作技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP函數(shù)實(shí)現(xiàn)從一個(gè)文本字符串中提取關(guān)鍵字的方法。分享給大家供大家參考。具體分析如下:

這是一個(gè)函數(shù)定位接收一個(gè)字符串作為參數(shù)(連同其他配置可選參數(shù)),并且定位該字符串中的所有關(guān)鍵字(出現(xiàn)最多的詞),返回一個(gè)數(shù)組或一個(gè)字符串由逗號(hào)分隔的關(guān)鍵字。功能正常工作,但我正在改進(jìn),因此,感興趣的朋友可以提出改進(jìn)意見(jiàn)。

 

 
  1. /** 
  2. * Finds all of the keywords (words that appear most) on param $str  
  3. * and return them in order of most occurrences to less occurrences. 
  4. * @param string $str The string to search for the keywords. 
  5. * @param int $minWordLen[optional] The minimun length (number of chars) of a word to be considered a keyword. 
  6. * @param int $minWordOccurrences[optional] The minimun number of times a word has to appear  
  7. * on param $str to be considered a keyword. 
  8. * @param boolean $asArray[optional] Specifies if the function returns a string with the  
  9. * keywords separated by a comma ($asArray = false) or a keywords array ($asArray = true). 
  10. * @return mixed A string with keywords separated with commas if param $asArray is true,  
  11. * an array with the keywords otherwise. 
  12. */ 
  13. function extract_keywords($str, $minWordLen = 3, $minWordOccurrences = 2, $asArray = false
  14. function keyword_count_sort($first, $sec) 
  15. return $sec[1] - $first[1]; 
  16. $str = preg_replace('/[^//w0-9 ]/'' ', $str); 
  17. $str = trim(preg_replace('//s+/'' ', $str)); 
  18. $words = explode(' ', $str); 
  19. $keywords = array(); 
  20. while(($c_word = array_shift($words)) !== null
  21. if(strlen($c_word) <= $minWordLen) continue
  22. $c_word = strtolower($c_word); 
  23. if(array_key_exists($c_word, $keywords)) $keywords[$c_word][1]++; 
  24. else $keywords[$c_word] = array($c_word, 1); 
  25. usort($keywords, 'keyword_count_sort'); 
  26. $final_keywords = array(); 
  27. foreach($keywords as $keyword_det) 
  28. if($keyword_det[1] < $minWordOccurrences) break
  29. array_push($final_keywords, $keyword_det[0]); 
  30. return $asArray ? $final_keywords : implode(', ', $final_keywords); 
  31. //How to use 
  32. //Basic lorem ipsum text to extract the keywords 
  33. $text = " 
  34. Lorem ipsum dolor sit amet, consectetur adipiscing elit.  
  35. Curabitur eget ipsum ut lorem laoreet porta a non libero.  
  36. Vivamus in tortor metus. Suspendisse potenti. Curabitur  
  37. metus nisi, adipiscing eget placerat suscipit, suscipit  
  38. vitae felis. Integer eu odio enim, sed dignissim lorem.  
  39. In fringilla molestie justo, vitae varius risus lacinia ac.  
  40. Nulla porttitor justo a lectus iaculis ut vestibulum magna  
  41. egestas. Ut sed purus et nibh cursus fringilla at id purus. 
  42. "; 
  43. //Echoes: lorem, suscipit, metus, fringilla, purus, justo, eget, vitae, ipsum, curabitur, adipiscing 
  44. echo extract_keywords($text); 

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 图们市| 车险| 汝州市| 土默特右旗| 融水| 诸城市| 什邡市| 周至县| 嘉黎县| 山丹县| 岗巴县| 酒泉市| 江门市| 平遥县| 山阴县| 惠水县| 新乡县| 兴和县| 林周县| 安新县| 秭归县| 邢台市| 莱州市| 禄丰县| 昌邑市| 洪江市| 射洪县| 资源县| 广河县| 皋兰县| 威远县| 锦屏县| 和顺县| 巧家县| 洪雅县| 东兴市| 比如县| 涿鹿县| 崇明县| 名山县| 昌图县|