    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.AddressFunction;
    006    import org.maltparser.core.feature.function.FeatureFunction;
    007    import org.maltparser.core.feature.value.AddressValue;
    008    import org.maltparser.core.feature.value.FeatureValue;
    009    import org.maltparser.core.feature.value.SingleFeatureValue;
    010    import org.maltparser.core.io.dataformat.ColumnDescription;
    011    import org.maltparser.core.io.dataformat.DataFormatInstance;
    012    import org.maltparser.core.symbol.SymbolTable;
    013    import org.maltparser.core.symbol.SymbolTableHandler;
    014    import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId;
    015    import org.maltparser.core.syntaxgraph.SyntaxGraphException;
    016    import org.maltparser.core.syntaxgraph.node.DependencyNode;
    017    /**
    018    *
    019    * @author Johan Hall
    020    * @since 1.1
    021    **/
    022    public class InputArcFeature implements FeatureFunction {
    023            protected AddressFunction addressFunction1;
    024            protected AddressFunction addressFunction2;
    025            protected ColumnDescription column;
    026            protected DataFormatInstance dataFormatInstance;
    027            protected SymbolTableHandler tableHandler;
    028            protected SymbolTable table;
    029            protected SingleFeatureValue featureValue;
    030
    031
    032            public InputArcFeature(DataFormatInstance dataFormatInstance, SymbolTableHandler tableHandler) throws MaltChainedException {
    033                    super();
    034                    setDataFormatInstance(dataFormatInstance);
    035                    setTableHandler(tableHandler);
    036                    setFeatureValue(new SingleFeatureValue(this));
    037            }
    038
    039            public void initialize(Object[] arguments) throws MaltChainedException {
    040                    if (arguments.length != 3) {
    041                            throw new FeatureException("Could not initialize InputArcFeature: number of arguments are not correct. ");
    042                    }
    043                    // Checks that the two arguments are address functions
    044
    045                    if (!(arguments[0] instanceof String)) {
    046                            throw new FeatureException("Could not initialize InputArcFeature: the first argument is not a string. ");
    047                    }
    048                    if (!(arguments[1] instanceof AddressFunction)) {
    049                            throw new SyntaxGraphException("Could not initialize InputArcFeature: the second argument is not an address function. ");
    050                    }
    051                    if (!(arguments[2] instanceof AddressFunction)) {
    052                            throw new SyntaxGraphException("Could not initialize InputArcFeature: the third argument is not an address function. ");
    053                    }
    054                    setAddressFunction1((AddressFunction)arguments[1]);
    055                    setAddressFunction2((AddressFunction)arguments[2]);
    056
    057                    setColumn(dataFormatInstance.getColumnDescriptionByName((String)arguments[0]));
    058                    setSymbolTable(tableHandler.addSymbolTable("ARC_"+column.getName(),ColumnDescription.INPUT, "one"));
    059                    table.addSymbol("LEFT");
    060                    table.addSymbol("RIGHT");
    061            }
    062
    063            public Class<?>[] getParameterTypes() {
    064                Class<?>[] paramTypes = { java.lang.String.class, org.maltparser.core.feature.function.AddressFunction.class, org.maltparser.core.feature.function.AddressFunction.class };
    065                    return paramTypes;
    066            }
    067
    068            public int getCode(String symbol) throws MaltChainedException {
    069                    return table.getSymbolStringToCode(symbol);
    070            }
    071
    072
    073            public FeatureValue getFeatureValue() {
    074                    return featureValue;
    075            }
    076
    077
    078            public String getSymbol(int code) throws MaltChainedException {
    079                    return table.getSymbolCodeToString(code);
    080            }
    081
    082
    083            public void updateCardinality() throws MaltChainedException {
    084    //              featureValue.setCardinality(table.getValueCounter());
    085            }
    086
    087            public void update() throws MaltChainedException {
    088                    // Retrieve the address value
    089                    final AddressValue arg1 = addressFunction1.getAddressValue();
    090                    final AddressValue arg2 = addressFunction2.getAddressValue();
    091                    if (arg1.getAddress() != null && arg1.getAddressClass() == org.maltparser.core.syntaxgraph.node.DependencyNode.class &&
    092                        arg2.getAddress() != null && arg2.getAddressClass() == org.maltparser.core.syntaxgraph.node.DependencyNode.class) {
    093                        DependencyNode node1 = (DependencyNode)arg1.getAddress();
    094                        DependencyNode node2 = (DependencyNode)arg2.getAddress();
    095                        try {
    096                            int head1 = Integer.parseInt(node1.getLabelSymbol(column.getSymbolTable()));
    097                            int head2 = Integer.parseInt(node2.getLabelSymbol(column.getSymbolTable()));
    098                            if (!node1.isRoot() && head1 == node2.getIndex()) {
    099                                featureValue.setIndexCode(table.getSymbolStringToCode("LEFT"));
    100                                featureValue.setSymbol("LEFT");
    101                                featureValue.setNullValue(false);
    102                            } else if (!node2.isRoot() && head2 == node1.getIndex()) {
    103                                featureValue.setIndexCode(table.getSymbolStringToCode("RIGHT"));
    104                                featureValue.setSymbol("RIGHT");
    105                                featureValue.setNullValue(false);
    106                            } else {
    107                                featureValue.setIndexCode(table.getNullValueCode(NullValueId.NO_NODE));
    108                                featureValue.setSymbol(table.getNullValueSymbol(NullValueId.NO_NODE));
    109                                featureValue.setNullValue(true);
    110                            }
    111                        } catch (NumberFormatException e) {
    112                            throw new FeatureException("The index of the feature must be an integer value. ", e);
    113                        }
    114                    } else {
    115                        featureValue.setIndexCode(table.getNullValueCode(NullValueId.NO_NODE));
    116                        featureValue.setSymbol(table.getNullValueSymbol(NullValueId.NO_NODE));
    117                        featureValue.setNullValue(true);
    118                    }
    119    //              featureValue.setKnown(true);
    120                    featureValue.setValue(1);
    121            }
    122
    123            public ColumnDescription getColumn() {
    124                    return column;
    125            }
    126
    127            public void setColumn(ColumnDescription column) throws MaltChainedException {
    128                    if (column.getType() != ColumnDescription.INTEGER) {
    129                            throw new FeatureException("InputArc feature column must be of type integer. ");
    130                    }
    131                    this.column = column;
    132            }
    133
    134            /**
    135             * Returns the address function 1 (argument 1)
    136             *
    137             * @return the address function 1 (argument 1)
    138             */
    139            public AddressFunction getAddressFunction1() {
    140                    return addressFunction1;
    141            }
    142
    143
    144            /**
    145             * Sets the address function 1 (argument 1)
    146             *
    147             * @param addressFunction1 a address function 1 (argument 1)
    148             */
    149            public void setAddressFunction1(AddressFunction addressFunction1) {
    150                    this.addressFunction1 = addressFunction1;
    151            }
    152
    153            /**
    154             * Returns the address function 2 (argument 2)
    155             *
    156             * @return the address function 1 (argument 2)
    157             */
    158            public AddressFunction getAddressFunction2() {
    159                    return addressFunction2;
    160            }
    161
    162            /**
    163             * Sets the address function 2 (argument 2)
    164             *
    165             * @param addressFunction2 a address function 2 (argument 2)
    166             */
    167            public void setAddressFunction2(AddressFunction addressFunction2) {
    168                    this.addressFunction2 = addressFunction2;
    169            }
    170
    171            public DataFormatInstance getDataFormatInstance() {
    172                    return dataFormatInstance;
    173            }
    174
    175            public void setDataFormatInstance(DataFormatInstance dataFormatInstance) {
    176                    this.dataFormatInstance = dataFormatInstance;
    177            }
    178
    179            public void setFeatureValue(SingleFeatureValue featureValue) {
    180                    this.featureValue = featureValue;
    181            }
    182
    183            public SymbolTable getSymbolTable() {
    184                    return table;
    185            }
    186
    187            public void setSymbolTable(SymbolTable table) {
    188                    this.table = table;
    189            }
    190
    191            public SymbolTableHandler getTableHandler() {
    192                    return tableHandler;
    193            }
    194
    195            public void setTableHandler(SymbolTableHandler tableHandler) {
    196                    this.tableHandler = tableHandler;
    197            }
    198
    199            public boolean equals(Object obj) {
    200                    if (!(obj instanceof InputArcFeature)) {
    201                            return false;
    202                    }
    203                    if (!obj.toString().equals(this.toString())) {
    204                            return false;
    205                    }
    206                    return true;
    207            }
    208
    209            public String toString() {
    210                    return "InputArc(" + column.getName() + ")";
    211            }
    212    }