⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 targettext.java

📁 垃圾邮件过滤器Java源码 本软件基于朴素贝叶斯算法
💻 JAVA
字号:
/*
 * TargetText.java
 *
 * Created on 2008年12月2日, 下午12:06
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package newpackage;

import java.io.*;
import java.util.*;
/**
 *
 * @author Administrator
 */
public class TargetText {
  Dictionary newDict;
  float []NB=new float[2];
  int []NBE=new int[2];

  public TargetText() {
  }
  public void init(Dictionary dict){
    newDict=dict;
    for(int i=0;i<2;i++){
      NB[i]=1;
      NBE[i]=0;
    }
  }

  public String categorize(Sample []v,int n,String filename){
  	
  	String str="";
    float p=0;
    String q;
    
    for(int i=0;i<n;i++){
      fileSegment(filename,v[i].wordTable,i);
    }
    
    p=NB[0]/(NB[0]+NB[1]);
    
    q=100*p+"";
    if(q.length()<5){
    	q=q+"     ";
    }
    
    str=q.substring(0,5);
    
    
    int temp=NBE[0];
    int j=0;
    for(int i=1;i<2;i++){
      if(temp>NBE[i]){
        temp=NBE[i];
        j=i;
      }
    }
    for(int i=1;i<2;i++){
      NB[i]=NBE[i]=1;
    }

    if(j==0){
     // System.out.println("This text belongs to 垃圾邮件.txt   垃圾率为:"+str);
   //   return new String("垃圾  垃圾率:"+str);
        return new String("垃圾");
    }else if(j==1){
    //  System.out.println("This text belongs to 合法邮件 垃圾率为:"+str);
     // return new String("合法 垃圾率:"+str);
        return new String("合法");
    }
    /*
    else if(j==2){
      System.out.println("This text belongs to history");
      return new String("history");
    }
    */
    else{
      return null;
    }

    //System.out.println(NB[0]+":"+NBE[0]+" "+NB[1]+":"+NBE[1]+" "+NB[2]+":"+NBE[2]);
  }

  public int wordSegment(String Sentence,HashMap hm,int n) {
    int senLen = Sentence.length();
    int i = 0, j = 0;
    int M = 12;
    String word;
    boolean bFind = false;

      while (i < senLen) {
        int N = i + M < senLen ? i + M : senLen + 1;
        bFind = false;
        for (j = N - 1; j > i; j--) {
          word = Sentence.substring(i, j);
          if (newDict.Find(word)) {
            if (j > i + 1) {
              if (hm.containsKey(word)) {
                NB[n]=NB[n]*((Float)hm.get(word)).floatValue();  //计算每一个类别的概率
                while(NB[n]<1){
                  NBE[n]=NBE[n]+1;
                  NB[n]=NB[n]*10;
                }
              }
            }
            bFind = true;
            i = j;
            break;
          }
        }
        if (bFind == false) {
          i = j + 1;
        }
      }
    return 1;
  }


  public void fileSegment(String fileName,HashMap hm,int n) { //按行读入
    try {
      BufferedReader in = new BufferedReader(
          new FileReader(fileName));
      String s;
      while ((s = in.readLine()) != null) {
        wordSegment(s,hm,n);
      }
    }
    catch (IOException e) {
      System.out.println(e);
    }
  }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -