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