001    package org.maltparser.core.syntaxgraph.feature;
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.Modifiable;
007    import org.maltparser.core.feature.value.FeatureValue;
008    import org.maltparser.core.feature.value.SingleFeatureValue;
009    import org.maltparser.core.io.dataformat.ColumnDescription;
010    import org.maltparser.core.symbol.SymbolTable;
011    import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId;
012    
013    /**
014    *
015    *
016    * @author Johan Hall
017    */
018    public abstract class ColumnFeature implements FeatureFunction, Modifiable {
019            protected ColumnDescription column;
020            protected SingleFeatureValue featureValue;
021            
022            public ColumnFeature() throws MaltChainedException {
023                    featureValue = new SingleFeatureValue(this);
024            }
025            
026            public abstract void update() throws MaltChainedException;
027            public abstract void initialize(Object[] arguments) throws MaltChainedException;
028            public abstract Class<?>[] getParameterTypes();
029            
030            public String getSymbol(int value) throws MaltChainedException {
031                    return column.getSymbolTable().getSymbolCodeToString(value);
032            }
033            
034            public int getCode(String value) throws MaltChainedException {
035                    return column.getSymbolTable().getSymbolStringToCode(value);
036            }
037            
038            public ColumnDescription getColumn() {
039                    return column;
040            }
041            
042            protected void setColumn(ColumnDescription column) {
043                    this.column = column;
044            }
045            
046            public void updateCardinality() {
047    //              featureValue.setCardinality(column.getSymbolTable().getValueCounter()); 
048            }
049            
050            public void setFeatureValue(int indexCode) throws MaltChainedException {
051                    final String symbol = column.getSymbolTable().getSymbolCodeToString(indexCode);
052                    
053                    if (symbol == null) {
054                            featureValue.update(indexCode, column.getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE), true, 1);
055                    } else {
056                            boolean nullValue = column.getSymbolTable().isNullValue(indexCode);
057                            if (column.getType() == ColumnDescription.STRING || nullValue) {
058                                    featureValue.update(indexCode, symbol, nullValue, 1);
059                            } else {
060                                    castFeatureValue(symbol);
061                            }
062                    }
063            }
064            
065            public void setFeatureValue(String symbol) throws MaltChainedException {
066                    int indexCode = column.getSymbolTable().getSymbolStringToCode(symbol);
067                    if (indexCode < 0) {
068                            featureValue.update(column.getSymbolTable().getNullValueCode(NullValueId.NO_NODE), symbol, true, 1);
069                    } else {
070                            boolean nullValue = column.getSymbolTable().isNullValue(symbol);
071                            if (column.getType() == ColumnDescription.STRING || nullValue) {
072                                    featureValue.update(indexCode, symbol, nullValue, 1);
073                            } else {
074                                    castFeatureValue(symbol);
075                            }
076                    }
077            }
078            
079            protected void castFeatureValue(String symbol) throws MaltChainedException {
080                    if (column.getType() == ColumnDescription.INTEGER) {
081                            try {
082                                    int dotIndex = symbol.indexOf('.');
083                                    if (dotIndex == -1) {
084                                            featureValue.setValue(Integer.parseInt(symbol));
085                                            featureValue.setSymbol(symbol);
086                                    } else {
087                                            featureValue.setValue(Integer.parseInt(symbol.substring(0,dotIndex)));
088                                            featureValue.setSymbol(symbol.substring(0,dotIndex));
089                                    }
090                            } catch (NumberFormatException e) {
091                                    throw new FeatureException("Could not cast the feature value '"+symbol+"' to integer value.", e);
092                            }
093                    } else if (column.getType() == ColumnDescription.BOOLEAN) {
094                            int dotIndex = symbol.indexOf('.');
095                            if (symbol.equals("1") || symbol.equals("true") ||  symbol.equals("#true#") || (dotIndex != -1 && symbol.substring(0,dotIndex).equals("1"))) {
096                                    featureValue.setValue(1);
097                                    featureValue.setSymbol("true");
098                            } else if (symbol.equals("false") || symbol.equals("0") || (dotIndex != -1 && symbol.substring(0,dotIndex).equals("0"))) {
099                                    featureValue.setValue(0);
100                                    featureValue.setSymbol("false");
101                            } else {
102                                    throw new FeatureException("Could not cast the feature value '"+symbol+"' to boolean value.");
103                            }
104                    } else if (column.getType() == ColumnDescription.REAL) {
105                            try {
106                                    featureValue.setValue(Double.parseDouble(symbol));
107                                    featureValue.setSymbol(symbol);
108                            } catch (NumberFormatException e) {
109                                    throw new FeatureException("Could not cast the feature value '"+symbol+"' to real value.", e);
110                            }
111                    }
112                    if (column.getType() == ColumnDescription.INTEGER || column.getType() == ColumnDescription.BOOLEAN || column.getType() == ColumnDescription.REAL) {
113                            featureValue.setNullValue(false);
114                            featureValue.setIndexCode(1);
115                    }
116            }
117            
118            public FeatureValue getFeatureValue() {
119                    return featureValue;
120            }
121            
122            public boolean equals(Object obj) {
123                    if (this == obj)
124                            return true;
125                    if (obj == null)
126                            return false;
127                    if (getClass() != obj.getClass())
128                            return false;
129                    return obj.toString().equals(this.toString());
130            }
131    
132            public String getColumnName() {
133                    return column.getName();
134            }
135            
136            public SymbolTable getSymbolTable() {
137                    return column.getSymbolTable();
138            }
139            
140            public String toString() {
141                    return column.getName();
142            }
143    }