PHP "related article recommendation" Feature easy implementation _php tutorial

Source: Internet
Author: User
Generally do content site, need to appear in each article with the article related to the article list. Most people use a method like this: Set up a list of keywords, judge each article contains the keywords, and finally find out the most relevant articles according to the keywords. For sites with more complex content, it is obviously troublesome to identify key list words.

Later I looked up some PHP functions, feel Similar_text (PHP4,PHP5) function can be very convenient to meet my requirements. This idea is: Remove all the article title from the article list, the title of all the articles are compared with the current title, the results of the comparison to generate an array, according to the size of the similarity from the big to the title, using Similar_text to compare the title of these articles with the original article title, by the title of the similarity to rearrange the title , you get a list of articles similar to the original article.

The key functions used in this idea are:

int Similar_text (string $first, String $second [, float $percent])

It returns the same number of bytes in the two-word root string.

In this way, we set up the following function, which functions to rearrange arrays in the same order as the $arr_title array in $title.

 
  "; for ($j = 0; $j <=2; $j + +) {echo ($j + 1). ":". $new _array[$j]. "
";} $title the current title, $arrayTitle the array function getsimilar ($title, $arr _title) you want to find {$arr _len = count ($arr _title); for ($i =0; $i <= ($arr _len-1); $i + +) {//Get two strings of similar bytes $arr_similar[$i] = Similar_text ($arr _title[$i], $title);} Arsort ($arr _similar);//Sort reset by a similar number of bytes from high to low ($arr _similar);//Move the pointer to the first cell of the array $index = 0;foreach ($arr _similar as $old _ Index=> $similar) {$new _title_array[$index] = $arr _title[$old _index]; $index + +;} return $new _title_array;}? >

Program Run Result:

Top three articles that are most relevant to the [Help house] are: 1: Simple and clear modern Magic 2: Simple, Modern Magic 3: Concise Ancient Magic

Here are some things to keep in mind:

    1. About Similar_text speed, someone has done this one test, the result is:
    2. The speed issues-Similar_text seem to is only a issue for long sections of text (>20000 chars).

      I found a huge performance improvement in my application by just testing if the string to be tested is less than 20000 ch Ars before calling Similar_text.

      20000+ took 3-5 secs to process, anything else (10000 and below) took a fraction of a second. Fortunately for me, there is only a handful of instances with >20000 chars which I couldn ' t get a comparison percent for.

      If you want to use the body directly, it may be slower than the speed.

    3. This function may not be good for English (I have not tried it). In English, you can separate English sentences into multiple words with spaces and then write a function similar to Similar_text.
    4. When the sentence contains more "," and other non-keyword characters, the resulting results may not be ideal.

http://www.bkjia.com/PHPjc/752454.html www.bkjia.com true http://www.bkjia.com/PHPjc/752454.html techarticle generally do content site, need to appear in each article with the article related to the article list. Most people use the method is: Set up a keyword list, judging each article contains ...

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