edu.stanford.nlp.trees.tregex.VariableStrings Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stanford-parser Show documentation
Show all versions of stanford-parser Show documentation
Stanford Parser processes raw text in English, Chinese, German, Arabic, and French, and extracts constituency parse trees.
package edu.stanford.nlp.trees.tregex;
import edu.stanford.nlp.stats.IntCounter;
import edu.stanford.nlp.util.ArrayMap;
import edu.stanford.nlp.util.MapFactory;
import edu.stanford.nlp.util.MutableInteger;
import java.util.Map;
/** A class that takes care of the stuff necessary for variable strings.
*
* @author Roger Levy ([email protected])
*/
class VariableStrings {
private final Map varsToStrings;
private final IntCounter numVarsSet;
public VariableStrings() {
varsToStrings = ArrayMap.newArrayMap();
numVarsSet = new IntCounter<>(MapFactory.arrayMapFactory());
}
public void reset() {
numVarsSet.clear();
varsToStrings.clear();
}
public boolean isSet(String o) {
return numVarsSet.getCount(o) >= 1;
}
public void setVar(String var, String string) {
String oldString = varsToStrings.put(var,string);
if(oldString != null && ! oldString.equals(string))
throw new RuntimeException("Error -- can't setVar to a different string -- old: " + oldString + " new: " + string);
numVarsSet.incrementCount(var);
}
public void unsetVar(String var) {
if(numVarsSet.getCount(var) > 0)
numVarsSet.decrementCount(var);
if(numVarsSet.getCount(var)==0)
varsToStrings.put(var,null);
}
public String getString(String var) {
return varsToStrings.get(var);
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append("{");
boolean appended = false;
for (String key : varsToStrings.keySet()) {
if (appended) {
s.append(",");
} else {
appended = true;
}
s.append(key);
s.append("=(");
s.append(varsToStrings.get(key));
s.append(":");
s.append(numVarsSet.getCount(key));
s.append(")");
}
s.append("}");
return s.toString();
}
}