PageRenderTime 65ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/components/spam_shield.php

https://github.com/felixding/LonelyThinker
PHP | 118 lines | 36 code | 12 blank | 70 comment | 0 complexity | 07c65efbb28e37893f0b931f2d733c82 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* SVN FILE: $Id: spam_shield.php 1 2009-04-16 13:02:44Z $ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 5
  9. *
  10. * Licensed under The BSD License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @filesource
  14. * @copyright Copyright 2007-2009, Felix Ding (http://dingyu.me)
  15. * @link http://lonelythinker.org Project LonelyThinker
  16. * @package LonelyThinker
  17. * @author $LastChangedBy: $
  18. * @version $Revision: 1 $
  19. * @modifiedby $LastChangedBy: $
  20. * @lastmodified $Date: 2009-04-16 21:02:44 +0800 (四, 16 4 2009) $
  21. * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  22. */
  23. class SpamShieldComponent extends Object
  24. {
  25. /**
  26. * b8 instance
  27. */
  28. var $b8;
  29. /**
  30. * standard rating
  31. *
  32. * ratings which are higher than this one will be considered as SPAM
  33. */
  34. var $standardRating = 0.7;
  35. /**
  36. * text to be classified
  37. */
  38. var $text;
  39. /**
  40. * rating of the text
  41. */
  42. var $rating;
  43. /**
  44. * Constructor
  45. *
  46. * @date 2009-1-20
  47. */
  48. function startup(&$controller)
  49. {
  50. //register a CommentModel to get the DBO resource link
  51. $comment = ClassRegistry::init('Comment');
  52. //import b8 and create an instance
  53. App::import('Vendor', 'b8/b8');
  54. $this->b8 = new b8($comment->getDBOResourceLink());
  55. //set standard rating
  56. $this->standardRating = Configure::read('LT.bayesRating') ? Configure::read('LT.bayesRating') : $this->standardRating;
  57. }
  58. /**
  59. * Set the text to be classified
  60. *
  61. * @param $text String the text to be classified
  62. * @date 2009-1-20
  63. */
  64. function set($text)
  65. {
  66. $this->text = $text;
  67. }
  68. /**
  69. * Get Bayesian rating
  70. *
  71. * @date 2009-1-20
  72. */
  73. function rate()
  74. {
  75. //get Bayes rating and return
  76. return $this->rating = $this->b8->classify($this->text);
  77. }
  78. /**
  79. * Validate a message based on the rating, return true if it's NOT a SPAM
  80. *
  81. * @date 2009-1-20
  82. */
  83. function validate()
  84. {
  85. return $this->rate() < $this->standardRating;
  86. }
  87. /**
  88. * Learn a SPAM or a HAM
  89. *
  90. * @date 2009-1-20
  91. */
  92. function learn($mode)
  93. {
  94. return $this->b8->learn($this->text, $mode);
  95. }
  96. /**
  97. * Unlearn a SPAM or a HAM
  98. *
  99. * @date 2009-1-20
  100. */
  101. function unlearn($mode)
  102. {
  103. return $this->b8->unlearn($this->text, $mode);
  104. }
  105. }
  106. ?>