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