PHP functions to implement methods for extracting keywords from a text string _php tutorial

Source: Internet
Author: User

PHP function to implement a method of extracting keywords from a text string


This article describes a PHP function that implements the method of extracting keywords from a text string. Share to everyone for your reference. The specific analysis is as follows:

This is a function that locates a string as a parameter (along with other configuration optional parameters), and locates all the keywords in the string (most occurrences of the word), returning an array or a comma-delimited keyword for a string. The function works fine, 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

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

/**

* Finds All of the keywords (words this appear most) on Param $str

* and return them in order of the 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 be considered a keyword.

* @param int $minWordOccurrences [OPTIONAL] The minimun number of times a word have to appear

* on Param $STR To is considered 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 is true,

* An array with the keywords otherwise.

*/

function Extract_keywords ($str, $minWordLen = 3, $minWordOccurrences = 2, $asArray = False)

{

function Keyword_count_sort ($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 adipiscing 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

Vitae Felis. The Integer EU odio enim, sed dignissim lorem.

In Fringilla molestie justo, vitae varius risus lacinia AC.

Nulla porttitor Justo a lectus iaculis ut vestibulum magna

Egestas. 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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1025320.html www.bkjia.com true http://www.bkjia.com/PHPjc/1025320.html techarticle PHP Function Implementation method of extracting keywords from a text string This article describes the PHP function implementation method to extract the keywords from a text string. Share for everyone to join us ...

  • 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.