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

📄 gismodelwriter.java~3~

📁 垃圾邮件过滤器源代码
💻 JAVA~3~
字号:
///////////////////////////////////////////////////////////////////////////////// Copyright (C) 2001 Jason Baldridge and Gann Bierner//// 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 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.//////////////////////////////////////////////////////////////////////////////package opennlp.maxent.io;import opennlp.maxent.*;import gnu.trove.*;import java.io.*;import java.util.*;/** * Abstract parent class for GISModel writers.  It provides the persist method * which takes care of the structure of a stored document, and requires an * extending class to define precisely how the data should be stored. * 输出模型的参数的抽象类.提供了一个persist方法来处理文档的结果,需要一个子类来定义数据应该如何存储 * @author      Jason Baldridge * @version     $Revision: 1.5 $, $Date: 2004/06/11 20:51:36 $ */public abstract class GISModelWriter {    protected TIntParamHashMap[] PARAMS;    protected String[] OUTCOME_LABELS;    protected double CORRECTION_CONSTANT;    protected double CORRECTION_PARAM;    protected String[] PRED_LABELS;    public GISModelWriter (GISModel model) {    	Object[] data = model.getDataStructures();	PARAMS = (TIntParamHashMap[])data[0];	TObjectIntHashMap pmap = (TObjectIntHashMap)data[1];	OUTCOME_LABELS = (String[])data[2];	CORRECTION_CONSTANT = ((Integer)data[3]).intValue();	CORRECTION_PARAM = ((Double)data[4]).doubleValue();	PRED_LABELS = new String[pmap.size()];	pmap.forEachEntry(new TObjectIntProcedure() {	    public boolean execute (Object pred, int index) {		PRED_LABELS[index] = (String)pred;		return true;	    }	});    }    protected abstract void writeUTF (String s) throws java.io.IOException;    protected abstract void writeInt (int i) throws java.io.IOException;    protected abstract void writeDouble (double d) throws java.io.IOException;    protected abstract void close () throws java.io.IOException;    /**     * Writes the model to disk, using the <code>writeX()</code> methods     * provided by extending classes.     *     * <p>If you wish to create a GISModelWriter which uses a different     * structure, it will be necessary to override the persist method in     * addition to implementing the <code>writeX()</code> methods.     */    public void persist() throws IOException {	// the type of model (GIS) GIS模型	writeUTF("GIS");        // the value of the correction constant  修正常量的值	//writeInt(CORRECTION_CONSTANT);        writeDouble(CORRECTION_CONSTANT);	// the value of the correction constant 修正常量对应的参数值	writeDouble(CORRECTION_PARAM);	// the mapping from outcomes to their integer indexes 从输出结果到整数索引的映射        // 输出结果的个数	writeInt(OUTCOME_LABELS.length);        // 每个可能的输出的字符串形式        for (int i=0; i<OUTCOME_LABELS.length; i++)	    writeUTF(OUTCOME_LABELS[i]);	// the mapping from predicates to the outcomes they contributed to.	// The sorting is done so that we actually can write this out more	// compactly than as the entire list.        // 从断言到对应的输出的映射关系,排序是为了更简洁地进行输出	ComparablePredicate[] sorted = sortValues();	List compressed = compressOutcomes(sorted);        // 断言与输出映射关系的个数,下面的共compressed.size()行是其详细信息	writeInt(compressed.size());        // 详细输出断言与输出的对应关系:第一个数字表示断言的个数        // 后面的表示输出的组合,即有多少个断言对应此种输出组合	for (int i=0; i<compressed.size(); i++) {	    List a = (List)compressed.get(i);	    writeUTF(a.size()		     + ((ComparablePredicate)a.get(0)).toString());	}        // 以上输出的几行中第一个数字之和应该等于断言的个数	// the mapping from predicate names to their integer indexes        // 断言到整数索引值的映射,断言的个数	writeInt(PARAMS.length);        String pred;  // ?根本没使用???        // 输出每个断言的字符串形式	for (int i=0; i<sorted.length; i++)	    writeUTF(sorted[i].name);	// write out the parameters 输出模型的参数	for (int i=0; i<sorted.length; i++)	    for (int j=0; j<sorted[i].params.length; j++)		writeDouble(sorted[i].params[j]);	close();    }    protected ComparablePredicate[] sortValues () {        ComparablePredicate[] sortPreds =            new ComparablePredicate[PARAMS.length];        int numParams = 0;        for (int pid=0; pid<PARAMS.length; pid++) {            int[] predkeys = PARAMS[pid].keys();            Arrays.sort(predkeys);            int numActive = predkeys.length;            numParams += numActive;            int[] activeOCs = new int[numActive];            double[] activeParams = new double[numActive];            int id = 0;            for (int i=0; i < predkeys.length; i++) {                int oid = predkeys[i];                activeOCs[id] = oid;                activeParams[id] = PARAMS[pid].get(oid);                id++;            }            sortPreds[pid] = new ComparablePredicate(PRED_LABELS[pid],                                                     activeOCs,                                                     activeParams);        }        Arrays.sort(sortPreds);        return sortPreds;    }    protected List compressOutcomes (ComparablePredicate[] sorted) {	ComparablePredicate cp = sorted[0];	List outcomePatterns = new ArrayList();	List newGroup = new ArrayList();	for (int i=0; i<sorted.length; i++) {	    if (cp.compareTo(sorted[i]) == 0) {		newGroup.add(sorted[i]);	    } else {		cp = sorted[i];		outcomePatterns.add(newGroup);		newGroup = new ArrayList();		newGroup.add(sorted[i]);	    }	}	outcomePatterns.add(newGroup);	return outcomePatterns;    }}

⌨️ 快捷键说明

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