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