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.AddressFunction; 006import org.maltparser.core.feature.function.FeatureFunction; 007import org.maltparser.core.feature.value.AddressValue; 008import org.maltparser.core.feature.value.FeatureValue; 009import org.maltparser.core.feature.value.SingleFeatureValue; 010import org.maltparser.core.io.dataformat.ColumnDescription; 011import org.maltparser.core.io.dataformat.DataFormatInstance; 012import org.maltparser.core.symbol.SymbolTable; 013import org.maltparser.core.symbol.SymbolTableHandler; 014import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId; 015import org.maltparser.core.syntaxgraph.node.DependencyNode; 016/** 017* 018* @author Johan Hall 019**/ 020public final class InputArcDirFeature implements FeatureFunction { 021 public final static Class<?>[] paramTypes = { java.lang.String.class, org.maltparser.core.feature.function.AddressFunction.class }; 022 private ColumnDescription column; 023 private final DataFormatInstance dataFormatInstance; 024 private final SymbolTableHandler tableHandler; 025 private SymbolTable table; 026 private final SingleFeatureValue featureValue; 027 private AddressFunction addressFunction; 028 029 public InputArcDirFeature(DataFormatInstance dataFormatInstance, SymbolTableHandler tableHandler) throws MaltChainedException { 030 this.dataFormatInstance = dataFormatInstance; 031 this.tableHandler = tableHandler; 032 this.featureValue = new SingleFeatureValue(this); 033 } 034 035 public void initialize(Object[] arguments) throws MaltChainedException { 036 if (arguments.length != 2) { 037 throw new FeatureException("Could not initialize InputArcDirFeature: number of arguments are not correct. "); 038 } 039 if (!(arguments[0] instanceof String)) { 040 throw new FeatureException("Could not initialize InputArcDirFeature: the first argument is not a string. "); 041 } 042 if (!(arguments[1] instanceof AddressFunction)) { 043 throw new FeatureException("Could not initialize InputArcDirFeature: the second argument is not an address function. "); 044 } 045 setColumn(dataFormatInstance.getColumnDescriptionByName((String)arguments[0])); 046 setSymbolTable(tableHandler.addSymbolTable("ARCDIR_"+column.getName(),ColumnDescription.INPUT, ColumnDescription.STRING, "one")); 047 table.addSymbol("LEFT"); 048 table.addSymbol("RIGHT"); 049 table.addSymbol("ROOT"); 050 setAddressFunction((AddressFunction)arguments[1]); 051 } 052 053 public Class<?>[] getParameterTypes() { 054 return paramTypes; 055 } 056 057 public int getCode(String symbol) throws MaltChainedException { 058 return table.getSymbolStringToCode(symbol); 059 } 060 061 public String getSymbol(int code) throws MaltChainedException { 062 return table.getSymbolCodeToString(code); 063 } 064 065 public FeatureValue getFeatureValue() { 066 return featureValue; 067 } 068 069 public void update() throws MaltChainedException { 070 AddressValue a = addressFunction.getAddressValue(); 071 if (a.getAddress() != null && a.getAddressClass() == org.maltparser.core.syntaxgraph.node.DependencyNode.class) { 072 DependencyNode node = (DependencyNode)a.getAddress(); 073 try { 074 int index = Integer.parseInt(node.getLabelSymbol(tableHandler.getSymbolTable(column.getName()))); 075 if (node.isRoot()) { 076 featureValue.setIndexCode(table.getNullValueCode(NullValueId.ROOT_NODE)); 077 featureValue.setSymbol(table.getNullValueSymbol(NullValueId.ROOT_NODE)); 078 featureValue.setNullValue(true); 079 } else if (index == 0) { 080 featureValue.setIndexCode(table.getSymbolStringToCode("ROOT")); 081 featureValue.setSymbol("ROOT"); 082 featureValue.setNullValue(false); 083 } else if (index < node.getIndex()) { 084 featureValue.setIndexCode(table.getSymbolStringToCode("LEFT")); 085 featureValue.setSymbol("LEFT"); 086 featureValue.setNullValue(false); 087 } else if (index > node.getIndex()) { 088 featureValue.setIndexCode(table.getSymbolStringToCode("RIGHT")); 089 featureValue.setSymbol("RIGHT"); 090 featureValue.setNullValue(false); 091 } 092 } catch (NumberFormatException e) { 093 throw new FeatureException("The index of the feature must be an integer value. ", e); 094 } 095 } else { 096 featureValue.setIndexCode(table.getNullValueCode(NullValueId.NO_NODE)); 097 featureValue.setSymbol(table.getNullValueSymbol(NullValueId.NO_NODE)); 098 featureValue.setNullValue(true); 099 } 100 featureValue.setValue(1); 101 } 102 103 public AddressFunction getAddressFunction() { 104 return addressFunction; 105 } 106 107 public void setAddressFunction(AddressFunction addressFunction) { 108 this.addressFunction = addressFunction; 109 } 110 111 public ColumnDescription getColumn() { 112 return column; 113 } 114 115 public void setColumn(ColumnDescription column) throws MaltChainedException { 116 if (column.getType() != ColumnDescription.INTEGER) { 117 throw new FeatureException("InputArc feature column must be of type integer. "); 118 } 119 this.column = column; 120 } 121 122 public DataFormatInstance getDataFormatInstance() { 123 return dataFormatInstance; 124 } 125 126 public SymbolTable getSymbolTable() { 127 return table; 128 } 129 130 public void setSymbolTable(SymbolTable table) { 131 this.table = table; 132 } 133 134 public SymbolTableHandler getTableHandler() { 135 return tableHandler; 136 } 137 138 public int getType() { 139 return ColumnDescription.STRING; 140 } 141 142 public String getMapIdentifier() { 143 return getSymbolTable().getName(); 144 } 145 146 public boolean equals(Object obj) { 147 if (!(obj instanceof InputArcDirFeature)) { 148 return false; 149 } 150 if (!obj.toString().equals(this.toString())) { 151 return false; 152 } 153 return true; 154 } 155 156 public String toString() { 157 return "InputArcDir(" + column.getName() + ", " + addressFunction.toString() + ")"; 158 } 159}