A method of extracting keywords from a text string by the PHP function implementation

Source: Internet
Author: User
Tags php programming

This example describes the method by which the PHP function implementation extracts keywords from a text string. Share to everyone for your reference. The specific analysis is as follows:

This is a function positioned to receive a string as an argument (along with other configuration optional parameters), and to locate all the keywords in the string (the words that appear the most), and return an array or a comma-delimited keyword. Functions are working properly, but I am improving, so interested friends can make suggestions for improvement.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 /** * finds all of the keywords (words, appear most) on Param $str * and return them in order of most occurrences to Less occurrences. * @param string $str The string to search for the keywords. * @param int $minWordLen [OPTIONAL] The minimun length (number of chars) of a word to is considered a keyword. * @param int $minWordOccurrences [OPTIONAL] The minimun number of times a word has to appear * in Param $str to be consider Ed a keyword. * @param boolean $asArray [optional] Specifies if the function returns a string with the * keywords separated by a comma ($ Asarray = false) or a keywords array ($asArray = True). * @return mixed A string with keywords separated with commas if Param $asArray are true, * An array with the keywords Wise. */function Extract_keywords ($str, $minWordLen = 3, $minWordOccurrences = 2, $asArray = False) {function Keyword_count_so RT ($first, $sec) {return $sec [1]-$first [1];} $str = Preg_replace ('/[^w0-9]/', ', ', $str); $str = Trim (preg_replace ('/s+/'), ", $str)); $words = Explode (", $str); $keywords = Array (); while (($c _word = Array_shift ($words))!== null) {if (strlen ($c _word) <= $minWordLen) continue; $c _word = Strtolower ($c _ Word); if (array_key_exists ($c _word, $keywords)) $keywords [$c _word][1]++; else $keywords [$c _word] = Array ($c _word, 1); } usort ($keywords, ' keyword_count_sort '); $final _keywords = Array (); foreach ($keywords as $keyword _det) {if ($keyword _det[1] < $minWordOccurrences) break; Array_push ($final _keywords, $ Keyword_det[0]); Return $asArray? $final _keywords:implode (', ', $final _keywords); //how to use//basic Lorem ipsum text to extract the keywords $text = "Lorem ipsum dolor sit amet, Consectetur Adipisci Ng Elit. Curabitur eget ipsum ut lorem laoreet porta a non libero. Vivamus in Tortor metus. Suspendisse Potenti. Curabitur metus nisi, adipiscing eget placerat, Suscipit suscipit. Integer eu odio enim, sed dignissim lorem. In Fringilla molestie justo, vitae varius risus AC. NullA porttitor justo a lectus iaculis ut vestibulum magna. Ut sed purus et nibh cursus fringilla at ID purus. "; Echoes:lorem, Suscipit, Metus, Fringilla, Purus, Justo, Eget, Vitae, Ipsum, Curabitur, adipiscing echo extract_keywords ($text);

I hope this article will help you with your PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.