001package org.maltparser.core.syntaxgraph.feature; 002 003import org.maltparser.core.exception.MaltChainedException; 004import org.maltparser.core.feature.function.AddressFunction; 005import org.maltparser.core.feature.value.AddressValue; 006import org.maltparser.core.io.dataformat.ColumnDescription; 007import org.maltparser.core.io.dataformat.DataFormatInstance; 008import org.maltparser.core.symbol.SymbolTableHandler; 009import org.maltparser.core.symbol.TableFeature; 010import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId; 011import org.maltparser.core.syntaxgraph.SyntaxGraphException; 012import org.maltparser.core.syntaxgraph.node.DependencyNode; 013/** 014* 015* 016* @author Johan Hall 017*/ 018public final class OutputTableFeature extends TableFeature { 019 public final static Class<?>[] paramTypes = { java.lang.String.class, org.maltparser.core.feature.function.AddressFunction.class }; 020 private AddressFunction addressFunction; 021 022 public OutputTableFeature(DataFormatInstance dataFormatInstance, SymbolTableHandler tableHandler) throws MaltChainedException { 023 super(tableHandler); 024 } 025 026 public void initialize(Object[] arguments) throws MaltChainedException { 027 if (arguments.length != 2) { 028 throw new SyntaxGraphException("Could not initialize OutputTableFeature: number of arguments are not correct. "); 029 } 030 if (!(arguments[0] instanceof String)) { 031 throw new SyntaxGraphException("Could not initialize OutputTableFeature: the first argument is not a string. "); 032 } 033 if (!(arguments[1] instanceof AddressFunction)) { 034 throw new SyntaxGraphException("Could not initialize OutputTableFeature: the second argument is not an address function. "); 035 } 036 setSymbolTable(tableHandler.getSymbolTable((String)arguments[0])); 037 setAddressFunction((AddressFunction)arguments[1]); 038 setType(ColumnDescription.STRING); // TODO Probably it could possible to vary the type 039 } 040 041 public Class<?>[] getParameterTypes() { 042 return paramTypes; 043 } 044 045 public void update() throws MaltChainedException { 046 final AddressValue a = addressFunction.getAddressValue(); 047 048 if (a.getAddress() == null) { 049 featureValue.setIndexCode(getSymbolTable().getNullValueCode(NullValueId.NO_NODE)); 050 featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE)); 051 featureValue.setNullValue(true); 052 } else { 053// try { 054// a.getAddressClass().asSubclass(org.maltparser.core.syntaxgraph.node.DependencyNode.class); 055 056 final DependencyNode node = (DependencyNode)a.getAddress(); 057 if (!node.isRoot()) { 058 if (node.hasHead()) { 059 featureValue.setIndexCode(node.getHeadEdge().getLabelCode(getSymbolTable())); 060 featureValue.setSymbol(getSymbolTable().getSymbolCodeToString(node.getHeadEdge().getLabelCode(getSymbolTable()))); 061 featureValue.setNullValue(false); 062 } else { 063 featureValue.setIndexCode(getSymbolTable().getNullValueCode(NullValueId.NO_VALUE)); 064 featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_VALUE)); 065 066 featureValue.setNullValue(true); 067 } 068 } else { 069 featureValue.setIndexCode(getSymbolTable().getNullValueCode(NullValueId.ROOT_NODE)); 070 featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.ROOT_NODE)); 071// featureValue.setKnown(true); 072 featureValue.setNullValue(true); 073 } 074// } catch (ClassCastException e) { 075// featureValue.setCode(getSymbolTable().getNullValueCode(NullValueId.NO_NODE)); 076// featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE)); 077// featureValue.setNullValue(true); 078// } 079 } 080 featureValue.setValue(1); 081 } 082 083 public AddressFunction getAddressFunction() { 084 return addressFunction; 085 } 086 087 public void setAddressFunction(AddressFunction addressFunction) { 088 this.addressFunction = addressFunction; 089 } 090 091 public SymbolTableHandler getTableHandler() { 092 return tableHandler; 093 } 094 095 public boolean equals(Object obj) { 096 if (this == obj) 097 return true; 098 if (obj == null) 099 return false; 100 if (getClass() != obj.getClass()) 101 return false; 102 return obj.toString().equals(toString()); 103 } 104 105 public int hashCode() { 106 return 217 + (null == toString() ? 0 : toString().hashCode()); 107 } 108 109 public String toString() { 110 final StringBuilder sb = new StringBuilder(); 111 sb.append("OutputTable("); 112 sb.append(super.toString()); 113 sb.append(", "); 114 sb.append(addressFunction.toString()); 115 sb.append(")"); 116 return sb.toString(); 117 } 118}