Home >Backend Development >PHP Tutorial >Teach you step by step how to do a keyword matching project (search engine)----Day 16, teach you how to do it-_PHP Tutorial
Day 16
Friendly 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 15
14 days pdf version: 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
Source code within 15 days:
http://files.cnblogs.com/oshine/myproject.rar
After the last discussion between Xiao Shuai Shuai and Boss Yu, there is a very depressing thing that has not yet been settled. That is, we already have correspondence, antonyms and synonyms. What is the data format?
Xiao Shuai Shuai needs to understand these before he can continue, so he went to ask Boss Yu for advice.
Boss Yu said to Xiao Shuaishuai, let’s not consider so many factors first, let’s start from reality first, let’s do the antonyms first, let’s start with women’s clothing, women’s clothing is a category, let’s go by category Start doing it.
Then the fields of the table structure have categories and antonyms.
Xiao Shuai Shuai created the watch:
<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
Xiao Shuai Shuai also completed the data.
<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>
Xiao Shuai Shuai easily corrected the code.
<?<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>
Xiao Shuaishuai’s original manuscript was not like this. His original manuscript:
<span>CharListHandle 的属性有两个 一个是charlist 一个是cid<br /><br />于老大看了之后就要小帅帅把cid换成了selectorItem,原因嘛要小帅帅去琢磨。<br /><br /><br /></span>
Xiao Shuaishuai also wrote a class for DB operations, the code is as follows:
<?<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>
When Xiao Shuai Shuai showed this code to Boss Yu, Boss Yu was very happy.
Boss Yu said to Xiao Shuaishuai, now the changes to our blacklist, no matter how you add or delete them, will not affect the normal operation of the system, and we can integrate it well with the business, now this For the blacklist task, Xiaoshuai, who do you think would be more appropriate to teach?
Xiao Shuaishuai was very happy when he heard that the task could be transferred: I think the product or operations department is more suitable for doing these things.
Boss Yu patted Xiao Shuaishuai on the shoulder and said: I will leave this matter to you to implement.
Occasional separation of duties is not necessarily lazy behavior, and overall makes the entire service more professional and efficient.
Little Shuaishuai had no choice but to go and implement the boss’s mission