    001    package org.maltparser.core.syntaxgraph.feature;
    002
    003    import org.maltparser.core.exception.MaltChainedException;
    004    import org.maltparser.core.feature.function.AddressFunction;
    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.symbol.SymbolTable;
    010    import org.maltparser.core.symbol.SymbolTableHandler;
    011    import org.maltparser.core.syntaxgraph.SyntaxGraphException;
    012
    013    public class ExistsFeature implements FeatureFunction {
    014            protected AddressFunction addressFunction;
    015            protected SymbolTableHandler tableHandler;
    016            protected SymbolTable table;
    017            protected SingleFeatureValue featureValue;
    018
    019            public ExistsFeature(SymbolTableHandler tableHandler) throws MaltChainedException {
    020                    super();
    021                    featureValue = new SingleFeatureValue(this);
    022                    setTableHandler(tableHandler);
    023            }
    024
    025            /**
    026             * Initialize the exists feature function
    027             *
    028             * @param arguments an array of arguments with the type returned by getParameterTypes()
    029             * @throws MaltChainedException
    030             */
    031            public void initialize(Object[] arguments) throws MaltChainedException {
    032                    if (arguments.length != 1) {
    033                            throw new SyntaxGraphException("Could not initialize ExistsFeature: number of arguments are not correct. ");
    034                    }
    035                    // Checks that the two arguments are address functions
    036                    if (!(arguments[0] instanceof AddressFunction)) {
    037                            throw new SyntaxGraphException("Could not initialize ExistsFeature: the first argument is not an address function. ");
    038                    }
    039
    040                    setAddressFunction((AddressFunction)arguments[0]);
    041
    042                    // Creates a symbol table called "EXISTS" using one null value
    043    //              setSymbolTable(tableHandler.addSymbolTable("EXISTS", ColumnDescription.INPUT, "one"));
    044    //
    045    //              table.addSymbol("TRUE"); // The address exists
    046    //              table.addSymbol("FALSE"); // The address don't exists
    047            }
    048
    049            /**
    050             * Returns an array of class types used by the feature extraction system to invoke initialize with
    051             * correct arguments.
    052             *
    053             * @return an array of class types
    054             */
    055            public Class<?>[] getParameterTypes() {
    056                    Class<?>[] paramTypes = { org.maltparser.core.feature.function.AddressFunction.class };
    057                    return paramTypes;
    058            }
    059            /**
    060             * Returns the string representation of the integer <code>code</code> according to the exists feature function.
    061             *
    062             * @param code the integer representation of the symbol
    063             * @return the string representation of the integer <code>code</code> according to the exists feature function.
    064             * @throws MaltChainedException
    065             */
    066            public String getSymbol(int code) throws MaltChainedException {
    067                    return (code == 1)?"true":"false";
    068            }
    069
    070            /**
    071             * Returns the integer representation of the string <code>symbol</code> according to the exists feature function.
    072             *
    073             * @param symbol the string representation of the symbol
    074             * @return the integer representation of the string <code>symbol</code> according to the exists feature function.
    075             * @throws MaltChainedException
    076             */
    077            public int getCode(String symbol) throws MaltChainedException {
    078                    return (symbol.equals("true"))?1:0;
    079            }
    080
    081            /**
    082             * Cause the exists feature function to update the cardinality of the feature value.
    083             *
    084             * @throws MaltChainedException
    085             */
    086            public void updateCardinality() {
    087    //              featureValue.setCardinality(table.getValueCounter());
    088            }
    089
    090            /**
    091             * Cause the feature function to update the feature value.
    092             *
    093             * @throws MaltChainedException
    094             */
    095            public void update() throws MaltChainedException {
    096                    featureValue.setIndexCode(1);
    097                    featureValue.setNullValue(false);
    098                    if (addressFunction.getAddressValue().getAddress() != null) {
    099                            featureValue.setSymbol("true");
    100                            featureValue.setValue(1);
    101                    } else {
    102                            featureValue.setSymbol("false");
    103                            featureValue.setValue(0);
    104                    }
    105            }
    106
    107            /**
    108             * Returns the feature value
    109             *
    110             * @return the feature value
    111             */
    112            public FeatureValue getFeatureValue() {
    113                    return featureValue;
    114            }
    115
    116            /**
    117             * Returns the symbol table used by the exists feature function
    118             *
    119             * @return the symbol table used by the exists feature function
    120             */
    121            public SymbolTable getSymbolTable() {
    122                    return table;
    123            }
    124
    125            /**
    126             * Returns the address function
    127             *
    128             * @return the address function
    129             */
    130            public AddressFunction getAddressFunction() {
    131                    return addressFunction;
    132            }
    133
    134
    135            /**
    136             * Sets the address function
    137             *
    138             * @param addressFunction a address function
    139             */
    140            public void setAddressFunction(AddressFunction addressFunction) {
    141                    this.addressFunction = addressFunction;
    142            }
    143
    144            /**
    145             * Returns symbol table handler
    146             *
    147             * @return a symbol table handler
    148             */
    149            public SymbolTableHandler getTableHandler() {
    150                    return tableHandler;
    151            }
    152            /**
    153             * Sets the symbol table handler
    154             *
    155             * @param tableHandler a symbol table handler
    156             */
    157            public void setTableHandler(SymbolTableHandler tableHandler) {
    158                    this.tableHandler = tableHandler;
    159            }
    160
    161            /**
    162             * Sets the symbol table used by the exists feature function
    163             *
    164             * @param table
    165             */
    166            public void setSymbolTable(SymbolTable table) {
    167                    this.table = table;
    168            }
    169
    170            public  int getType() {
    171                    return ColumnDescription.BOOLEAN;
    172            }
    173
    174            public String getMapIdentifier() {
    175                    return "EXISTS";
    176            }
    177
    178            public boolean equals(Object obj) {
    179                    if (this == obj)
    180                            return true;
    181                    if (obj == null)
    182                            return false;
    183                    if (getClass() != obj.getClass())
    184                            return false;
    185                    return obj.toString().equals(this.toString());
    186            }
    187
    188            public int hashCode() {
    189                    return 217 + (null == toString() ? 0 : toString().hashCode());
    190            }
    191
    192            public String toString() {
    193                    final StringBuilder sb = new StringBuilder();
    194                    sb.append("Exists(");
    195                    sb.append(addressFunction.toString());
    196                    sb.append(')');
    197                    return sb.toString();
    198            }
    199    }