001 package org.maltparser.parser.algorithm.covington;
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.parser.Algorithm;
007 import org.maltparser.parser.ParsingException;
008 /**
009 * @author Johan Hall
010 *
011 */
012 public class CovingtonAddressFunction extends AddressFunction {
013 public enum CovingtonSubFunction {
014 LEFT, RIGHT, LEFTCONTEXT, RIGHTCONTEXT
015 };
016 protected String subFunctionName;
017 protected CovingtonSubFunction subFunction;
018 protected Algorithm parsingAlgorithm;
019 protected int index;
020
021 public CovingtonAddressFunction(String subFunctionName, Algorithm parsingAlgorithm) {
022 super();
023 setSubFunctionName(subFunctionName);
024 setAlgorithm(parsingAlgorithm);
025 }
026
027 public void initialize(Object[] arguments) throws MaltChainedException {
028 if (arguments.length != 1) {
029 throw new ParsingException("Could not initialize "+this.getClass().getName()+": number of arguments are not correct. ");
030 }
031 if (!(arguments[0] instanceof Integer)) {
032 throw new ParsingException("Could not initialize "+this.getClass().getName()+": the first argument is not an integer. ");
033 }
034
035 setIndex(((Integer)arguments[0]).intValue());
036 }
037
038 public Class<?>[] getParameterTypes() {
039 Class<?>[] paramTypes = { java.lang.Integer.class };
040 return paramTypes;
041 }
042
043 public void update() throws MaltChainedException {
044 update((CovingtonConfig)parsingAlgorithm.getCurrentParserConfiguration());
045 }
046
047 public void update(Object[] arguments) throws MaltChainedException {
048 if (arguments.length != 1 || !(arguments[0] instanceof CovingtonConfig)) {
049 throw new ParsingException("Number of arguments to the Covington address function is not correct. ");
050 }
051 update((CovingtonConfig)arguments[0]);
052 }
053
054 private void update(CovingtonConfig config) throws MaltChainedException {
055 if (subFunction == CovingtonSubFunction.LEFT) {
056 address.setAddress(config.getLeftNode(index));
057 } else if (subFunction == CovingtonSubFunction.RIGHT) {
058 address.setAddress(config.getRightNode(index));
059 } else if (subFunction == CovingtonSubFunction.LEFTCONTEXT) {
060 address.setAddress(config.getLeftContextNode(index));
061 } else if (subFunction == CovingtonSubFunction.RIGHTCONTEXT) {
062 address.setAddress(config.getRightContextNode(index));
063 } else {
064 address.setAddress(null);
065 }
066 }
067
068 public String getSubFunctionName() {
069 return subFunctionName;
070 }
071
072 public void setSubFunctionName(String subFunctionName) {
073 this.subFunctionName = subFunctionName;
074 subFunction = CovingtonSubFunction.valueOf(subFunctionName.toUpperCase());
075 }
076
077 public CovingtonSubFunction getSubFunction() {
078 return subFunction;
079 }
080
081 public AddressValue getAddressValue() {
082 return address;
083 }
084
085 public Algorithm getParsingAlgorithm() {
086 return parsingAlgorithm;
087 }
088
089 public void setAlgorithm(Algorithm parsingAlgorithm) {
090 this.parsingAlgorithm = parsingAlgorithm;
091 }
092
093 public int getIndex() {
094 return index;
095 }
096
097 public void setIndex(int index) {
098 this.index = index;
099 }
100
101 public boolean equals(Object obj) {
102 if (this == obj)
103 return true;
104 if (obj == null)
105 return false;
106 if (getClass() != obj.getClass())
107 return false;
108
109 CovingtonAddressFunction other = (CovingtonAddressFunction) obj;
110 if (index != other.index)
111 return false;
112 if (parsingAlgorithm == null) {
113 if (other.parsingAlgorithm != null)
114 return false;
115 } else if (!parsingAlgorithm.equals(other.parsingAlgorithm))
116 return false;
117 if (subFunction == null) {
118 if (other.subFunction != null)
119 return false;
120 } else if (!subFunction.equals(other.subFunction))
121 return false;
122 return true;
123 }
124
125 public String toString() {
126 return subFunctionName + "[" + index + "]";
127 }
128 }