IKEANalyzer中国語分詞器を利用してBayesアルゴリズムをシミュレートして迷惑メールのフィルタリングの実現(ソース付き)


この時間は時間を割いてBayesアルゴリズムを利用してメールを濾過する小さい東を模擬して、自分に対する鍛錬と言えるでしょう.全部で一週間以上かかりました.       Bayesアルゴリズムの迷惑防止メールへの応用については、ここをご覧ください.
            http://www.5dmail.net/html/2006-5-18/2006518234548.htm
      明らかに上のリンクはdemoの中に大きな欠陥があります.それはただ一つの統計文字で、言葉ではありません.中国語の問題を処理する上で、demoの考えは通用しないので、必ず単語を統計して一つの単語ではありません.大量のテキストからなじみのある言葉を抽出して言うのは簡単です.作るのはかなり難しいです.この問題は何日間も悩みました.やっとJavaEyeのトップページに中国語の分詞器IKAnalyzerの紹介がありました.本当に「鉄の靴を踏み潰すところがなくて、時間がかかりません.」自分で使ってみましたが、やはりとても強いです.
      IKAANalyzer中国語の分詞器については、下記の住所を参照してください.
            http://www.iteye.com/wiki/interview/1899-ik-analyzer
     次はコードを貼ります.この分詞器の強さを見てください. 
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()); 
		} 
  }
}
 

  :

       
    
  
  
     
  
   
  
 

    Bayes, 。

 

package com.cn.bayes;

import java.util.*;

public class Bayes {
	private Hashtable<String, Integer> hashtable_good;
	private Hashtable<String, Integer> hashtabel_bad;
	private Hashtable<String, Double> hashtable_good_p;
	private Hashtable<String, Double> hashtable_bad_p;
	private Hashtable<String, Double> hashtable_probability;

	public Hashtable<String, Double> getHashtable_probability() {
		return hashtable_probability;
	}

	public void setHashtable_probability(
			Hashtable<String, Double> hashtableProbability) {
		hashtable_probability = hashtableProbability;
	}

	public Bayes(Hashtable<String, Integer> hashtable_good,
			Hashtable<String, Integer> hashtabel_bad) {
		this.hashtable_good = hashtable_good;
		this.hashtabel_bad = hashtabel_bad;
		this.hashtable_good_p = this.getGoodOrBadPercent(hashtable_good);
		this.hashtable_bad_p = this.getGoodOrBadPercent(hashtabel_bad);

		Set<String> set_allkeys = this.combineHasetableByKeys(this
				.getHashtable_good(), this.getHashtabel_bad());
//		Set s=hashtable_good_p.entrySet();
//		System.out.println("hashtable_good_p");
//		for (Object o :s.toArray()) {
//			System.out.print("  "+o);
//		}
//		s=hashtable_bad_p.entrySet();
//		System.out.println("hashtable_bad_p");
//		for (Object o :s.toArray()) {
//			System.out.print("  "+o);
//		}
//		
//		for (Object o : set_allkeys.toArray()) {
//			System.out.print("  "+o);
//		}
		this.hashtable_probability = this
				.calcHashtable_probability(set_allkeys);
//		System.out.println();
//        s=hashtable_probability.entrySet();
//		for (Object o : s.toArray()) {
//			System.out.print("  "+o);
//		}
	}

	/*
	 *           hashtable_good_p   hashtable_bad_p
	 */
	@SuppressWarnings("unchecked")
	private Hashtable<String, Double> getGoodOrBadPercent(
			Hashtable hashtable_goodOrBad) {
		Hashtable<String, Double> percent = new Hashtable<String, Double>();
		int total = 0;
		String key;
		Integer value;
		Enumeration enumeration = hashtable_goodOrBad.elements();
		while (enumeration.hasMoreElements()) {
			total = total + (Integer) enumeration.nextElement();
		}
//		System.out.println("total=" + total);

		enumeration = hashtable_goodOrBad.keys();
		while (enumeration.hasMoreElements()) {
			key = (String) enumeration.nextElement();
			value = (Integer) hashtable_goodOrBad.get(key);
//			System.out.println(key + "   " + value);
			percent.put(key, new Double((value + 0.0) / total));
		}
//		 Set s = percent.entrySet();
//		 for (Object o : s.toArray()) {
//		 System.out.println(o);
//		 }
		return percent;
	}

	/*
	 *    hash    key      Set ,Set           
	 * 
	 *   :            hash    key     Set 
	 * 
	 */
	@SuppressWarnings("unchecked")
	private Set<String> combineHasetableByKeys(
			Hashtable<String, Integer> hashtable_good,
			Hashtable<String, Integer> hashtabel_bad) {
		Set<String> allkeysSet = new HashSet();

		Set<String> goodKeysSet = hashtable_good.keySet();
		Set<String> badKeysSet = hashtabel_bad.keySet();
		Iterator it;
		it = goodKeysSet.iterator();
		while (it.hasNext()) {
			allkeysSet.add(it.next().toString());
		}
		it = badKeysSet.iterator();
		while (it.hasNext()) {
			allkeysSet.add(it.next().toString());
		}
		return allkeysSet;
	}

	/*
	 *   Set   key ,    key        ,    hashtable 
	 * 
	 */
	@SuppressWarnings("unchecked")
	private Hashtable<String, Double> calcHashtable_probability(
			Set<String> set_allkeys) {
		Iterator it = set_allkeys.iterator();
		Hashtable<String, Double> hashtable_probability = new Hashtable();
		while (it.hasNext()) {
			String key = it.next().toString();
			Double good_p_value = this.hashtable_good_p.get(key);
			Double bad_p_value = this.hashtable_bad_p.get(key);
			if (null == good_p_value) {
				good_p_value = 0.0;
			}
			if (null == bad_p_value) {
				bad_p_value = 0.0;
			}
			Double result = good_p_value + bad_p_value;
			Double percent=null;
			if (result != 0.0) {
				percent = bad_p_value / result;
			}
			hashtable_probability.put(key, percent);
		}
		return hashtable_probability;
	}

	public Hashtable<String, Integer> getHashtable_good() {
		return hashtable_good;
	}

	public Hashtable<String, Integer> getHashtabel_bad() {
		return hashtabel_bad;
	}
}
 
 
  なコードは ファイルにあります.ご を します.