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

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

WBOY
WBOYoriginal
2016-06-13 09:27:011221parcourir

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

第十二天

起点:

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

回顾:

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

上回说到,关键词应用需求为:

通过淘宝API取到的宝贝标题以及宝贝属性,匹配出适合该宝贝的关键词.

初期考虑以下因素:

适合人群的匹配 :男装 (匹配出来的关键词不能有女)  女装(匹配出来的关键词不能有男) 情侣装(男女适用)  童装(?)

 

淘宝API取出的宝贝属性字段:

小帅帅想了很久,总算想出来了一个解决方案,方案如下:

 

<?<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>
}

小帅帅很高兴的把代码拿给于老大时,小帅帅被于老大批了,原因很简单:

1.  没有考虑未来变化因素

2.  if 太多

小帅帅被批了,心里很不高兴,但还是不得不去请教于老大的高招。

于老大给了一个方向给他。

1. 学习设计模式消除过多的if,以及如何去解耦。

 

设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。 毫无疑问,设计模式于己于他人于系统都是多赢的;设计模式使代码编制真正工程化;设计模式是软件工程的基石脉络,如同大厦的结构一样。[来自百度百科]

 

小帅帅只好去了解设计模式了。

 

附:

<span>SelectorItem 里面的 __get 函数,称为Magic Methods<br />如:</span>
$selectorItem->title 其实会调用 __get('title')
<span><br /><br /></span>

 

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn