Home >php教程 >php手册 >手把手教你做关键词匹配项目(搜索引擎)---- 第十六天,教你做----

手把手教你做关键词匹配项目(搜索引擎)---- 第十六天,教你做----

WBOY
WBOYOriginal
2016-06-13 09:26:241153browse

手把手教你做关键词匹配项目(搜索引擎)---- 第十六天,教你做----

第十六天

友情客串:屌丝的坑人表单神器

起点:手把手教你做关键词匹配项目(搜索引擎)---- 第一天

回顾:手把手教你做关键词匹配项目(搜索引擎)---- 第十五天

14天pdf版:http://files.cnblogs.com/oshine/%E6%89%8B%E6%8A%8A%E6%89%8B%E6%95%99%E4%BD%A0%E5%81%9A%E5%85%B3%E9%94%AE%E8%AF%8D%E5%8C%B9%E9%85%8D%E9%A1%B9%E7%9B%AE%2814%E5%A4%A9%29.pdf

15天内的源码:
http://files.cnblogs.com/oshine/myproject.rar

从上次小帅帅跟于老大讨论完毕后,有个很郁闷的事情还没定下来,就是我们已有对应关系、反义词以及近义词这些数据它的数据格式是怎样的?

小帅帅要弄懂这些才能继续进行下去,于是他又去请教于老大。

于老大就对小帅帅说,我们先不考虑这么多因素,我们先从实际出发,我们先做反义词这块,就先从女装开始,女装是个类目吧,我们就按类目开始做。

那么表结构的字段就有类目、反义词。

小帅帅把表创建了出来:

<span>CREATE</span> <span>TABLE</span><span> `category_backlist` (
   `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>18</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>,
   </span><span>UNIQUE</span> <span>KEY</span><span> `cid` (`cid`,`keyword`)
 ) ENGINE</span><span>=</span>INNODB <span>DEFAULT</span> CHARSET<span>=</span>utf8 COLLATE<span>=</span>utf8_bin

小帅帅并把数据补齐了。

<span>INSERT</span> <span>INTO</span> category_backlist(cid,catmatch,word)<span>VALUES</span>("<span>50010850</span><span>","女装","男");
</span><span>INSERT</span> <span>INTO</span> category_backlist(cid,catmatch,word)<span>VALUES</span>("<span>50010850</span><span>","女装","童");
</span><span>INSERT</span> <span>INTO</span> category_backlist(cid,catmatch,word)<span>VALUES</span>("<span>50010850</span><span>","女装","宝宝");
#......</span>

小帅帅很轻松的把代码也改正了一下。

<?<span>php
</span><span>abstract</span> <span>class</span><span> CharListHandle {

    </span><span>protected</span> <span>$charlist</span><span>;
    </span><span>protected</span> <span>$selectorItem</span><span>;
    </span><span>public</span> <span>function</span> __construct(<span>$charlist</span>,<span>$selectorItem</span><span>){
        </span><span>$this</span>->charlist = <span>$charlist</span><span>;
        </span><span>$this</span>->selectorItem = <span>$selectorItem</span><span>;
    }

    </span><span>abstract</span> <span>function</span> <span>exec</span><span>();
}

</span><span>class</span> BacklistCharListHandle <span>extends</span><span> CharListHandle {
    </span><span>public</span> <span>function</span> <span>exec</span><span>(){
        </span><span>$sql</span> = "select word from category_backlist where cid='<span>$this</span>->selectorItem->cid'"<span>;
        </span><span>$backlist</span> = DB::makeArray(<span>$sql</span><span>);
        </span><span>foreach</span>(<span>$backlist</span> <span>as</span> <span>$char</span><span>){
            </span><span>$this</span>->charlist->addBlacklist(<span>$char</span><span>);
        }

    }

}

</span><span>class</span><span> Selector
{

    </span><span>private</span> <span>static</span> <span>$charListHandle</span> = <span>array</span><span>(      
        </span>"黑名单" => "BacklistCharListHandle"<span>
    );

    </span><span>public</span> <span>static</span> <span>function</span> select(<span>$num_iid</span><span>)
    {
        </span><span>$selectorItem</span> = SelectorItem::createFromApi(<span>$num_iid</span><span>);

        Logger</span>::trace(<span>$selectorItem</span>-><span>props_name);

        </span><span>$charlist</span> = <span>new</span><span> CharList();

        </span><span>foreach</span> (self::<span>$charListHandle</span> <span>as</span> <span>$matchKey</span> => <span>$className</span><span>) {

            </span><span>$handle</span> = self::createCharListHandle(<span>$className</span>, <span>$charlist</span>, <span>$selectorItem</span><span>);
            </span><span>$handle</span>-><span>exec</span><span>();
           
        }

        </span><span>//</span><span>do search things</span>
        <span>var_dump</span>(<span>$charlist</span><span>);
    }

    </span><span>public</span> <span>static</span> <span>function</span> createCharListHandle(<span>$className</span>, <span>$charlist</span>, <span>$selectorItem</span><span>)
    {
        </span><span>if</span> (<span>class_exists</span>(<span>$className</span><span>)) {
            </span><span>return</span> <span>new</span> <span>$className</span>(<span>$charlist</span>, <span>$selectorItem</span><span>);
        }
        </span><span>throw</span> <span>new</span> <span>Exception</span>("class not exists", 0<span>);
    }
}</span>

小帅帅的原稿本来不是这样的,他的原稿:

<span>CharListHandle 的属性有两个 一个是charlist 一个是cid<br /><br />于老大看了之后就要小帅帅把cid换成了selectorItem,原因嘛要小帅帅去琢磨。<br /><br /><br /></span>

小帅帅还写了个DB操作的类,代码如下:

<?<span>php

</span><span>define</span>('DATABASE_HOST','127.0.0.1'<span>);
</span><span>define</span>('DATABASE_USER','xiaoshuaishuai'<span>);
</span><span>define</span>('DATABASE__PASSWORD','xiaoshuaishuai'<span>);
</span><span>define</span>('DATABASE_CHARSET','utf-8'<span>);

</span><span>class</span><span> DB {

    </span><span>public</span> <span>static</span> <span>$conn</span> = <span>null</span><span>;
    
    </span><span>public</span> <span>static</span> <span>function</span><span> Connect(){
        </span><span>if</span>(self::<span>$conn</span> == <span>null</span><span>){
            self</span>::<span>$conn</span> = <span>mysql_connect</span>(DATABASE_HOST,DATABASE_USER,<span>DATABASE__PASSWORD);
            </span><span>mysql_query</span>("SET NAMES '".DATABASE_CHARSET."'",self::<span>$conn</span><span>);
            </span><span>mysql_select_db</span>("dict",self::<span>$conn</span><span>);
            </span><span>return</span> self::<span>$conn</span><span>;
        }
        </span><span>return</span> self::<span>$conn</span><span>;
    }
    
    </span><span>public</span> <span>static</span> <span>function</span> Query(<span>$sql</span><span>){
       </span><span>return</span> <span>mysql_query</span>(<span>$sql</span>,self::<span>Connect());
    }
    
    </span><span>public</span> <span>static</span> <span>function</span> makeArray(<span>$sql</span><span>){
        </span><span>$rs</span> = self::Query(<span>$sql</span><span>);
        </span><span>$result</span> = <span>array</span><span>();
        </span><span>while</span>(<span>$data</span> = <span>mysql_fetch_assoc</span>(<span>$rs</span><span>)){
            </span><span>$result</span>[] = <span>$data</span><span>;
        }
        </span><span>return</span> <span>$result</span><span>;
    }
} </span>

小帅帅把这代码拿给于老大的时候,于老大很开心。

于老大对小帅帅说,现在我们这个黑名单的改动,不管你怎么加或者怎么删除都不会影响系统的正常运行了把,而且我们可以很好的跟业务结合,这下这个黑名单的任务,小帅帅你觉得教给谁去做比较合适。

小帅帅听到可以把任务转交,很开心:做这些事情,我觉得产品或者运营部门比较合适。

于老大拍了拍小帅帅的肩膀说道:这件事情就交给你去落实了。

 

偶尔的职责分离不一定是懒惰的行为,从整体上来说是让整个服务做到更专业、更有效率。

 

小帅帅只好去落实于老大的任务去了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn