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