PHP function to extract keywords from a string of text


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.