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

📄 predict.java~9~

📁 垃圾邮件过滤器源代码
💻 JAVA~9~
字号:
///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2001 Chieu Hai Leong and Jason Baldridge
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//////////////////////////////////////////////////////////////////////////////

import opennlp.maxent.*;
import opennlp.maxent.io.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;

/**
 * Test the model on some input.
 *
 * @author  Jason Baldridge
 * @version $Revision: 1.2 $, $Date: 2001/11/20 17:07:17 $
 */
public class Predict {
    MaxentModel _model;
    ContextGenerator _cg = new BasicContextGenerator();

    public Predict (MaxentModel m) {
	_model = m;
    }

    private String eval (String predicates) {
        double[] ocs = _model.eval(_cg.getMailContext(predicates));
	System.out.println("For context: " + predicates
			   + "\n" + _model.getAllOutcomes(ocs) + "\n");

        return _model.getBestOutcome(ocs);
    }

    /**
     * Main method. Call as follows:
     * <p>
     * java Predict dataFile (modelFile)
     */
    public static void main(String[] args) {
	String dataFileName, modelFileName;
	if (args.length > 0) {
	    dataFileName = args[0];   // 要测试的数据文件
	    if (args.length > 1)
		modelFileName = args[1];  // 可以指定或者系统按规则查找要使用的模型文件名
	    else
		modelFileName = dataFileName.substring(0,dataFileName.lastIndexOf('.')) + "Model.txt";
	}
	else {  //没有参数?不太可能执行!!!
	    dataFileName = "";
	    modelFileName = "weatherModel.txt";
	}

	Predict predictor = null;
	try {   // 从模型文件获得要使用的最大熵模型
	    GISModel m =new SuffixSensitiveGISModelReader(new File(modelFileName)).getModel();
	    predictor = new Predict(m);
	} catch (Exception e) {
	    e.printStackTrace();
	    System.exit(0);
	}

	if (dataFileName.equals("")) {  // 没有指明数据文件的情况,一般不会使用
	    predictor.eval("Rainy Happy Humid");
	    predictor.eval("Rainy");
	    predictor.eval("Blarmey");
	}
	else {
	    try {   // 从指定的数据文件生成要测试的DataStream
		DataStream ds = new PlainTextByLineDataStream(new FileReader(new File(args[0])));
                File outputFile = new File("result.txt");   // 测试结果文件
                BufferedWriter output = new BufferedWriter(new FileWriter(outputFile));

		while (ds.hasNext()) {  // 循环处理所有事件
                  String s = (String) ds.nextToken();
                  String result = predictor.eval(s.substring(0, s.lastIndexOf(' '))); // 取得事件的特征,交给模型进行测试
                  output.write(result);
                  output.newLine();
		}
                output.close();
                System.out.print("Congratulations! Test process has finished!");
		return;
	    }
	    catch (Exception e) {
		System.out.println("Unable to read from specified file: " + args[0]);
		System.out.println();
	    }
	}
    }

    public Predict() {
      try {
        jbInit();
      }
      catch(Exception e) {
        e.printStackTrace();
      }
    }

    private void jbInit() throws Exception {
    }
}

⌨️ 快捷键说明

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