001package org.maltparser.core.syntaxgraph.feature; 002 003import org.maltparser.core.exception.MaltChainedException; 004import org.maltparser.core.feature.FeatureException; 005import org.maltparser.core.feature.function.FeatureFunction; 006import org.maltparser.core.feature.function.Modifiable; 007import org.maltparser.core.feature.value.FeatureValue; 008import org.maltparser.core.feature.value.SingleFeatureValue; 009import org.maltparser.core.io.dataformat.ColumnDescription; 010import org.maltparser.core.symbol.SymbolTable; 011import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId; 012 013/** 014* 015* 016* @author Johan Hall 017*/ 018public abstract class ColumnFeature implements FeatureFunction, Modifiable { 019 protected ColumnDescription column; 020 protected SymbolTable symbolTable; 021 protected final SingleFeatureValue featureValue; 022 023 public ColumnFeature() throws MaltChainedException { 024 this.featureValue = new SingleFeatureValue(this); 025 } 026 027 public abstract void update() throws MaltChainedException; 028 public abstract void initialize(Object[] arguments) throws MaltChainedException; 029 public abstract Class<?>[] getParameterTypes(); 030 031 public String getSymbol(int value) throws MaltChainedException { 032 return symbolTable.getSymbolCodeToString(value); 033 } 034 035 public int getCode(String value) throws MaltChainedException { 036 return symbolTable.getSymbolStringToCode(value); 037 } 038 039 public ColumnDescription getColumn() { 040 return column; 041 } 042 043 protected void setColumn(ColumnDescription column) { 044 this.column = column; 045 } 046 047 public SymbolTable getSymbolTable() { 048 return symbolTable; 049 } 050 051 protected void setSymbolTable(SymbolTable symbolTable) { 052 this.symbolTable = symbolTable; 053 } 054 055 public void setFeatureValue(int indexCode) throws MaltChainedException { 056 final String symbol = symbolTable.getSymbolCodeToString(indexCode); 057 058 if (symbol == null) { 059 featureValue.update(indexCode, symbolTable.getNullValueSymbol(NullValueId.NO_NODE), true, 1); 060 } else { 061 boolean nullValue = symbolTable.isNullValue(indexCode); 062 if (column.getType() == ColumnDescription.STRING || nullValue) { 063 featureValue.update(indexCode, symbol, nullValue, 1); 064 } else { 065 castFeatureValue(symbol); 066 } 067 } 068 } 069 070 public void setFeatureValue(String symbol) throws MaltChainedException { 071 final int indexCode = symbolTable.getSymbolStringToCode(symbol); 072 if (indexCode < 0) { 073 featureValue.update(symbolTable.getNullValueCode(NullValueId.NO_NODE), symbol, true, 1); 074 } else { 075 boolean nullValue = symbolTable.isNullValue(symbol); 076 if (column.getType() == ColumnDescription.STRING || nullValue) { 077 featureValue.update(indexCode, symbol, nullValue, 1); 078 } else { 079 castFeatureValue(symbol); 080 } 081 } 082 } 083 084 protected void castFeatureValue(String symbol) throws MaltChainedException { 085 if (column.getType() == ColumnDescription.INTEGER) { 086 try { 087 final int dotIndex = symbol.indexOf('.'); 088 if (dotIndex == -1) { 089 featureValue.setValue(Integer.parseInt(symbol)); 090 featureValue.setSymbol(symbol); 091 } else { 092 featureValue.setValue(Integer.parseInt(symbol.substring(0,dotIndex))); 093 featureValue.setSymbol(symbol.substring(0,dotIndex)); 094 } 095 } catch (NumberFormatException e) { 096 throw new FeatureException("Could not cast the feature value '"+symbol+"' to integer value.", e); 097 } 098 } else if (column.getType() == ColumnDescription.BOOLEAN) { 099 final int dotIndex = symbol.indexOf('.'); 100 if (symbol.equals("1") || symbol.equals("true") || symbol.equals("#true#") || (dotIndex != -1 && symbol.substring(0,dotIndex).equals("1"))) { 101 featureValue.setValue(1); 102 featureValue.setSymbol("true"); 103 } else if (symbol.equals("false") || symbol.equals("0") || (dotIndex != -1 && symbol.substring(0,dotIndex).equals("0"))) { 104 featureValue.setValue(0); 105 featureValue.setSymbol("false"); 106 } else { 107 throw new FeatureException("Could not cast the feature value '"+symbol+"' to boolean value."); 108 } 109 } else if (column.getType() == ColumnDescription.REAL) { 110 try { 111 featureValue.setValue(Double.parseDouble(symbol)); 112 featureValue.setSymbol(symbol); 113 } catch (NumberFormatException e) { 114 throw new FeatureException("Could not cast the feature value '"+symbol+"' to real value.", e); 115 } 116 } 117 if (column.getType() == ColumnDescription.INTEGER || column.getType() == ColumnDescription.BOOLEAN || column.getType() == ColumnDescription.REAL) { 118 featureValue.setNullValue(false); 119 featureValue.setIndexCode(1); 120 } 121 } 122 123 public FeatureValue getFeatureValue() { 124 return featureValue; 125 } 126 127 public boolean equals(Object obj) { 128 if (this == obj) 129 return true; 130 if (obj == null) 131 return false; 132 if (getClass() != obj.getClass()) 133 return false; 134 return obj.toString().equals(this.toString()); 135 } 136 137 public String getColumnName() { 138 return column.getName(); 139 } 140 141 public int getType() { 142 return column.getType(); 143 } 144 145 public String getMapIdentifier() { 146 return getSymbolTable().getName(); 147 } 148 149 public String toString() { 150 return column.getName(); 151 } 152}