search
HomeBackend DevelopmentPHP TutorialWeight calculation, with slight modifications, can also be used for word segmentation, word frequency statistics, full text and spam detection, etc.

Efficiency is very objective. If you change it to other uses, I cannot guarantee the efficiency.
  1. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  2. // +--------------------- --------------------------------------------------
  3. // Name: Weight calculation
  4. // Description: With slight modifications, it can also be used for word segmentation, word frequency statistics, full-text retrieval and garbage detection
  5. // Date: 2013/12/16 08:51
  6. // Authors: late
  7. // +---------------------------------------- --------------------------------
  8. //
  9. /*external call 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. //Clear the recorded matching table and output results
  34. unset($this->aShow);
  35. }
  36. public function newItems($ mItems) {
  37. //Import new items
  38. $this->aItems = (is_array($mItems))? $mItems: array($mItems);
  39. $this->init();
  40. }
  41. public function newTable(array $aTable) {
  42. //Import a new comparison table and generate a dictionary
  43. foreach($aTable as $iTableKey=>$sTableLine) {
  44. $aTableLine = explode(',', str_replace('|' , ',', $sTableLine));
  45. $setter = function($v, $k, $paraMeter) {
  46. $k1 = $paraMeter[0]; $oWeight = $paraMeter[1];
  47. $oWeight-> ;genDict($v, $k1);
  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 .= "
  96. ?>
Copy code


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.