PHP函数从文本字符串中提取关键字

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP函数从文本字符串中提取关键字相关的知识,希望对你有一定的参考价值。

This is a function that receives a string as an argument (along with other config optional arguments) and locates all keywords (words that appear most) from that string, returning the keywords as an array or as a string separated by commas. The function works correctly, but i'm working on improving it, so any sugestions please comment.
  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. {
  15. function keyword_count_sort($first, $sec)
  16. {
  17. return $sec[1] - $first[1];
  18. }
  19. $str = preg_replace('/[^p{L}0-9 ]/', ' ', $str);
  20. $str = trim(preg_replace('/s+/', ' ', $str));
  21.  
  22. $words = explode(' ', $str);
  23. $keywords = array();
  24. while(($c_word = array_shift($words)) !== null)
  25. {
  26. if(strlen($c_word) < $minWordLen) continue;
  27.  
  28. $c_word = strtolower($c_word);
  29. if(array_key_exists($c_word, $keywords)) $keywords[$c_word][1]++;
  30. else $keywords[$c_word] = array($c_word, 1);
  31. }
  32. usort($keywords, 'keyword_count_sort');
  33.  
  34. $final_keywords = array();
  35. foreach($keywords as $keyword_det)
  36. {
  37. if($keyword_det[1] < $minWordOccurrences) break;
  38. array_push($final_keywords, $keyword_det[0]);
  39. }
  40. return $asArray ? $final_keywords : implode(', ', $final_keywords);
  41. }
  42.  
  43. //How to use
  44.  
  45. //Basic lorem ipsum text to extract the keywords
  46. $text = "
  47. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  48. Curabitur eget ipsum ut lorem laoreet porta a non libero.
  49. Vivamus in tortor metus. Suspendisse potenti. Curabitur
  50. metus nisi, adipiscing eget placerat suscipit, suscipit
  51. vitae felis. Integer eu odio enim, sed dignissim lorem.
  52. In fringilla molestie justo, vitae varius risus lacinia ac.
  53. Nulla porttitor justo a lectus iaculis ut vestibulum magna
  54. egestas. Ut sed purus et nibh cursus fringilla at id purus.
  55. ";
  56. //Echoes: lorem, suscipit, metus, fringilla, purus, justo, eget, vitae, ipsum, curabitur, adipiscing
  57. echo extract_keywords($text);

以上是关于PHP函数从文本字符串中提取关键字的主要内容,如果未能解决你的问题,请参考以下文章

文本分类

文本分类

比较有用的php代码片段

使用从循环内的代码片段中提取的函数避免代码冗余/计算开销

从单个按钮从多个片段中提取数据

从 HTML 正文中提取文本片段(在 .NET 中)