PHP 根据类目ID获取PDD主类目

180it 2022-08-07 PM 1170℃ 0条
$obj = new MaxWordSegmentation();
$ret = $obj->run('14966');
echo $ret[0][1];
//var_dump($ret);

class MaxWordSegmentation{

    private $dict = array();//保存字典的list

    function __construct(){
        $this->dict = require('idlist.php');
    }
    
    

    //查看词是否在字典中
    function inDict($word){
        return array_key_exists($word,$this->dict);
    }

    //按照词典进行分词。正向最大匹配法
    function run($text,$encode = 'utf-8'){
        $minLen = 0;
        $maxLen = 0;
        //找出最长的单词长度及最短的单词长度
        foreach($this->dict as $key=>$value){
            $iLen = mb_strlen($key,$encode);
            if($minLen > $iLen || $minLen == 0 ){
                $minLen = $iLen;
            }

            if($maxLen < $iLen){
                $maxLen = $iLen;
            }
        }
        

        $sLen = mb_strlen($text, $encode);
        $result = array();
        for($start = 0;$start < $sLen;$start ++){    
            for($maxLoop = $maxLen;$maxLoop >= $minLen;$maxLoop --){
                $word = mb_substr ($text , $start, $maxLoop , $encode);
                //是否匹配成功
                if($this->inDict($word,$this->dict)){
                    //添加到输出列表
                    if(!in_array($word,$result)){
                        $result[] = array($word,$this->dict[$word]);
                    }
                    break;
                }
            }
            
            
        }
        return $result;
    }
}


idlist.php

<?php return array (
'17412' => '家居生活',
'16901' => '家居生活',
'17671' => '家居生活',
'8721' => '虚拟商品',
'8722' => '虚拟商品',
'5906' => '数码电器',
'8723' => '虚拟商品',
'8724' => '虚拟商品',
'8725' => '虚拟商品',
'8726' => '虚拟商品',
'8727' => '虚拟商品',
'8728' => '虚拟商品',
'8729' => '虚拟商品',
'8730' => '虚拟商品',
'16155' => '家居生活',
'8731' => '虚拟商品',
'8732' => '虚拟商品',
'8733' => '虚拟商品',
'8734' => '虚拟商品',
'8736' => '虚拟商品',
'5921' => '数码电器',
'16676' => '家居生活',
'2603' => '数码电器',
'17455' => '家居生活',
'18482' => '美容个护',
'8508' => '服饰箱包',
'8509' => '服饰箱包',
'16192' => '家居生活',
'6209' => '数码电器',
'5955' => '数码电器',
'2629' => '数码电器',
'16209' => '家居生活',
'14933' => '母婴玩具',
'8538' => '服饰箱包',
'16989' => '家居生活',
'17249' => '家居生活',
'5217' => '家居生活',
'9313' => '家纺家具家装',
'19298' => '母婴玩具',
'9314' => '家纺家具家装',
'9315' => '家纺家具家装',
'9316' => '家纺家具家装',
'9317' => '家纺家具家装',
'6758' => '食品保健',
'9318' => '家纺家具家装',
'9319' => '家纺家具家装',
'9320' => '家纺家具家装',
'14697' => '母婴玩具',
'9321' => '家纺家具家装',
'9322' => '家纺家具家装',
'9323' => '家纺家具家装',
'9324' => '家纺家具家装',
'20078' => '汽配摩托',
'2933' => '数码电器',
'14966' => '母婴玩具',
'5752' => '数码电器',
'6785' => '食品保健',
'17285' => '家居生活',
'8583' => '服饰箱包',
'6536' => '食品保健',
'17803' => '家居生活',
'18574' => '美容个护',
'6290' => '数码电器',
'14740' => '母婴玩具',
'16794' => '家居生活',
'7323' => '数码电器',
'16288' => '家居生活',
'11683' => '运动户外',
'16548' => '家居生活',
'11684' => '运动户外',
'20645' => '母婴玩具',
'11685' => '运动户外',
'11686' => '运动户外',
'11687' => '运动户外',
'11688' => '运动户外',
'18088' => '家纺家具家装',
'18601' => '美容个护',
'21417' => '家居生活',
'11689' => '运动户外',
'15543' => '家居生活',
'8634' => '服饰箱包',
'6586' => '食品保健',
'6076' => '数码电器',
'5834' => '数码电器',
'18637' => '美容个护',
'7629' => '汽配摩托',
'5839' => '数码电器',
'7639' => '汽配摩托',
'5851' => '数码电器',
'8669' => '服饰箱包',
'6883' => '食品保健',
'6630' => '食品保健',
'15083' => '母婴玩具',
'8172' => '水果生鲜',
'17134' => '家居生活',
'239' => '服饰箱包',
'6128' => '数码电器',
'8439' => '服饰箱包',
'15356' => '母婴玩具',
'6398' => '食品保健',


);

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

PHP 根据类目ID获取PDD主类目