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.function.FeatureFunction;
006 import org.maltparser.core.feature.value.FeatureValue;
007 import org.maltparser.core.feature.value.SingleFeatureValue;
008 import org.maltparser.core.io.dataformat.ColumnDescription;
009 import org.maltparser.core.symbol.SymbolTable;
010 import org.maltparser.core.symbol.SymbolTableHandler;
011 import org.maltparser.core.syntaxgraph.SyntaxGraphException;
012
013 public class ExistsFeature implements FeatureFunction {
014 protected AddressFunction addressFunction;
015 protected SymbolTableHandler tableHandler;
016 protected SymbolTable table;
017 protected SingleFeatureValue featureValue;
018
019 public ExistsFeature(SymbolTableHandler tableHandler) throws MaltChainedException {
020 super();
021 featureValue = new SingleFeatureValue(this);
022 setTableHandler(tableHandler);
023 }
024
025 /**
026 * Initialize the exists feature function
027 *
028 * @param arguments an array of arguments with the type returned by getParameterTypes()
029 * @throws MaltChainedException
030 */
031 public void initialize(Object[] arguments) throws MaltChainedException {
032 if (arguments.length != 1) {
033 throw new SyntaxGraphException("Could not initialize ExistsFeature: number of arguments are not correct. ");
034 }
035 // Checks that the two arguments are address functions
036 if (!(arguments[0] instanceof AddressFunction)) {
037 throw new SyntaxGraphException("Could not initialize ExistsFeature: the first argument is not an address function. ");
038 }
039
040 setAddressFunction((AddressFunction)arguments[0]);
041
042 // Creates a symbol table called "EXISTS" using one null value
043 setSymbolTable(tableHandler.addSymbolTable("EXISTS", ColumnDescription.INPUT, "one"));
044
045 table.addSymbol("TRUE"); // The address exists
046 table.addSymbol("FALSE"); // The address don't exists
047 }
048
049 /**
050 * Returns an array of class types used by the feature extraction system to invoke initialize with
051 * correct arguments.
052 *
053 * @return an array of class types
054 */
055 public Class<?>[] getParameterTypes() {
056 Class<?>[] paramTypes = { org.maltparser.core.feature.function.AddressFunction.class };
057 return paramTypes;
058 }
059 /**
060 * Returns the string representation of the integer <code>code</code> according to the exists feature function.
061 *
062 * @param code the integer representation of the symbol
063 * @return the string representation of the integer <code>code</code> according to the exists feature function.
064 * @throws MaltChainedException
065 */
066 public String getSymbol(int code) throws MaltChainedException {
067 return table.getSymbolCodeToString(code);
068 }
069
070 /**
071 * Returns the integer representation of the string <code>symbol</code> according to the exists feature function.
072 *
073 * @param symbol the string representation of the symbol
074 * @return the integer representation of the string <code>symbol</code> according to the exists feature function.
075 * @throws MaltChainedException
076 */
077 public int getCode(String symbol) throws MaltChainedException {
078 return table.getSymbolStringToCode(symbol);
079 }
080
081 /**
082 * Cause the exists feature function to update the cardinality of the feature value.
083 *
084 * @throws MaltChainedException
085 */
086 public void updateCardinality() {
087 featureValue.setCardinality(table.getValueCounter());
088 }
089
090 /**
091 * Cause the feature function to update the feature value.
092 *
093 * @throws MaltChainedException
094 */
095 public void update() throws MaltChainedException {
096 if (addressFunction.getAddressValue().getAddress() != null) {
097 featureValue.setCode(table.getSymbolStringToCode("TRUE"));
098 featureValue.setSymbol("TRUE");
099 featureValue.setKnown(true);
100 featureValue.setNullValue(false);
101 } else {
102 featureValue.setCode(table.getSymbolStringToCode("FALSE"));
103 featureValue.setSymbol("FALSE");
104 featureValue.setKnown(true);
105 featureValue.setNullValue(false);
106 }
107 }
108
109 /**
110 * Returns the feature value
111 *
112 * @return the feature value
113 */
114 public FeatureValue getFeatureValue() {
115 return featureValue;
116 }
117
118 /**
119 * Returns the symbol table used by the exists feature function
120 *
121 * @return the symbol table used by the exists feature function
122 */
123 public SymbolTable getSymbolTable() {
124 return table;
125 }
126
127 /**
128 * Returns the address function
129 *
130 * @return the address function
131 */
132 public AddressFunction getAddressFunction() {
133 return addressFunction;
134 }
135
136
137 /**
138 * Sets the address function
139 *
140 * @param addressFunction a address function
141 */
142 public void setAddressFunction(AddressFunction addressFunction) {
143 this.addressFunction = addressFunction;
144 }
145
146 /**
147 * Returns symbol table handler
148 *
149 * @return a symbol table handler
150 */
151 public SymbolTableHandler getTableHandler() {
152 return tableHandler;
153 }
154 /**
155 * Sets the symbol table handler
156 *
157 * @param tableHandler a symbol table handler
158 */
159 public void setTableHandler(SymbolTableHandler tableHandler) {
160 this.tableHandler = tableHandler;
161 }
162
163 /**
164 * Sets the symbol table used by the exists feature function
165 *
166 * @param table
167 */
168 public void setSymbolTable(SymbolTable table) {
169 this.table = table;
170 }
171
172 public boolean equals(Object obj) {
173 if (this == obj)
174 return true;
175 if (obj == null)
176 return false;
177 if (getClass() != obj.getClass())
178 return false;
179 return obj.toString().equals(this.toString());
180 }
181
182 public int hashCode() {
183 return 217 + (null == toString() ? 0 : toString().hashCode());
184 }
185
186 public String toString() {
187 final StringBuilder sb = new StringBuilder();
188 sb.append("Exists(");
189 sb.append(addressFunction.toString());
190 sb.append(')');
191 return sb.toString();
192 }
193 }