Bayes算法对垃圾邮件的过滤

 这段时间抽空做了一个利用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.   
  3. 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代码 复制代码
  1.   

 输出结果:

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

 

Java代码 复制代码
  1.   

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

 

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

你可能感兴趣的:(apache,eclipse,.net,算法,Lucene)