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.value.AddressValue;
006 import org.maltparser.core.syntaxgraph.SyntaxGraphException;
007 import org.maltparser.core.syntaxgraph.node.DependencyNode;
008 import org.maltparser.core.syntaxgraph.node.TokenNode;
009 /**
010 *
011 *
012 * @author Johan Hall
013 */
014 public class DGraphAddressFunction extends AddressFunction {
015 public enum DGraphSubFunction {
016 HEAD, LDEP, RDEP, RDEP2, LSIB, RSIB, PRED, SUCC, ANC, PANC, LDESC, PLDESC, RDESC, PRDESC
017 };
018 private AddressFunction addressFunction;
019 private String subFunctionName;
020 private DGraphSubFunction subFunction;
021
022 public DGraphAddressFunction(String subFunctionName) {
023 super();
024 setSubFunctionName(subFunctionName);
025 }
026
027 public void initialize(Object[] arguments) throws MaltChainedException {
028 if (arguments.length != 1) {
029 throw new SyntaxGraphException("Could not initialize NodeAddressFunction: number of arguments are not correct. ");
030 }
031 if (!(arguments[0] instanceof AddressFunction)) {
032 throw new SyntaxGraphException("Could not initialize NodeAddressFunction: the second argument is not an addres function. ");
033 }
034 setAddressFunction((AddressFunction)arguments[0]);
035 }
036
037 public Class<?>[] getParameterTypes() {
038 Class<?>[] paramTypes = { org.maltparser.core.feature.function.AddressFunction.class };
039 return paramTypes;
040 }
041
042 public void update() throws MaltChainedException {
043 final AddressValue a = addressFunction.getAddressValue();
044 if (a.getAddress() == null) {
045 address.setAddress(null);
046 } else {
047 // try {
048 // a.getAddressClass().asSubclass(org.maltparser.core.syntaxgraph.node.DependencyNode.class);
049
050 final DependencyNode node = (DependencyNode)a.getAddress();
051 if (subFunction == DGraphSubFunction.HEAD && !node.isRoot()) {
052 address.setAddress(node.getHead());
053 } else if (subFunction == DGraphSubFunction.LDEP) {
054 address.setAddress(node.getLeftmostDependent());
055 } else if (subFunction == DGraphSubFunction.RDEP) {
056 address.setAddress(node.getRightmostDependent());
057 } else if (subFunction == DGraphSubFunction.RDEP2) {
058 // To emulate the behavior of MaltParser 0.4 (bug)
059 if (!node.isRoot()) {
060 address.setAddress(node.getRightmostDependent());
061 } else {
062 address.setAddress(null);
063 }
064 } else if (subFunction == DGraphSubFunction.LSIB) {
065 address.setAddress(node.getSameSideLeftSibling());
066 } else if (subFunction == DGraphSubFunction.RSIB) {
067 address.setAddress(node.getSameSideRightSibling());
068 } else if (node instanceof TokenNode && subFunction == DGraphSubFunction.PRED) {
069 address.setAddress(((TokenNode)node).getPredecessor());
070 } else if (node instanceof TokenNode && subFunction == DGraphSubFunction.SUCC) {
071 address.setAddress(((TokenNode)node).getSuccessor());
072 } else if (subFunction == DGraphSubFunction.ANC) {
073 address.setAddress(node.getAncestor());
074 } else if (subFunction == DGraphSubFunction.PANC) {
075 address.setAddress(node.getProperAncestor());
076 } else if (subFunction == DGraphSubFunction.LDESC) {
077 address.setAddress(node.getLeftmostDescendant());
078 } else if (subFunction == DGraphSubFunction.PLDESC) {
079 address.setAddress(node.getLeftmostProperDescendant());
080 } else if (subFunction == DGraphSubFunction.RDESC) {
081 address.setAddress(node.getRightmostDescendant());
082 } else if (subFunction == DGraphSubFunction.PRDESC) {
083 address.setAddress(node.getRightmostProperDescendant());
084 } else {
085 address.setAddress(null);
086 }
087 // } catch (ClassCastException e) {
088 // address.setAddress(null);
089 // }
090 }
091 }
092
093 public void update(Object[] arguments) throws MaltChainedException {
094 update();
095 }
096
097 public AddressFunction getAddressFunction() {
098 return addressFunction;
099 }
100
101 public void setAddressFunction(AddressFunction addressFunction) {
102 this.addressFunction = addressFunction;
103 }
104
105 public String getSubFunctionName() {
106 return subFunctionName;
107 }
108
109 public void setSubFunctionName(String subFunctionName) {
110 this.subFunctionName = subFunctionName;
111 subFunction = DGraphSubFunction.valueOf(subFunctionName.toUpperCase());
112 }
113
114 public DGraphSubFunction getSubFunction() {
115 return subFunction;
116 }
117
118 public boolean equals(Object obj) {
119 if (this == obj)
120 return true;
121 if (obj == null)
122 return false;
123 if (getClass() != obj.getClass())
124 return false;
125 if (!addressFunction.equals(((DGraphAddressFunction)obj).getAddressFunction())) {
126 return false;
127 } else if (!subFunction.equals(((DGraphAddressFunction)obj).getSubFunction())) {
128 return false;
129 }
130 return true;
131 }
132
133 public String toString() {
134 return subFunctionName + "(" + addressFunction.toString() + ")";
135 }
136 }