📄 assignment.java
字号:
/* Copyright (C) 2003 Univ. of Massachusetts Amherst, Computer Science Dept. This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit). http://www.cs.umass.edu/~mccallum/mallet This software is provided under the terms of the Common Public License, version 1.0, as published by http://www.opensource.org. For further information, see the file `LICENSE' included with this distribution. */package edu.umass.cs.mallet.grmm;import edu.umass.cs.mallet.base.types.Alphabet;import gnu.trove.TIntArrayList;import java.util.ArrayList;/** * An assignment to a bunch of variables. * * Note that outcomes are always integers. If you * want them to be something else, then the Variables * all have outcome Alphabets; for example, see * {@link Variable#lookupOutcome}. * * Created: Tue Oct 21 15:11:11 2003 * * @author <a href="mailto:casutton@cs.umass.edu">Charles Sutton</a> * @version $Id: Assignment.java,v 1.1 2004/07/15 17:53:31 casutton Exp $ */public class Assignment { /* Maps from vars => indicies */ Alphabet varMap; /* Maps from indices => outcomes. */ TIntArrayList values; /** Creates an empty assignment. */ public Assignment() { varMap = new Alphabet (Variable.class); values = new TIntArrayList (); } public Assignment (Variable var, int outcome) { this(); setValue (var, outcome); } /** Creates an assignemnt for the given variables. */ public Assignment (Variable[] vars, int[] outcomes) { varMap = new Alphabet (vars.length, Variable.class); values = new TIntArrayList (vars.length); setValues (vars, outcomes); } /** Sets one variable in the assignment. */ public void setValue (Variable var, int outcome) { int vidx = varMap.lookupIndex (var); if (vidx < values.size()) { values.set (vidx, outcome); } else { values.insert (vidx, outcome); } } /** Add lots of variables to the assignment. */ public void setValues (Variable[] vars, int[] outcomes) { for (int i = 0; i < vars.length; i++) { setValue (vars [i], outcomes [i]); } } /** Returns the value of var in this assigment. */ public int get (Variable var) { int idx = varMap.lookupIndex (var, false); if (idx == -1) throw new IndexOutOfBoundsException ("Assignment does not give a value for variable "+var); return values.get (varMap.lookupIndex (var, false)); } public Object getObject (Variable var) { return var.lookupOutcome (get (var)); } public Variable getVariable (int i) { return (Variable) varMap.lookupObject (i); } public int size() { return values.size(); } public void dump () { for (int i = 0; i < varMap.size(); i++) { Variable var = (Variable) varMap.lookupObject (i); Object obj = getObject (var); System.out.println(var+" "+obj); } } public void dumpNumeric () { for (int i = 0; i < varMap.size(); i++) { Variable var = (Variable) varMap.lookupObject (i); int outcome = get (var); System.out.println(var+" "+outcome); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -