Home > Article > Backend Development > Teach you step by step how to do a keyword matching project (search engine) ---- Day 12, teach you how to do it on the twelfth day_PHP Tutorial
Day 12
Starting point:
1. Teach you step by step how to do keyword matching project (search engine) ---- Day 1
Review:
11. Teach you step by step how to do keyword matching project (search engine) ---- Day 11
As mentioned last time, the keyword application requirements are:
The product title and product attributes obtained through Taobao API are matched with keywords suitable for the product.
Consider the following factors initially:
Matching suitable for the group of people: Men’s clothing (matched keywords cannot contain women) Women’s clothing (matched keywords cannot contain men) Couples clothing (applicable to both men and women) Children’s clothing (?)
Baby attribute fields retrieved by Taobao API:
Xiao Shuai Shuai thought for a long time and finally came up with a solution. The solution is as follows:
<?<span>php </span><span>class</span><span> SelectorItem { </span><span>private</span> <span>$item</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$item</span><span>){ </span><span>$this</span>->item = <span>$item</span><span>; } </span><span>public</span> <span>function</span> __get(<span>$name</span><span>){ </span><span>if</span>(<span>isset</span>(<span>$this</span>->item-><span>$name</span><span>)){ </span><span>return</span> <span>$this</span>->item-><span>$name</span><span>; } </span><span>return</span> <span>null</span><span>; } </span><span>public</span> <span>static</span> <span>function</span> createFromApi(<span>$num_iid</span><span>){ </span><span>$client</span> = <span>new</span><span> TopClient(); </span><span>$client</span>->appkey = 'xx'<span>; </span><span>$client</span>->secretKey = 'xx'<span>; </span><span>$req</span> = <span>new</span><span> ItemGetRequest(); </span><span>$req</span>->setFields('props_name,property_alias,detail_url,cid,title'<span>); </span><span>$req</span>->setNumIid(<span>$num_iid</span><span>); </span><span>$resp</span> = <span>$client</span>->execute(<span>$req</span><span>); </span><span>if</span>(<span>isset</span>(<span>$resp</span>-><span>code)){ </span><span>#</span><span> error handle</span> <span>throw</span> <span>new</span> <span>Exception</span>(<span>$resp</span>->msg, <span>$resp</span>-><span>code); } </span><span>return</span> <span>new</span> self(<span>$resp</span>-><span>item); } } </span><span>$selectorItem</span> = SelectorItem::createFromApi(<span>$_REQUEST</span>["num_iid"<span>]); Logger</span>::trace(<span>$selectorItem</span>-><span>props_name); </span><span>$blackCharList</span> = <span>array</span><span>(); </span><span>$coreCharList</span> = <span>array</span><span>(); </span><span>$matchTitle</span> = <span>$selectorItem</span>->title.<span>$selectorItem</span>-><span>props_name; </span><span>if</span>(<span>preg_match</span>('/男装/', <span>$matchTitle</span><span>)){ </span><span>$coreCharList</span> = <span>array</span><span>( </span>"男装"<span> ); </span><span>$blackList</span> = <span>array</span><span>( </span>"女"<span> ); }</span><span>else</span> <span>if</span>(<span>preg_match</span>('/女装/', <span>$matchTitle</span><span>)){ </span><span>$coreCharList</span> = <span>array</span><span>( </span>"女装"<span> ); </span><span>$blackList</span> = <span>array</span><span>( </span>"男"<span> ); }</span><span>else</span> <span>if</span>(<span>preg_match</span>('/情侣装/', <span>$matchTitle</span><span>)){ </span><span>$coreCharList</span> = <span>array</span><span>( </span>"情侣装", "男装", "女装"<span> ); }</span><span>else</span> <span>if</span>(<span>preg_match</span>('/童装/',<span>$matchTitle</span><span>)){ </span><span>$coreCharList</span> = <span>array</span><span>( </span>"童装", "儿童装", "女童装", "男童装"<span> ); } </span><span>$where</span> = <span>array</span><span>(); </span><span>foreach</span>(<span>$coreCharList</span> <span>as</span> <span>$char</span><span>){ </span><span>$where</span>[] = " word LIKE '%<span>$char</span>%'"<span>; } </span><span>foreach</span>(<span>$blackCharList</span> <span>as</span> <span>$char</span><span>){ </span><span>$where</span>[] = " word NOT LIKE '%<span>$char</span>%'"<span>; } </span><span>if</span>(<span>count</span>(<span>$where</span>)>0<span>){ </span><span>$sql</span> = "SELECT * FROM keywords WHERE ".<span>implode</span>(' AND ',<span>$where</span><span>); Logger</span>::trace(<span>$sql</span><span>); </span><span>//</span><span>search database</span> }
When Xiao Shuaishuai happily gave the code to Boss Yu, Xiao Shuaishuai was criticized by Boss Yu. The reason is very simple:
1. Failure to consider future changes
2. Too many if
Xiao Shuai Shuai was not happy because he was approved, but he still had to ask Boss Yu for advice.
Boss Yu gave him a direction.
1. Learn design patterns to eliminate too many ifs and how to decouple them.
Design pattern (Design pattern) is a set of classified and cataloged summary of code design experience that is used repeatedly, known to most people. The purpose of using design patterns is to reuse code, make the code easier to understand by others, and ensure code reliability. There is no doubt that design patterns are win-win for oneself, others and the system; design patterns make coding truly engineering; design patterns are the cornerstone of software engineering, just like the structure of a building. [From Baidu Encyclopedia]
Xiao Shuai Shuai had no choice but to learn about design patterns.
Attachment:
<span>SelectorItem 里面的 __get 函数,称为Magic Methods<br />如:</span>
$selectorItem->title 其实会调用 __get('title')
<span><br /><br /></span>