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