This article mainly introduces how PHP simply implements the "recommended related articles" function. The method is simple and practical. If you need it, refer to the following.
This article mainly introduces how PHP simply implements the "recommended related articles" function. The method is simple and practical. If you need it, refer to the following.
Generally, when you create a content website, you need to include a list of articles related to this Article in each article. For most people, the method is usually to create a keyword list, determine which keywords are contained in each article, and finally find the most relevant article based on the keywords. For websites with complex content, identifying key list words is obviously troublesome.
This article describes the similar_text (php4, php5) functions that are different from the previous methods to meet our expectation. The specific idea is: retrieve all the article titles from the article list, compare all the article titles with the current title, and generate an array of the comparison results ,, the article titles are compared with the original article titles by similar_text based on the similarity, and the titles are re-arranged according to the similarity of the titles, A list of similar articles is obtained.
The key functions used in this approach are:
Int similar_text (string $ first, string $ second [, float $ percent])
It returns the same number of bytes of the two root strings.
According to this idea, we create the following function. The function is to rearrange the $ arr_title array in a sequence similar to $ title.
<? Php $ demo_title = ""; $ demo_arr_title = array (" ", "modern magic is not simple", "modern magic is difficult to understand"); $ new_array = getSimilar ($ demo_title, $ demo_arr_title); // print_r ($ new_array ); the first three articles most relevant to echo "and [$ demo_title] are:
"; For ($ j = 0; $ j <= 2; $ j ++) {echo ($ j + 1 ). ":". $ new_array [$ j]."
";}// $ Title: Current title. $ arrayTitle is the array functiongetSimilar ($ title, $ arr_title) to be searched {$ arr_len = count ($ arr_title ); for ($ I = 0; $ I <= ($ arr_len-1); $ I ++) {// obtain the number of bytes similar to two strings $ arr_similar [$ I] = similar_text ($ arr_title [$ I], $ title);} arsort ($ arr_similar ); // sort the reset ($ arr_similar) by the number of similar bytes; // move the pointer to the first unit of the array $ index = 0; foreach ($ arr_similaras $ old_index => $ similar) {$ new_title_array [$ index] = $ arr_title [$ old_index]; $ Index ++;} return $ new_title_array;}?>
Program running result:
The first three articles most relevant to [concise modern magic] are: 1: simple modern magic 2: easy to understand modern magic 3: Concise ancient magic
Note the following:
Someone has done this test on similar_text speed. The result is:
The speed issues for similar_text seem to be only an issue for long sections of text (& gt; 20000 chars ).
I found a huge performance improvement in my application by just testing if the string to be tested was less than 20000 chars 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 was only a handful of instances with> 20000 chars which I couldn't get a comparison %.
It may be slow to directly use the text for comparison.
It is said that this function is not very effective in English (interested readers can try it on their own ). When used in English, you can split an English sentence into multiple words with spaces and then write a function similar to similar_text.
In addition, if the sentence contains many non-Keyword characters, such as "," and ", the result may be unsatisfactory.