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