ITeye博客

Bayes算法对垃圾邮件的过滤(转载)

MyEyeOfJava · 2009-11-06 15:59 · Markdown

这段时间抽空做了一个利用Bayes算法来模拟对邮件进行过滤的小东东,算是对自己的一个锻炼吧。前前后后总共花了一周多的时间。 
      有关Bayes算法在反垃圾邮件中的应用,请看这里:

            http://www.5dmail.net/html/2006-5-18/2006518234548.htm

      很显然,上面的链接给的一个demo中,有一个比较大的缺陷,那就是它只是一个一个地统计字,而不是词语。在处理中文的问题上,demo的思路是行不通的,必须统计词语而不是一个一个的单字。从大量的文本中提取一个一个的我们熟悉的词语,说起来容易,做起来就相当的困难了,这个问题纠结了我很多天,终于发现原来就在javaEye首页上有对中文分词器IKAnalyzer的介绍,真的是“踏破铁鞋无觅处,得来全不费功夫”,立马把IKAnalyzer“偷”了过去,自己使用了一下,果然是非常的强大。

      有关IKAnalyzer中文分词器,请参看如下地址:

           http://www.iteye.com/wiki/interview/1899-ik-analyzer

     下面贴一段代码,看看这个分词器的强大: 

Java代码 复制代码

  1. <SPAN style="FONT-SIZE: small">package ik.com.cn.test;
  2. import java.io.IOException;
package ik.com.cn.test;

import java.io.IOException;
import java.io.StringReader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.wltea.analyzer.lucene.IKAnalyzer;
/*
 * 分词的小Demo
 * 这个分词工具确实比较强悍
 * 相关的jar包在附件eclipse工程下lib包下
 */
public class SecondTest {
  public static void main(String args[]) throws IOException{
	    Analyzer analyzer = new IKAnalyzer();
		String text="中华人民共和国"; 
		StringReader reader = new StringReader(text); 
		TokenStream ts = analyzer.tokenStream(text, reader); 
		Token t = ts.next(new Token()); 
		while (t != null) {
		    String s=t.term();
		    System.out.println(s); 
		    t = ts.next(new Token()); 
		} 
  }
}

 

 

Java代码 复制代码


 输出结果:

Java代码 复制代码

  1. <SPAN style="FONT-SIZE: small">中华人民共和国
  2. 中华人民
  3. 中华
  4. 华人
  5. 人民共和国
  6. 人民
  7. 共和国
  8. 共和</SPAN>
中华人民共和国
中华人民
中华
华人
人民共和国
人民
共和国
共和

 

Java代码 复制代码


   下面贴出一个比较核心的类Bayes,主要是实现具体的算法。

 

Java代码 复制代码

  1. <SPAN style="FONT-SIZE: small">package com.cn.bayes;
  2. import java.util.*;
  3. public class Bayes {
  4. private Hashtable<String, Integer> hashtable_good;
  5. private Hashtable<String, Integer> hashtabel_bad;
  6. private Hashtable<String, Double> hashtable_good_p;
  7. private Hashtable<String, Double> hashtable_bad_p;
  8. private Hashtable<String, Double> hashtable_probability;
  9. public Hashtable<String, Double> getHashtable_probability() {
  10. return hashtable_probability;
  11. }
  12. public void setHashtable_probability(
  13. Hashtable<String, Double> hashtableProbability) {
  14. hashtable_probability = hashtableProbability;
  15. }
  16. public Bayes(Hashtable<String, Integer> hashtable_good,
  17. Hashtable<String, Integer> hashtabel_bad) {
  18. this.hashtable_good = hashtable_good;
  19. this.hashtabel_bad = hashtabel_bad;
  20. this.hashtable_good_p = this.getGoodOrBadPercent(hashtable_good);
  21. this.hashtable_bad_p = this.getGoodOrBadPercent(hashtabel_bad);
  22. Set<String> set_allkeys = this.combineHasetableByKeys(this
  23. .getHashtable_good(), this.getHashtabel_bad());
  24. //      Set s=hashtable_good_p.entrySet();
  25. //      System.out.println("hashtable_good_p");
  26. //      for (Object o :s.toArray()) {
  27. //          System.out.print("  "+o);
  28. //      }
  29. //      s=hashtable_bad_p.entrySet();
  30. //      System.out.println("hashtable_bad_p");
  31. //      for (Object o :s.toArray()) {
  32. //          System.out.print("  "+o);
  33. //      }
  34. //
  35. //      for (Object o : set_allkeys.toArray()) {
  36. //          System.out.print("  "+o);
  37. //      }
  38. this.hashtable_probability = this
  39. .calcHashtable_probability(set_allkeys);
  40. //      System.out.println();
  41. //        s=hashtable_probability.entrySet();
  42. //      for (Object o : s.toArray()) {
  43. //          System.out.print("  "+o);
  44. //      }
  45. }
  46. /*
  47. * 通过统计计算 得到 hashtable_good_p 或 hashtable_bad_p
  48. */
  49. @SuppressWarnings("unchecked")
  50. private Hashtable<String, Double> getGoodOrBadPercent(
  51. Hashtable hashtable_goodOrBad) {
  52. Hashtable<String, Double> percent = new Hashtable<String, Double>();
  53. int total = 0;
  54. String key;
  55. Integer value;
  56. Enumeration enumeration = hashtable_goodOrBad.elements();
  57. while (enumeration.hasMoreElements()) {
  58. total = total + (Integer) enumeration.nextElement();
  59. }
  60. //      System.out.println("total=" + total);
  61. enumeration = hashtable_goodOrBad.keys();
  62. while (enumeration.hasMoreElements()) {
  63. key = (String) enumeration.nextElement();
  64. value = (Integer) hashtable_goodOrBad.get(key);
  65. //          System.out.println(key + "   " + value);
  66. percent.put(key, new Double((value + 0.0) / total));
  67. }
  68. //       Set s = percent.entrySet();
  69. //       for (Object o : s.toArray()) {
  70. //       System.out.println(o);
  71. //       }
  72. return percent;
  73. }
  74. /*
  75. * 将两个hash表的所有key值保存在一个Set中,Set是不允许出现重复的元素
  76. *
  77. * 注意:这个也比较容易扩展将多个hash表的所有key保存在一个Set中
  78. *
  79. */
  80. @SuppressWarnings("unchecked")
  81. private Set<String> combineHasetableByKeys(
  82. Hashtable<String, Integer> hashtable_good,
  83. Hashtable<String, Integer> hashtabel_bad) {
  84. Set<String> allkeysSet = new HashSet();
  85. Set<String> goodKeysSet = hashtable_good.keySet();
  86. Set<String> badKeysSet = hashtabel_bad.keySet();
  87. Iterator it;
  88. it = goodKeysSet.iterator();
  89. while (it.hasNext()) {
  90. allkeysSet.add(it.next().toString());
  91. }
  92. it = badKeysSet.iterator();
  93. while (it.hasNext()) {
  94. allkeysSet.add(it.next().toString());
  95. }
  96. return allkeysSet;
  97. }
  98. /*
  99. * 根据Set提供的key值,计算每个key对应出现的可能性,并封装到hashtable中
  100. *
  101. */
  102. @SuppressWarnings("unchecked")
  103. private Hashtable<String, Double> calcHashtable_probability(
  104. Set<String> set_allkeys) {
  105. Iterator it = set_allkeys.iterator();
  106. Hashtable<String, Double> hashtable_probability = new Hashtable();
  107. while (it.hasNext()) {
  108. String key = it.next().toString();
  109. Double good_p_value = this.hashtable_good_p.get(key);
  110. Double bad_p_value = this.hashtable_bad_p.get(key);
  111. if (null == good_p_value) {
  112. good_p_value = 0.0;
  113. }
  114. if (null == bad_p_value) {
  115. bad_p_value = 0.0;
  116. }
  117. Double result = good_p_value + bad_p_value;
  118. Double percent=null;
  119. if (result != 0.0) {
  120. percent = bad_p_value / result;
  121. }
  122. hashtable_probability.put(key, percent);
  123. }
  124. return hashtable_probability;
  125. }
  126. public Hashtable<String, Integer> getHashtable_good() {
  127. return hashtable_good;
  128. }
  129. public Hashtable<String, Integer> getHashtabel_bad() {
  130. return hashtabel_bad;
  131. }
  132. }</SPAN>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。