001package org.maltparser.parser.transition; 002 003import java.util.SortedMap; 004import java.util.TreeMap; 005 006import org.maltparser.core.exception.MaltChainedException; 007import org.maltparser.core.helper.HashMap; 008import org.maltparser.core.symbol.Table; 009import org.maltparser.parser.history.container.DecisionPropertyTable; 010/** 011* 012* @author Johan Hall 013**/ 014public class TransitionTable implements Table, DecisionPropertyTable { 015 private final String name; 016 private final SortedMap<Integer,Transition> code2transitionMap; 017 private final HashMap<String,Transition> symbol2transitionMap; 018 private final HashMap<Transition,TransitionTable> childrenTables; 019 020 public TransitionTable(String tableName) { 021 this.name = tableName; 022 this.code2transitionMap = new TreeMap<Integer,Transition>(); 023 this.symbol2transitionMap = new HashMap<String,Transition>(); 024 this.childrenTables = new HashMap<Transition,TransitionTable>(); 025 } 026 027 public void addTransition(int code, String symbol, boolean labeled, TransitionTable childrenTable) { 028 final Transition transition = new Transition(code, symbol, labeled); 029 code2transitionMap.put(code,transition); 030 symbol2transitionMap.put(symbol, transition); 031 childrenTables.put(transition, childrenTable); 032 } 033 034 public boolean continueWithNextDecision(int code) throws MaltChainedException { 035 if (code2transitionMap.containsKey(code)) { 036 return code2transitionMap.get(code).isLabeled(); 037 } 038 return true; 039 } 040 041 public boolean continueWithNextDecision(String symbol) throws MaltChainedException { 042 if (symbol2transitionMap.containsKey(symbol)) { 043 return symbol2transitionMap.get(symbol).isLabeled(); 044 } 045 return true; 046 } 047 048 public Table getTableForNextDecision(int code) throws MaltChainedException { 049 if (code2transitionMap.containsKey(code)) { 050 return childrenTables.get(code2transitionMap.get(code)); 051 } 052 return null; 053 } 054 055 public Table getTableForNextDecision(String symbol) throws MaltChainedException { 056 if (symbol2transitionMap.containsKey(symbol)) { 057 return childrenTables.get(symbol2transitionMap.get(symbol)); 058 } 059 return null; 060 } 061 062 public Transition getTransition(String symbol) { 063 return symbol2transitionMap.get(symbol); 064 } 065 066 public Transition getTransition(int code) { 067 return code2transitionMap.get(code); 068 } 069 070 public int addSymbol(String symbol) throws MaltChainedException { 071 return -1; 072 } 073 074 public String getName() { 075 return name; 076 } 077 078 public String getSymbolCodeToString(int code) throws MaltChainedException { 079 if (code < 0) { 080 return null; 081 } 082 return code2transitionMap.get(code).getSymbol(); 083 } 084 085 public int getSymbolStringToCode(String symbol) throws MaltChainedException { 086 if (symbol == null) { 087 return -1; 088 } 089 return symbol2transitionMap.get(symbol).getCode(); 090 } 091 092 public double getSymbolStringToValue(String symbol) throws MaltChainedException { 093 return 1.0; 094 } 095 096 public int size() { 097 return code2transitionMap.size(); 098 } 099 100}