org.maltparserx.parser.transition.TransitionTable Maven / Gradle / Ivy
package org.maltparserx.parser.transition;
import java.util.SortedMap;
import java.util.TreeMap;
import org.maltparserx.core.exception.MaltChainedException;
import org.maltparserx.core.helper.HashMap;
import org.maltparserx.core.symbol.Table;
import org.maltparserx.parser.history.container.DecisionPropertyTable;
/**
*
* @author Johan Hall
* @since 1.1
**/
public class TransitionTable implements Table, DecisionPropertyTable {
private String name;
private final SortedMap code2transitionMap;
private final HashMap symbol2transitionMap;
private final HashMap childrenTables;
public TransitionTable(String tableName) {
setName(name);
code2transitionMap = new TreeMap();
symbol2transitionMap = new HashMap();
childrenTables = new HashMap();
}
public void addTransition(int code, String symbol, boolean labeled, TransitionTable childrenTable) {
final Transition transition = new Transition(code, symbol, labeled);
code2transitionMap.put(code,transition);
symbol2transitionMap.put(symbol, transition);
if (childrenTable != null) {
childrenTables.put(transition, childrenTable);
}
}
public boolean continueWithNextDecision(int code) throws MaltChainedException {
if (code2transitionMap.containsKey(code)) {
return code2transitionMap.get(code).isLabeled();
}
return true;
}
public boolean continueWithNextDecision(String symbol) throws MaltChainedException {
if (symbol2transitionMap.containsKey(symbol)) {
return symbol2transitionMap.get(symbol).isLabeled();
}
return true;
}
public Table getTableForNextDecision(int code) throws MaltChainedException {
if (code2transitionMap.containsKey(code)) {
return childrenTables.get(code2transitionMap.get(code));
}
return null;
}
public Table getTableForNextDecision(String symbol) throws MaltChainedException {
if (symbol2transitionMap.containsKey(symbol)) {
return childrenTables.get(symbol2transitionMap.get(symbol));
}
return null;
}
public Transition getTransition(String symbol) {
return symbol2transitionMap.get(symbol);
}
public Transition getTransition(int code) {
return code2transitionMap.get(code);
}
public int addSymbol(String symbol) throws MaltChainedException {
return -1;
}
public String getName() {
return name;
}
public String getSymbolCodeToString(int code) throws MaltChainedException {
if (code < 0) {
return null;
}
return code2transitionMap.get(code).getSymbol();
}
public int getSymbolStringToCode(String symbol) throws MaltChainedException {
if (symbol == null) {
return -1;
}
return symbol2transitionMap.get(symbol).getCode();
}
protected void setName(String name) {
this.name = name;
}
public int size() {
return code2transitionMap.size();
}
}