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

Source: Internet
Author: User
The efficiency is very objective, if you change to other uses that efficiency I don't promise.
  1. /* Vim:set expandtab tabstop=4 shiftwidth=4: */
  2. // +------------------------------------------------------------------------
  3. Name: Weight Calculation
  4. Description: Slightly modified, can also be used for word segmentation, frequency statistics, full-text search and garbage detection
  5. DATE:2013/12/16 08:51
  6. Authors:latel
  7. // +------------------------------------------------------------------------
  8. //
  9. /* External Invocation example */
  10. /*
  11. $aItems = Array (
  12. ' Chinaisbig ',
  13. ' Whichisnot ',
  14. ' Totalyrightforme ',
  15. );
  16. $aTable = Array (
  17. ' China,is|small ',
  18. ' China,big|me ',
  19. ' China,is|big,which|not,me ',
  20. ' Totaly|right,for,me ',
  21. );
  22. $oWeight = new Ttrie;
  23. $oWeight->newitems ($aItems);
  24. $aResult = $oWeight->newtable ($aTable);
  25. */
  26. Class Weight {
  27. Protected $aDict = Array (array ());
  28. Protected $aItems = Array ();
  29. protected $sLastRule;
  30. Protected $aMatchs = Array ();
  31. Protected $aShow = Array ();
  32. Private Function init () {
  33. Clears the record's match table and output results
  34. unset ($this->ashow);
  35. }
  36. Public Function NewItems ($mItems) {
  37. Import a new project
  38. $this->aitems = (Is_array ($mItems))? $mItems: Array ($mItems);
  39. $this->init ();
  40. }
  41. Public Function newtable (array $aTable) {
  42. Import a new table and generate a dictionary
  43. foreach ($aTable as $iTableKey = = $sTableLine) {
  44. $aTableLine = Explode (', ', Str_replace (' | ', ', ', $sTableLine));
  45. $setter = function ($v, $k, $paraMeter) {
  46. $k 1 = $paraMeter [0]; $oWeight = $paraMeter [1];
  47. $oWeight->gendict ($v, $k 1);
  48. };
  49. Array_walk ($aTableLine, $setter, Array ($iTableKey, $this));
  50. }
  51. $this->init ();
  52. }
  53. Public Function getshow ($sRule = ' Max ') {
  54. Get the final display result
  55. if (Empty ($this->aitems) | | empty ($this->adict))
  56. return Array ();
  57. if (Empty ($this->ashow) | | $sRule! = $this->slastrule)
  58. return $this->genshow ($sRule);
  59. return $this->ashow;
  60. }
  61. Public Function Genshow ($sRule) {
  62. $aShow = Array ();
  63. $aMatchs = Array ();
  64. $getter = function ($v, $k, $oWeight) use (& $aShow, & $aMatchs, $sRule) {
  65. $t = Array_count_values ($oWeight->matchword ($v));
  66. $aMatchs [] = $t;
  67. Switch ($sRule) {
  68. Case ' Max ':
  69. $aShow [$k] = Array_keys ($t, Max ($t));
  70. Break
  71. }
  72. };
  73. Array_walk ($this->aitems, $getter, $this);
  74. $this->ashow = $aShow;
  75. $this->amatchs = $aMatchs;
  76. return $aShow;
  77. }
  78. Private Function Gendict ($mWord, $iKey = ") {
  79. $iInsertPonit = count ($this->adict);
  80. $iCur = 0; Current node number
  81. foreach (Str_split ($mWord) as $iChar) {
  82. if (Isset ($this->adict[$iCur [$iChar])) {
  83. $iCur = $this->adict[$iCur] [$iChar];
  84. Continue
  85. }
  86. $this->adict[$iInsertPonit] = array ();
  87. $this->adict[$iCur] [$iChar] = $iInsertPonit;
  88. $iCur = $iInsertPonit;
  89. $iInsertPonit + +;
  90. }
  91. $this->adict[$iCur [' ACC '] [] = $iKey;
  92. }
  93. function Matchword ($sLine) {
  94. $iCur = $iOffset = $iPosition = 0;
  95. $sLine. = "n";
  96. $iLen = strlen ($sLine);
  97. $aReturn = Array ();
  98. while ($iOffset < $iLen) {
  99. $sChar = $sLine {$iOffset};
  100. if (Isset ($this->adict[$iCur [$sChar])) {
  101. $iCur = $this->adict[$iCur] [$sChar];
  102. if (Isset ($this->adict[$iCur [' ACC '])) {
  103. $aReturn = Array_merge ($aReturn, $this->adict[$iCur [' acc ']);
  104. $iPosition = $iOffset + 1;
  105. $iCur = 0;
  106. }
  107. } else {
  108. $iCur = 0;
  109. $iOffset = $iPosition;
  110. $iPosition = $iOffset + 1;
  111. }
  112. + + $iOffset;
  113. }
  114. return $aReturn;
  115. }
  116. }
  117. ?>
Copy Code
  • 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.