Home >Backend Development >PHP Tutorial >Teach you step by step how to do a keyword matching project (search engine) ---- Day 17, teach you how to do it on Day 17_PHP Tutorial
Day 17
Guest appearance: Diaosi’s deceptive form artifact
Starting point: Teach you step by step how to do keyword matching project (search engine) ---- Day 1
Review: Teach you step by step how to do keyword matching project (search engine) ---- Day 16
When Xiao Shuaishuai went to implement the blacklist entry task, he explained it to Xiao Dingding for a long time. Xiao Dingding held his chin and looked at Xiao Shuaishuai with an expression of admiration.
Xiao Shuai Shuai felt shocked and ran to Boss Yu to complain: Boss Yu, I talked to Xiao Ding Ding for a long time about how to install the database client, how to fill in the blacklist, and why it was necessary to fill it in. I made it clear to Xiao Ding Ding. I still don’t understand, I really can’t stand it anymore.
Boss Yu comforted Xiao Shuaishuai and said: Let’s not rush to implement this task. Let’s make the preliminary functions first, set up the entire shelf, and input a baby to match the keywords. In the early stage, There are definitely many incorrect keywords, but it doesn’t matter, we will teach you again.
Xiao Shuai Shuai:......
Boss Yu then said: As long as they see the results, it should be easy to understand. Then they will only admire you. Okay, Xiao Shuai Shuai, be happy. It's normal to hit a wall once, as long as you learn a lesson from it.
Although Xiao Shuai Shuai is still a little dissatisfied, he is not as angry as he was at the beginning. Xiao Shuaishuai replied: Okay, let me continue to work on the function first. Let me try the function of synonyms first.
Synonym examples:
1. XXL,加大,加大码<br /> 2. 外套,衣,衣服,外衣,上衣<br /> 3. 女款,女士,女生,女性<br /><br /><br />
Considering that a certain category has different definitions for synonyms of some words, Xiaoshuai designed the table structure like this:
<span>CREATE</span> <span>TABLE</span><span> `category_linklist` ( `cid` </span><span>BIGINT</span>(<span>20</span>) <span>DEFAULT</span> <span>NULL</span> COMMENT <span>'</span><span>类目ID</span><span>'</span><span>, `catmatch` </span><span>VARCHAR</span>(<span>50</span>) <span>CHARACTER</span> <span>SET</span> utf8 <span>DEFAULT</span> <span>NULL</span> COMMENT <span>'</span><span>类目名称</span><span>'</span><span>, `word` </span><span>VARCHAR</span>(<span>255</span>) <span>CHARACTER</span> <span>SET</span> utf8 <span>DEFAULT</span> <span>NULL</span> COMMENT <span>'</span><span>关键词,用逗号分隔</span><span>'</span><span>, `created` </span><span>DATETIME</span> <span>DEFAULT</span> <span>NULL</span> COMMENT <span>'</span><span>录入时间</span><span>'</span><span>, ) ENGINE</span><span>=</span>INNODB <span>DEFAULT</span> CHARSET<span>=</span>utf8 COLLATE<span>=</span>utf8_bi
Xiao Shuai Shuai also added some data.
<span>INSERT</span> <span>INTO</span> category_linklist(cid,catmatch,word) <span>VALUES</span>("<span>50010850</span><span>","女装","XXL,加大,加大码"); </span><span>INSERT</span> <span>INTO</span> category_linklist(cid,catmatch,word) <span>VALUES</span>("<span>50010850</span><span>","女装","外套,衣,衣服,外衣,上衣"); </span><span>INSERT</span> <span>INTO</span> category_linklist(cid,catmatch,word) <span>VALUES</span>("<span>50010850</span><span>","女装","女款,女士,女生,女性"); #......</span>
Xiao Shuai Shuai extended a CharListHandle class to specifically handle these synonyms.
The code is as follows:
<?<span>php </span><span>class</span> LinklistCharListHandle <span>extends</span><span> CharListHandle { </span><span>public</span> <span>function</span> <span>exec</span><span>(){ </span><span>$sql</span> = "select word from category_linklist where cid='<span>$this</span>->selectorItem->cid'"<span>; </span><span>$linklist</span> = DB::makeArray(<span>$sql</span><span>); </span><span>foreach</span>(<span>$linklist</span> <span>as</span> <span>$strWords</span><span>){ </span><span>$words</span> = <span>explode</span>(",",<span>$strWords</span><span>); </span><span>$properties</span> = <span>$this</span>->selectorItem-><span>getProperties(); </span><span>foreach</span>(<span>$properties</span> <span>as</span> <span>$property</span><span>){ </span><span>$this</span>->charlist->addCore(<span>$property</span>-><span>value); </span><span>if</span>(<span>in_array</span>(<span>$property</span>->value,<span>$words</span><span>)){ </span><span>foreach</span>(<span>$words</span> <span>as</span> <span>$char</span><span>){ </span><span>$this</span>->charlist->addCore(<span>$char</span><span>); } } } } } }</span>
Xiao Shuai Shuai has added an interface for SelectorItem to obtain attribute arrays:
<span>class</span><span> SelectorItem { </span><span>#</span><span>......</span> <span>public</span> <span>function</span><span> getProperties(){ </span><span>$result</span> = <span>array</span><span>(); </span><span>$properties</span> = <span>explode</span>(";",<span>$this</span>->item-><span>props_name); </span><span>foreach</span>(<span>$properties</span> <span>as</span> <span>$strProperty</span><span>){ </span><span>$result</span>[] = self::createItemProperty(<span>explode</span>(":",<span>$strProperty</span><span>)); } </span><span>return</span> <span>$result</span><span>; } </span><span>public</span> <span>static</span> <span>function</span> createItemProperty(<span>$propertyArr</span><span>){ </span><span>$property</span> = <span>new</span><span> stdClass(); </span><span>$property</span>->id = <span>$propertyArr</span>[0<span>]; </span><span>$property</span>->fieldId = <span>$propertyArr</span>[1<span>]; </span><span>$property</span>->name = <span>$propertyArr</span>[2<span>]; </span><span>$property</span>->value = <span>$propertyArr</span>[3<span>]; </span><span>return</span> <span>$property</span><span>; } </span><span>#</span><span>......</span> }
The easy changes to Selector are as follows:
<span>class</span><span> Selector { </span><span>private</span> <span>static</span> <span>$charListHandle</span> = <span>array</span><span>( </span>"黑名单" => "BacklistCharListHandle", "近义词" => "LinklistCharListHandle"<span> ); </span><span>#</span><span>......</span> }
When Xiao Shuaishuai showed this code to Boss Yu, Boss Yu just took a look at it and approved Xiao Shuaishuai.
Xiao Shuai Shuai is puzzled.
Foretelling why Xiao Shuai Shuai was criticized, please read the breakdown next time.