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