PHP weight calculation method code sharing

Source: Internet
Author: User
Tags foreach comparison table

  Weight calculation, a little modification can also be used for participle, word frequency statistics, full-text and spam detection, etc.

The code is as follows: <?php/* Vim:set expandtab tabstop=4 shiftwidth=4: *//+---------------------------------------------------- --------------------// name      :   weight calculation               &NB Sp                         // description:   Minor Repair Change, can also be used for participle, word frequency statistics, full-text Search and garbage detection// date      :   2013/12/16 08:51   class Weight {    p rotected $aDict = Array (array ());     Protected $aItems = Array ();     protected $sLastRule;     Protected $aMatchs = Array ();     Protected $aShow = Array ();    private function init () { //Empty records matching table and output   unset ($this->ashow);         Public Function NewItems ($mItems) { //import new project   $this->aitems = (Is_array ($mItems))? $mItems: Array ($mItems );   $this->init (); &NBSP}    public function neWtable (array $aTable) {       //Import a new comparison table and generate a dictionary         foreach ($aTable as $iTable Key=> $sTableLine) {            $aTableLine = explode (', ', Str_replace (' | ', ', '), $sTableL INE);              $setter = function ($v, $k, $paraMeter) {      &NBSP ;         $K 1 = $paraMeter [0]; $oWeight = $paraMeter [1];                 $oWeight->gendict ($v, $k 1);            };             Array_walk ($aTableLine, $setter, Array ($iTableKey, $this));        }         $this->init (); &NBSP}       public Function getshow ($sRule = ' Max ') { //Get final display results         if (E Mpty ($this->aitems) | | Empty ($this->adict))             return array ();   If (Empty ($this->ashow) | | $sRule!= $this->slastrule)               return $this->genshow ($sRule);         return $this->ashow; &NBSP}       public Function genshow ($sRule) {        $aShow = array ();         $aMatchs = array ();   $getter = function ($v, $k, $oWeight) use (& $aShow, & $aMatchs, $sRule) {    $t = array_count_values ($oWeight->matchword ($v));             $aMatchs [] = $t;             switch ($sRule) {                case ' Max ':                     $aShow [$k] = Array_keys ($t, Max ($t));  &N Bsp                   break;            }  };   Array_walk ($this->aitems, $getter, $this);   $this->ashoW = $aShow;   $this->amatchs = $aMatchs;   return $aShow;     {      private Function gendict ($mWord, $iKey = ') {        $iInsertPo NIT = count ($this->adict);         $iCur = 0; Current node number         foreach (Str_split ($mWord) as $iChar) {            if ( Isset ($this->adict[$iCur] [$iChar]) {                $iCur = $this->adict[$ icur][$iChar];                 continue;                         $this->adict[$iInsertPonit] = arr Ay ();             $this->adict[$iCur] [$iChar] = $iInsertPonit;             $iCur = $iInsertPonit;             $iInsertPonit + +;        }         $this->adict[$iCur] [' ACC '] [] = $iKey;       }           function Matchword ($sLine) {& nbsp           $iCur = $iOffset = $iPosition = 0;             $sLine. = "";             $iLen = strlen ($sLine);             $aReturn = array ();             while ($iOffset < $iLen) {              &N Bsp $sChar = $sLine {$iOffset};                 if (isset ($this->adict[$iCur] [$sChar]) {      & nbsp             $iCur = $this->adict[$iCur] [$sChar];                     if (isset ($this->adict[$iCur] [' ACC ']) {  &N Bsp                     $aReturn = Array_merge ($aReturn, $this->adict[$iC Ur]['ACC ']);                           $iPosition = $iOffset + 1;                         $iCur = 0;                                   & nbsp else {                    $iCur = 0         &NBS P           $iOffset = $iPosition;                     $iPosition = $iOffset + 1;                                 + + $iOffs Et                         return $aReturn;        }}  ?>       External invocation example   code is as follows: $aItems = Array (    ' chin Aisbig ',   &NBSP ' Whichisnot ',     ' totalyrightforme ',); $aTable = Array (    ' China,is|small ',     ' China,big|me ',     ' China,is|big,which|not,me ',     ' totaly|right,for,me ',);   $oWeight = new Ttrie; $oWeight->newitems ($aItems); $aResult = $oWeight->newtable ($aTable);  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.