Php weight calculation method code sharing

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php
/* Vim: set expandtab tabstop = 4 shiftwidth = 4 :*/
// + ------------------------------------------------------------------------
// Name: Weight Calculation
// Description: slightly modified. It can also be used for word segmentation, Word Frequency Statistics, full-text search, and spam detection.
// Date: 2013/12/16 08:51

Class weight {
Protected $ aDict = array ());
Protected $ aItems = array ();
Protected $ sLastRule;
Protected $ aMatchs = array ();
Protected $ aShow = array ();

Private function init (){
// Clear the matching table and output result of the record
Unset ($ this-> aShow );
}

Public function newItems ($ mItems ){
// Import a new project
$ This-> aItems = (is_array ($ mItems ))? $ MItems: array ($ mItems );
$ This-> init ();
}

Public function newTable (array $ aTable ){
// Import a new 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 '){
// Obtain 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. = "\ 0 ";
$ ILen = strlen ($ sLine );
$ AReturn = array ();
While ($ iOffset <$ iLen ){
$ SChar = $ sLine {$ iOffset };
If (isset ($ this-> aDict [$ iCur] [$ sChar]) {
$ ICur = $ this-> aDict [$ iCur] [$ sChar];
If (isset ($ this-> aDict [$ iCur] ['Acc ']) {
$ AReturn = array_merge ($ aReturn, $ this-> aDict [$ iCur] ['Acc ']);

$ IPosition = $ iOffset + 1;
$ ICur = 0;
}
} Else {
$ ICur = 0;
$ IOffset = $ iPosition;
$ IPosition = $ iOffset + 1;
}
++ $ IOffset;
}
Return $ aReturn;
}
}

?>

External call example
Copy codeThe Code is as follows:
$ 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 );

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.