001 package org.maltparser.core.symbol; 002 003 import org.maltparser.core.exception.MaltChainedException; 004 import org.maltparser.core.feature.function.FeatureFunction; 005 import org.maltparser.core.feature.function.Modifiable; 006 import org.maltparser.core.feature.value.FeatureValue; 007 import org.maltparser.core.feature.value.SingleFeatureValue; 008 import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId; 009 010 public abstract class TableFeature implements FeatureFunction, Modifiable { 011 protected SingleFeatureValue featureValue; 012 protected SymbolTable table; 013 014 public TableFeature() throws MaltChainedException { 015 featureValue = new SingleFeatureValue(this); 016 } 017 018 public abstract void update() throws MaltChainedException; 019 public abstract void initialize(Object[] arguments) throws MaltChainedException; 020 public abstract Class<?>[] getParameterTypes(); 021 022 public String getSymbol(int value) throws MaltChainedException { 023 return table.getSymbolCodeToString(value); 024 } 025 026 public int getCode(String value) throws MaltChainedException { 027 return table.getSymbolStringToCode(value); 028 } 029 030 public SymbolTable getSymbolTable() { 031 return table; 032 } 033 034 public void setSymbolTable(SymbolTable table) { 035 this.table = table; 036 } 037 038 public void updateCardinality() { 039 featureValue.setCardinality(table.getValueCounter()); 040 } 041 042 public void setFeatureValue(int value) throws MaltChainedException { 043 if (table.getSymbolCodeToString(value) == null) { 044 featureValue.setCode(value); 045 featureValue.setKnown(table.getKnown(value)); 046 featureValue.setSymbol(table.getNullValueSymbol(NullValueId.NO_NODE)); 047 featureValue.setNullValue(true); 048 } else { 049 featureValue.setCode(value); 050 featureValue.setKnown(table.getKnown(value)); 051 featureValue.setSymbol(table.getSymbolCodeToString(value)); 052 featureValue.setNullValue(table.isNullValue(value)); 053 } 054 } 055 056 public void setFeatureValue(String value) throws MaltChainedException { 057 if (table.getSymbolStringToCode(value) < 0) { 058 featureValue.setCode(table.getNullValueCode(NullValueId.NO_NODE)); 059 featureValue.setKnown(table.getKnown(value)); 060 featureValue.setSymbol(value); 061 featureValue.setNullValue(true); 062 } else { 063 featureValue.setCode(table.getSymbolStringToCode(value)); 064 featureValue.setKnown(table.getKnown(value)); 065 featureValue.setSymbol(value); 066 featureValue.setNullValue(table.isNullValue(value)); 067 } 068 } 069 070 public FeatureValue getFeatureValue() { 071 return featureValue; 072 } 073 074 public boolean equals(Object obj) { 075 if (!(obj instanceof TableFeature)) { 076 return false; 077 } 078 if (!obj.toString().equals(this.toString())) { 079 return false; 080 } 081 return true; 082 } 083 084 public String getTableName() { 085 return table.getName(); 086 } 087 088 public String toString() { 089 return table.getName(); 090 } 091 }