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