Heim > Artikel > Backend-Entwicklung > 手把手教你做关键词匹配项目(搜索引擎)---- 第十七天,教你做第十七天_PHP教程
第十七天
客串:屌丝的坑人表单神器
起点:手把手教你做关键词匹配项目(搜索引擎)---- 第一天
回顾:手把手教你做关键词匹配项目(搜索引擎)---- 第十六天
小帅帅去落实黑名单录入的任务的时候,他给小丁丁讲解了半天,小丁丁就托着下巴,看着小帅帅,做着崇拜的表情。
小帅帅感觉受打击了,跑到于老大那儿抱怨:于老大,我给小丁丁讲了半天,怎么装数据库客户端,怎么填黑名单,为什么要填都说明白了,小丁丁还是不懂,我真心受不了了。
于老大就安慰小帅帅说:我们先不急着落实这项任务把,我们先把初步的功能做出来,把整个架子搭好,输入个宝贝就能匹配出关键词,前期的话关键词肯定很多不正确,但是不要紧,我们再来教。
小帅帅:......
于老大接着说到:只要他们看到了结果,应该很容易懂的,到时他们只有崇拜你的份啦。好了,小帅帅开心点,一次的碰壁很正常,只要在这里面吸取到教训就好了。
小帅帅,心里虽然还有点不如意,不过没有刚开始那么气愤了。小帅帅答到:好吧,我先继续做功能先吧,我先去尝试做近义词的功能吧。
近义词范例:
1. XXL,加大,加大码<br /> 2. 外套,衣,衣服,外衣,上衣<br /> 3. 女款,女士,女生,女性<br /><br /><br />
考虑到某一个类目对于一些词的近义词有不同的定义,故而小帅帅把表结构设计成这样:
<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
小帅帅并把数据补了一些进去。
<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>
小帅帅扩展了一个CharListHandle类来专门处理这些近义词.
代码如下:
<?<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>
小帅帅又增加了SelectorItem获取属性数组的接口:
<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> }
Selector的轻松改动如下:
<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> }
当小帅帅把这代码拿给于老大看的时候,于老大只是瞧了一下,就把小帅帅给批了。
小帅帅百思不得其解。
预知小帅帅为啥被批,请看下回分解。