Teach you how to create a keyword matching project (search engine) ---- 19th days

Source: Internet
Author: User

Guest string: hacker form artifact

Those things we 've gone through: Database things

Discussion: Explore Server Load balancer

Object-Oriented Knowledge: An Object-Oriented understanding-a new understanding, an object-oriented understanding-a sleepwalking of thoughts (1)

 

Start Point: Teach you how to create a keyword matching project (search engine) ---- Day 1

Review: Experts teach you how to perform keyword matching projects (search engines)-18th days

 

19th days

In the past, we have always mentioned the construction of dictionaries and the special processing of baby attributes, which has been raised too much. As a result, the entire system cannot run completely yet.

Handsome boy needs to spend more time on this.

Even if we use the like attribute in the SQL statement we mentioned last time, the not like attribute in the blacklist cannot solve all the problems. We just narrow down the keyword that needs to be matched.

Let's take an example to illustrate:

There is a baby is a chiffon dress, so chiffon dress a, trendy chiffon dress a, Korean chiffon dress a, high imitation chiffon dress, genuine women's dress and so on these words can be used?

Xiao Shuai, Yu laifeng, Xiao Lele, Xiao Huanhuan, and Xiao Hun agreed that each keyword needs to be matched with the baby attribute (dictionary). When the matching degree is at a certain value, for example, if it is greater than or equal to 80%, it is available.

He wondered how to match each keyword with the baby attribute (dictionary?

At that time, the boss mentioned the keyword splitting algorithm, which is actually very simple and the principle is as follows:

Split keywords into phrases or words based on business words, and calculate the proportion of each word or phrase in the entire keyword. For example, chiffon dress a is split into chiffon, dress, and, the ratio is 33%, 50%, and 17%.

Then compare the split phrase or word with the baby attribute (dictionary) to determine whether the matching is available. For example, if the baby is a chiffon dress, the matching degree of A is 33% + 50% = 83%.

 

As soon as the algorithm is explained, Shuai can't wait to write the code. The Code is as follows:

<? Phpclass splitter {/*** obtains the phrase data of word segmentation under a category, sorted by String Length comparison * @ Param $ CID * @ return array */public static function getwordsegmentation ($ CID) {$ ret = array (); $ SQL = "select word from category_linklist where cid = '$ CID'"; $ words = DB: makearray ($ SQL ); foreach ($ words as $ strwords) {$ words = explode (",", $ strwords); foreach ($ words as $ word) {If (SELF :: isphrase ($ word) {$ RET [] = $ word ;}} usort ($ ret, function ($, $ B) {(strlen ($ A)> strlen ($ B ))? 1:-1;}); return $ ret;}/*** check whether the phrase * @ Param $ word * @ return bool */public static function isphrase ($ word) {If (preg_match ('/^ [\ x {4e00}-\ x {9fa5}] + $/U', $ word) & strlen ($ word) <= 3) {return false;} If (strlen ($ word) <= 1) {return false;} return true ;} /*** split keywords into phrases or words * @ Param $ keyword * @ Param $ CID * @ return array */public static function split ($ keyword, $ CID) {$ splitwords = array (); $ splitsegmentation = self: getwordsegmentation ($ CID); $ remainkeyword = $ keyword; foreach ($ splitsegmentation as $ phrase) {If (count (explode ($ phrase, $ remainkeyword)> 1) {$ splitwords [] = $ phrase; $ remainkeyword = str_replace ($ phrase ,"::", $ remainkeyword) ;}}$ remainkeywords = explode (":", $ remainkeyword); $ splitwords = array_merge ($ splitwords, $ remainkeywords); $ keywordscores = array (); foreach ($ splitwords as $ splitword) {$ keywordscore = new keywordscore (); $ keywordscore-> splitword = $ splitword; $ keywordscore-> Weight = self: calculateweight ($ splitword, $ keyword); $ keywordscores [] = $ keywordscore;} return $ keywordscores ;} /*** @ DESC calculate utf8 string weight * @ Param string $ splitword * @ Param string $ word * @ return float */public static function calculateweight ($ splitword, $ word) {return round (strlen ($ splitword)/strlen ($ word), 3) ;}} class keywordscore {public $ splitword; Public $ weight ;}

Shuai writes this is also the result of many reconstruction at. He does not want to disappoint the boss, so he keeps improving.

When Shuai showed the code to the boss, what was the boss's reaction? Please refer to the next decomposition.

Teach you how to create a keyword matching project (search engine) ---- 19th days

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.