001 package org.maltparser.core.feature.map;
002
003
004 import org.maltparser.core.exception.MaltChainedException;
005 import org.maltparser.core.feature.FeatureException;
006 import org.maltparser.core.feature.function.FeatureFunction;
007 import org.maltparser.core.feature.function.FeatureMapFunction;
008 import org.maltparser.core.feature.value.FeatureValue;
009 import org.maltparser.core.feature.value.FunctionValue;
010 import org.maltparser.core.feature.value.SingleFeatureValue;
011 import org.maltparser.core.symbol.SymbolTable;
012 import org.maltparser.core.symbol.SymbolTableHandler;
013 /**
014 *
015 *
016 * @author Johan Hall
017 */
018 public class MergeFeature implements FeatureMapFunction {
019 protected FeatureFunction firstFeature;
020 protected FeatureFunction secondFeature;
021 protected SymbolTableHandler tableHandler;
022 protected SymbolTable table;
023 protected SingleFeatureValue singleFeatureValue;
024
025 public MergeFeature(SymbolTableHandler tableHandler) throws MaltChainedException {
026 super();
027 setTableHandler(tableHandler);
028 singleFeatureValue = new SingleFeatureValue(this);
029 }
030
031 public void initialize(Object[] arguments) throws MaltChainedException {
032 if (arguments.length != 2) {
033 throw new FeatureException("Could not initialize MergeFeature: number of arguments are not correct. ");
034 }
035 if (!(arguments[0] instanceof FeatureFunction)) {
036 throw new FeatureException("Could not initialize MergeFeature: the first argument is not a feature. ");
037 }
038 if (!(arguments[1] instanceof FeatureFunction)) {
039 throw new FeatureException("Could not initialize MergeFeature: the second argument is not a feature. ");
040 }
041 setFirstFeature((FeatureFunction)arguments[0]);
042 setSecondFeature((FeatureFunction)arguments[1]);
043 setSymbolTable(tableHandler.addSymbolTable("MERGE2_"+firstFeature.getSymbolTable().getName()+"_"+secondFeature.getSymbolTable().getName(), firstFeature.getSymbolTable()));
044 }
045
046 public void update() throws MaltChainedException {
047 // multipleFeatureValue.reset();
048 singleFeatureValue.reset();
049 firstFeature.update();
050 secondFeature.update();
051 FunctionValue firstValue = firstFeature.getFeatureValue();
052 FunctionValue secondValue = secondFeature.getFeatureValue();
053 if (firstValue instanceof SingleFeatureValue && secondValue instanceof SingleFeatureValue) {
054 String symbol = ((SingleFeatureValue)firstValue).getSymbol();
055 if (((FeatureValue)firstValue).isNullValue() && ((FeatureValue)secondValue).isNullValue()) {
056 singleFeatureValue.setCode(firstFeature.getSymbolTable().getSymbolStringToCode(symbol));
057 singleFeatureValue.setKnown(firstFeature.getSymbolTable().getKnown(symbol));
058 singleFeatureValue.setSymbol(symbol);
059 singleFeatureValue.setNullValue(true);
060 // multipleFeatureValue.addFeatureValue(firstFeature.getSymbolTable().getSymbolStringToCode(symbol), symbol, true);
061 // multipleFeatureValue.setNullValue(true);
062 } else {
063 StringBuilder mergedValue = new StringBuilder();
064 mergedValue.append(((SingleFeatureValue)firstValue).getSymbol());
065 mergedValue.append('~');
066 mergedValue.append(((SingleFeatureValue)secondValue).getSymbol());
067
068 singleFeatureValue.setCode(table.addSymbol(mergedValue.toString()));
069 singleFeatureValue.setKnown(table.getKnown(mergedValue.toString()));
070 singleFeatureValue.setSymbol(mergedValue.toString());
071 singleFeatureValue.setNullValue(false);
072 // multipleFeatureValue.addFeatureValue(table.addSymbol(mergedValue.toString()), mergedValue.toString(), table.getKnown(mergedValue.toString()));
073 // multipleFeatureValue.setNullValue(false);
074 }
075 } else {
076 throw new FeatureException("It is not possible to merge Split-features. ");
077 }
078 }
079
080 public Class<?>[] getParameterTypes() {
081 Class<?>[] paramTypes = { org.maltparser.core.feature.function.FeatureFunction.class, org.maltparser.core.feature.function.FeatureFunction.class };
082 return paramTypes;
083 }
084
085 public FeatureValue getFeatureValue() {
086 return singleFeatureValue;
087 // return multipleFeatureValue;
088 }
089
090 public String getSymbol(int code) throws MaltChainedException {
091 return table.getSymbolCodeToString(code);
092 }
093
094 public int getCode(String symbol) throws MaltChainedException {
095 return table.getSymbolStringToCode(symbol);
096 }
097
098 public void updateCardinality() throws MaltChainedException {
099 firstFeature.updateCardinality();
100 secondFeature.updateCardinality();
101 singleFeatureValue.setCardinality(table.getValueCounter());
102 // multipleFeatureValue.setCardinality(table.getValueCounter());
103 }
104
105 public FeatureFunction getFirstFeature() {
106 return firstFeature;
107 }
108
109 public void setFirstFeature(FeatureFunction firstFeature) {
110 this.firstFeature = firstFeature;
111 }
112
113 public FeatureFunction getSecondFeature() {
114 return secondFeature;
115 }
116
117 public void setSecondFeature(FeatureFunction secondFeature) {
118 this.secondFeature = secondFeature;
119 }
120
121 public SymbolTableHandler getTableHandler() {
122 return tableHandler;
123 }
124
125 public void setTableHandler(SymbolTableHandler tableHandler) {
126 this.tableHandler = tableHandler;
127 }
128
129 public SymbolTable getSymbolTable() {
130 return table;
131 }
132
133 public void setSymbolTable(SymbolTable table) {
134 this.table = table;
135 }
136
137 public boolean equals(Object obj) {
138 if (this == obj)
139 return true;
140 if (obj == null)
141 return false;
142 if (getClass() != obj.getClass())
143 return false;
144 return obj.toString().equals(this.toString());
145 }
146
147 public String toString() {
148 final StringBuilder sb = new StringBuilder();
149 sb.append("Merge(");
150 sb.append(firstFeature.toString());
151 sb.append(", ");
152 sb.append(secondFeature.toString());
153 sb.append(')');
154 return sb.toString();
155 }
156
157 }