Efficiency is very objective. If you change it to other uses, I cannot guarantee the efficiency.
- /* vim: set expandtab tabstop=4 shiftwidth=4: */
- // +--------------------- --------------------------------------------------
- // Name: Weight calculation
- // Description: With slight modifications, it can also be used for word segmentation, word frequency statistics, full-text retrieval and garbage detection
- // Date: 2013/12/16 08:51
- // Authors: late < latelx64@gmail.com>
- // +---------------------------------------- --------------------------------
- //
-
- /*external call example*/
- /*
- $aItems = array(
- 'chinaisbig',
- '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);
- */
-
- class weight {
- protected $aDict = array(array());
- protected $aItems = array();
- protected $sLastRule;
- protected $aMatchs = array( );
- protected $aShow = array();
-
- private function init() {
- //Clear the recorded matching table and output results
- unset($this->aShow);
- }
-
- public function newItems($ mItems) {
- //Import new items
- $this->aItems = (is_array($mItems))? $mItems: array($mItems);
- $this->init();
- }
-
- public function newTable(array $aTable) {
- //Import a new comparison table and generate a dictionary
- foreach($aTable as $iTableKey=>$sTableLine) {
- $aTableLine = explode(',', str_replace('|' , ',', $sTableLine));
- $setter = function($v, $k, $paraMeter) {
- $k1 = $paraMeter[0]; $oWeight = $paraMeter[1];
- $oWeight-> ;genDict($v, $k1);
- };
- array_walk($aTableLine, $setter, array($iTableKey, $this));
- }
- $this->init();
- }
-
- public function getShow($sRule = 'max') {
- //Get the final display result
- if(empty($this->aItems) || empty($this->aDict))
- return array();
- if (empty($this->aShow) || $sRule != $this->sLastRule)
- return $this->genShow($sRule);
- return $this->aShow;
- }
-
- 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));
- break;
- }
- };
- array_walk($this->aItems, $getter, $this);
- $this->aShow = $aShow;
- $ this->aMatchs = $aMatchs;
- return $aShow;
- }
-
- private function genDict($mWord, $iKey = '') {
- $iInsertPonit = 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] = array();
- $this->aDict[$iCur][$iChar] = $iInsertPonit ;
- $iCur = $iInsertPonit;
- $iInsertPonit++;
- }
- $this->aDict[$iCur]['acc'][] = $iKey;
-
- }
-
- function matchWord($sLine) {
- $ iCur = $iOffset = $iPosition = 0;
- $sLine .= "
-
- ?>
Copy code
|