001 package org.maltparser.core.feature.map; 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.function.FeatureMapFunction; 007 import org.maltparser.core.feature.value.FeatureValue; 008 import org.maltparser.core.feature.value.MultipleFeatureValue; 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 /** 015 * 016 * 017 * @author Johan Hall 018 */ 019 public class PrefixFeature implements FeatureMapFunction { 020 protected FeatureFunction parentFeature; 021 protected MultipleFeatureValue multipleFeatureValue; 022 protected SymbolTableHandler tableHandler; 023 protected SymbolTable table; 024 protected DataFormatInstance dataFormatInstance; 025 protected ColumnDescription column; 026 protected int prefixLength; 027 028 public PrefixFeature(DataFormatInstance dataFormatInstance) throws MaltChainedException { 029 super(); 030 setDataFormatInstance(dataFormatInstance); 031 multipleFeatureValue = new MultipleFeatureValue(this); 032 } 033 034 public void initialize(Object[] arguments) throws MaltChainedException { 035 if (arguments.length != 2) { 036 throw new FeatureException("Could not initialize PrefixFeature: number of arguments are not correct. "); 037 } 038 if (!(arguments[0] instanceof FeatureFunction)) { 039 throw new FeatureException("Could not initialize PrefixFeature: the first argument is not a feature. "); 040 } 041 if (!(arguments[1] instanceof Integer)) { 042 throw new FeatureException("Could not initialize PrefixFeature: the second argument is not a string. "); 043 } 044 setParentFeature((FeatureFunction)arguments[0]); 045 setPrefixLength(((Integer)arguments[1]).intValue()); 046 ColumnDescription parentColumn = dataFormatInstance.getColumnDescriptionByName(parentFeature.getSymbolTable().getName()); 047 if (parentColumn.getType() != ColumnDescription.STRING) { 048 throw new FeatureException("Could not initialize PrefixFeature: the first argument must be a string. "); 049 } 050 setColumn(dataFormatInstance.addInternalColumnDescription("PREFIX_"+prefixLength+"_"+parentFeature.getSymbolTable().getName(), parentColumn)); 051 setSymbolTable(column.getSymbolTable()); 052 // setSymbolTable(tableHandler.addSymbolTable("PREFIX_"+prefixLength+"_"+parentFeature.getSymbolTable().getName(), parentFeature.getSymbolTable())); 053 } 054 055 public Class<?>[] getParameterTypes() { 056 Class<?>[] paramTypes = { org.maltparser.core.syntaxgraph.feature.InputColumnFeature.class, java.lang.Integer.class }; 057 return paramTypes; 058 } 059 060 public FeatureValue getFeatureValue() { 061 return multipleFeatureValue; 062 } 063 064 public int getCode(String symbol) throws MaltChainedException { 065 return table.getSymbolStringToCode(symbol); 066 } 067 068 public String getSymbol(int code) throws MaltChainedException { 069 return table.getSymbolCodeToString(code); 070 } 071 072 public void update() throws MaltChainedException { 073 parentFeature.update(); 074 FeatureValue value = parentFeature.getFeatureValue(); 075 if (value instanceof SingleFeatureValue) { 076 String symbol = ((SingleFeatureValue)value).getSymbol(); 077 if (((FeatureValue)value).isNullValue()) { 078 multipleFeatureValue.addFeatureValue(parentFeature.getSymbolTable().getSymbolStringToCode(symbol), symbol); 079 multipleFeatureValue.setNullValue(true); 080 } else { 081 String prefixStr; 082 if (symbol.length()-prefixLength > 0) { 083 prefixStr = symbol.substring(0, prefixLength); 084 } else { 085 prefixStr = symbol; 086 } 087 int code = table.addSymbol(prefixStr); 088 multipleFeatureValue.addFeatureValue(code, prefixStr); 089 multipleFeatureValue.setNullValue(false); 090 } 091 } else if (value instanceof MultipleFeatureValue) { 092 multipleFeatureValue.reset(); 093 if (((MultipleFeatureValue)value).isNullValue()) { 094 multipleFeatureValue.addFeatureValue(parentFeature.getSymbolTable().getSymbolStringToCode(((MultipleFeatureValue)value).getFirstSymbol()), ((MultipleFeatureValue)value).getFirstSymbol()); 095 multipleFeatureValue.setNullValue(true); 096 } else { 097 for (String symbol : ((MultipleFeatureValue)value).getSymbols()) { 098 String prefixStr; 099 if (symbol.length()-prefixLength > 0) { 100 prefixStr = symbol.substring(0, prefixLength); 101 } else { 102 prefixStr = symbol; 103 } 104 int code = table.addSymbol(prefixStr); 105 multipleFeatureValue.addFeatureValue(code, prefixStr); 106 multipleFeatureValue.setNullValue(true); 107 } 108 } 109 } 110 } 111 112 public void updateCardinality() throws MaltChainedException { 113 // parentFeature.updateCardinality(); 114 // multipleFeatureValue.setCardinality(table.getValueCounter()); 115 } 116 117 public FeatureFunction getParentFeature() { 118 return parentFeature; 119 } 120 121 public void setParentFeature(FeatureFunction feature) { 122 this.parentFeature = feature; 123 } 124 125 public int getPrefixLength() { 126 return prefixLength; 127 } 128 129 public void setPrefixLength(int prefixLength) { 130 this.prefixLength = prefixLength; 131 } 132 133 public SymbolTableHandler getTableHandler() { 134 return dataFormatInstance.getSymbolTables(); 135 } 136 137 public SymbolTable getSymbolTable() { 138 return table; 139 } 140 141 public void setSymbolTable(SymbolTable table) { 142 this.table = table; 143 } 144 145 public DataFormatInstance getDataFormatInstance() { 146 return dataFormatInstance; 147 } 148 149 public void setDataFormatInstance(DataFormatInstance dataFormatInstance) { 150 this.dataFormatInstance = dataFormatInstance; 151 } 152 153 public ColumnDescription getColumn() { 154 return column; 155 } 156 157 protected void setColumn(ColumnDescription column) { 158 this.column = column; 159 } 160 161 public boolean equals(Object obj) { 162 if (this == obj) 163 return true; 164 if (obj == null) 165 return false; 166 if (getClass() != obj.getClass()) 167 return false; 168 return obj.toString().equals(this.toString()); 169 } 170 171 public String toString() { 172 final StringBuilder sb = new StringBuilder(); 173 sb.append("Prefix("); 174 sb.append(parentFeature.toString()); 175 sb.append(", "); 176 sb.append(prefixLength); 177 sb.append(')'); 178 return sb.toString(); 179 } 180 } 181