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 ((subFunction == DGraphSubFunction.PRED || subFunction == DGraphSubFunction.SUCC) && node instanceof TokenNode) {
069 final TokenNode tokenNode = (TokenNode)node;
070 if (subFunction == DGraphSubFunction.PRED) {
071 address.setAddress(tokenNode.getPredecessor());
072 } else if (subFunction == DGraphSubFunction.SUCC) {
073 address.setAddress(tokenNode.getSuccessor());
074 }
075 } else if (subFunction == DGraphSubFunction.ANC) {
076 address.setAddress(node.getAncestor());
077 } else if (subFunction == DGraphSubFunction.PANC) {
078 address.setAddress(node.getProperAncestor());
079 } else if (subFunction == DGraphSubFunction.LDESC) {
080 address.setAddress(node.getLeftmostDescendant());
081 } else if (subFunction == DGraphSubFunction.PLDESC) {
082 address.setAddress(node.getLeftmostProperDescendant());
083 } else if (subFunction == DGraphSubFunction.RDESC) {
084 address.setAddress(node.getRightmostDescendant());
085 } else if (subFunction == DGraphSubFunction.PRDESC) {
086 address.setAddress(node.getRightmostProperDescendant());
087 } else {
088 address.setAddress(null);
089 }
090 // } catch (ClassCastException e) {
091 // address.setAddress(null);
092 // }
093 }
094 }
095
096 public void update(Object[] arguments) throws MaltChainedException {
097 update();
098 }
099
100 public AddressFunction getAddressFunction() {
101 return addressFunction;
102 }
103
104 public void setAddressFunction(AddressFunction addressFunction) {
105 this.addressFunction = addressFunction;
106 }
107
108 public String getSubFunctionName() {
109 return subFunctionName;
110 }
111
112 public void setSubFunctionName(String subFunctionName) {
113 this.subFunctionName = subFunctionName;
114 subFunction = DGraphSubFunction.valueOf(subFunctionName.toUpperCase());
115 }
116
117 public DGraphSubFunction getSubFunction() {
118 return subFunction;
119 }
120
121 public boolean equals(Object obj) {
122 if (this == obj)
123 return true;
124 if (obj == null)
125 return false;
126 if (getClass() != obj.getClass())
127 return false;
128 if (!addressFunction.equals(((DGraphAddressFunction)obj).getAddressFunction())) {
129 return false;
130 } else if (!subFunction.equals(((DGraphAddressFunction)obj).getSubFunction())) {
131 return false;
132 }
133 return true;
134 }
135
136 public String toString() {
137 return subFunctionName + "(" + addressFunction.toString() + ")";
138 }
139 }