001package org.maltparser.core.syntaxgraph.feature; 002 003import org.maltparser.core.exception.MaltChainedException; 004import org.maltparser.core.feature.function.AddressFunction; 005import org.maltparser.core.feature.value.AddressValue; 006import org.maltparser.core.syntaxgraph.SyntaxGraphException; 007import org.maltparser.core.syntaxgraph.node.DependencyNode; 008/** 009* 010* 011* @author Johan Hall 012*/ 013public final class DGraphAddressFunction extends AddressFunction { 014 public final static Class<?>[] paramTypes = { org.maltparser.core.feature.function.AddressFunction.class }; 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 final String subFunctionName; 020 private final DGraphSubFunction subFunction; 021 022 public DGraphAddressFunction(String _subFunctionName) { 023 super(); 024 this.subFunctionName = _subFunctionName; 025 this.subFunction = DGraphSubFunction.valueOf(subFunctionName.toUpperCase()); 026 } 027 028 public void initialize(Object[] arguments) throws MaltChainedException { 029 if (arguments.length != 1) { 030 throw new SyntaxGraphException("Could not initialize DGraphAddressFunction: number of arguments are not correct. "); 031 } 032 if (!(arguments[0] instanceof AddressFunction)) { 033 throw new SyntaxGraphException("Could not initialize DGraphAddressFunction: the second argument is not an addres function. "); 034 } 035 this.addressFunction = (AddressFunction)arguments[0]; 036 } 037 038 public Class<?>[] getParameterTypes() { 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 && !node.isRoot()) { 069 address.setAddress(node.getPredecessor()); 070 } else if (subFunction == DGraphSubFunction.SUCC && !node.isRoot()) { 071 address.setAddress(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 String getSubFunctionName() { 102 return subFunctionName; 103 } 104 105 public DGraphSubFunction getSubFunction() { 106 return subFunction; 107 } 108 109 public boolean equals(Object obj) { 110 if (this == obj) 111 return true; 112 if (obj == null) 113 return false; 114 if (getClass() != obj.getClass()) 115 return false; 116 if (!addressFunction.equals(((DGraphAddressFunction)obj).getAddressFunction())) { 117 return false; 118 } else if (!subFunction.equals(((DGraphAddressFunction)obj).getSubFunction())) { 119 return false; 120 } 121 return true; 122 } 123 124 public String toString() { 125 return subFunctionName + "(" + addressFunction.toString() + ")"; 126 } 127}