001 package org.maltparser.parser.history.container; 002 003 import org.maltparser.core.exception.MaltChainedException; 004 import org.maltparser.core.symbol.Table; 005 /** 006 * 007 * @author Johan Hall 008 **/ 009 public class TableContainer { 010 public enum RelationToNextDecision { COMBINED, SEQUANTIAL, BRANCHED, SWITCHED, NONE } 011 protected int cachedCode; 012 protected final StringBuilder cachedSymbol; 013 protected Table table; 014 protected String name; 015 protected RelationToNextDecision relationToNextDecision; 016 017 public TableContainer(Table table, String name, char decisionSeparator) { 018 setTable(table); 019 setName(name); 020 setRelationToNextDecision(decisionSeparator); 021 cachedSymbol = new StringBuilder(); 022 cachedCode = -1; 023 } 024 025 public void clearCache() { 026 cachedCode = -1; 027 cachedSymbol.setLength(0); 028 } 029 030 public String getSymbol(int code) throws MaltChainedException { 031 if (code < 0 && !containCode(code)) { 032 clearCache(); 033 return null; 034 } 035 if (cachedCode != code) { 036 clearCache(); 037 cachedCode = code; 038 cachedSymbol.append(table.getSymbolCodeToString(cachedCode)); 039 } 040 return cachedSymbol.toString(); 041 } 042 043 public int getCode(String symbol) throws MaltChainedException { 044 if (cachedSymbol == null || !cachedSymbol.equals(symbol)) { 045 clearCache(); 046 cachedSymbol.append(symbol); 047 cachedCode = table.getSymbolStringToCode(symbol); 048 } 049 return cachedCode; 050 } 051 052 public boolean containCode(int code) throws MaltChainedException { 053 if (cachedCode != code) { 054 clearCache(); 055 cachedSymbol.append(table.getSymbolCodeToString(code)); 056 if (cachedSymbol == null) { 057 return false; 058 } 059 cachedCode = code; 060 } 061 return true; 062 } 063 064 public boolean containSymbol(String symbol) throws MaltChainedException { 065 if (cachedSymbol == null || !cachedSymbol.equals(symbol)) { 066 clearCache(); 067 cachedCode = table.getSymbolStringToCode(symbol); 068 if (cachedCode < 0) { 069 return false; 070 } 071 cachedSymbol.append(symbol); 072 } 073 return true; 074 } 075 076 public boolean continueWithNextDecision(int code) throws MaltChainedException { 077 if (table instanceof DecisionPropertyTable) { 078 return ((DecisionPropertyTable)table).continueWithNextDecision(code); 079 } 080 return true; 081 } 082 083 public boolean continueWithNextDecision(String symbol) throws MaltChainedException { 084 if (table instanceof DecisionPropertyTable) { 085 return ((DecisionPropertyTable)table).continueWithNextDecision(symbol); 086 } 087 return true; 088 } 089 090 public Table getTable() { 091 return table; 092 } 093 094 public String getTableName() { 095 return table != null?table.getName():null; 096 } 097 098 public String getTableContainerName() { 099 return name; 100 } 101 102 public RelationToNextDecision getRelationToNextDecision() { 103 return relationToNextDecision; 104 } 105 106 protected void setRelationToNextDecision(char decisionSeparator) { 107 switch (decisionSeparator) { 108 case '+': 109 this.relationToNextDecision = RelationToNextDecision.COMBINED; 110 break; 111 case ',': 112 this.relationToNextDecision = RelationToNextDecision.SEQUANTIAL; 113 break; 114 case ';': 115 this.relationToNextDecision = RelationToNextDecision.BRANCHED; 116 break; 117 case '#': 118 this.relationToNextDecision = RelationToNextDecision.BRANCHED; 119 break; 120 case '?': 121 this.relationToNextDecision = RelationToNextDecision.SWITCHED; 122 break; 123 default: 124 this.relationToNextDecision = RelationToNextDecision.NONE; 125 } 126 } 127 128 protected void setTable(Table table) { 129 this.table = table; 130 } 131 132 protected void setName(String name) { 133 this.name = name; 134 } 135 136 public int size() { 137 return table.size(); 138 } 139 140 public String toString() { 141 StringBuilder sb = new StringBuilder(); 142 sb.append(name); 143 sb.append(" -> " ); 144 sb.append(cachedSymbol); 145 sb.append(" = "); 146 sb.append(cachedCode); 147 return sb.toString(); 148 } 149 }