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.ParsingAlgorithm;
007    import org.maltparser.parser.algorithm.ParsingException;
008    
009    /**
010    *
011    * @author Johan Hall
012    * @since 1.1
013    **/
014    public class CovAddressFunction extends AddressFunction {
015            public enum CovSubFunction {
016                    LEFT, RIGHT, LEFTCONTEXT, RIGHTCONTEXT
017            };
018            protected String subFunctionName;
019            protected CovSubFunction subFunction;
020            protected Covington parsingAlgorithm;
021            protected int index;
022            
023            public CovAddressFunction(String subFunctionName, ParsingAlgorithm parsingAlgorithm) {
024                    super();
025                    setSubFunctionName(subFunctionName);
026                    setParsingAlgorithm((Covington)parsingAlgorithm);
027            }
028    
029            /* (non-Javadoc)
030             * @see org.maltparser.core.feature.function.Function#initialize(java.lang.Object[])
031             */
032            public void initialize(Object[] arguments) throws MaltChainedException {
033                    if (arguments.length != 1) {
034                            throw new ParsingException("Could not initialize "+this.getClass().getName()+": number of arguments are not correct. ");
035                    }
036                    if (!(arguments[0] instanceof Integer)) {
037                            throw new ParsingException("Could not initialize "+this.getClass().getName()+": the first argument is not an integer. ");
038                    }
039                    
040                    setIndex(((Integer)arguments[0]).intValue());
041            }
042            
043            /* (non-Javadoc)
044             * @see org.maltparser.core.feature.function.Function#getParameterTypes()
045             */
046            public Class<?>[] getParameterTypes() {
047                    Class<?>[] paramTypes = { java.lang.Integer.class };
048                    return paramTypes; 
049            }
050            
051            /* (non-Javadoc)
052             * @see org.maltparser.core.feature.function.Function#update()
053             */
054            public void update() throws MaltChainedException {
055                    if (subFunction == CovSubFunction.LEFT) {
056                            address.setAddress(parsingAlgorithm.getLeftNode(index));
057                    } else if (subFunction == CovSubFunction.RIGHT) {
058                            address.setAddress(parsingAlgorithm.getRightNode(index));
059                    } else if (subFunction == CovSubFunction.LEFTCONTEXT) {
060                            address.setAddress(parsingAlgorithm.getLeftContextNode(index));
061                    } else if (subFunction == CovSubFunction.RIGHTCONTEXT) {
062                            address.setAddress(parsingAlgorithm.getRightContextNode(index));
063                    } else {
064                            address.setAddress(null);
065                    }
066            }
067            
068            /**
069             * Returns the string representation of subfunction name
070             * 
071             * @return the string representation of subfunction name
072             */
073            public String getSubFunctionName() {
074                    return subFunctionName;
075            }
076    
077            /**
078             * Sets the string representation of subFunction name
079             * 
080             * @param subFunctionName the subfunction name
081             */
082            public void setSubFunctionName(String subFunctionName) {
083                    this.subFunctionName = subFunctionName;
084                    subFunction = CovSubFunction.valueOf(subFunctionName.toUpperCase());
085            }
086            
087            /**
088             * Returns the subfunction (LEFT, RIGHT, LEFTCONTEXT, RIGHTCONTEXT)
089             * 
090             * @return the subfunction (LEFT, RIGHT, LEFTCONTEXT, RIGHTCONTEXT)
091             */
092            public CovSubFunction getSubFunction() {
093                    return subFunction;
094            }
095            
096            /* (non-Javadoc)
097             * @see org.maltparser.core.feature.function.AddressFunction#getAddressValue()
098             */
099            public AddressValue getAddressValue() {
100                    return address;
101            }
102            
103            /**
104             * Returns one of the two version of the covington parsing algorithm
105             * 
106             * @return one of the two version of the covington parsing algorithm
107             */
108            public Covington getParsingAlgorithm() {
109                    return parsingAlgorithm;
110            }
111    
112            /**
113             * Sets the parsing algorthm
114             * 
115             * @param parsingAlgorithm      a covington parsing algorithm
116             */
117            public void setParsingAlgorithm(Covington parsingAlgorithm) {
118                    this.parsingAlgorithm = parsingAlgorithm;
119            }
120    
121            /**
122             * Returns the index that is used for indexing the data structures: Left, Right, LeftContext and RightContext
123             * 
124             * @return the index that is used for indexing the data structures: Left, Right, LeftContext and RightContext
125             */
126            public int getIndex() {
127                    return index;
128            }
129    
130            /**
131             * Sets the index that is used for indexing the data structures: Left, Right, LeftContext and RightContext
132             * 
133             * @param index the index
134             */
135            public void setIndex(int index) {
136                    this.index = index;
137            }
138            
139            /* (non-Javadoc)
140             * @see org.maltparser.core.feature.function.AddressFunction#equals(java.lang.Object)
141             */
142            public boolean equals(Object obj) {
143                    if (!(obj instanceof CovAddressFunction)) {
144                            return false;
145                    } else if (!parsingAlgorithm.equals(((CovAddressFunction)obj).getParsingAlgorithm())) {
146                            return false;
147                    } else if (index != ((CovAddressFunction)obj).getIndex()) {
148                            return false;
149                    } else if (!subFunction.equals(((CovAddressFunction)obj).getSubFunction())) {
150                            return false;
151                    }
152                    return true;
153            }
154            
155            /* (non-Javadoc)
156             * @see org.maltparser.core.feature.function.AddressFunction#toString()
157             */
158            public String toString() {
159                    return subFunctionName + "[" + index + "]";
160            }
161    }