001 package org.maltparser.core.feature; 002 003 import java.io.Serializable; 004 import java.util.ArrayList; 005 006 import org.maltparser.core.exception.MaltChainedException; 007 import org.maltparser.core.feature.function.FeatureFunction; 008 import org.maltparser.core.feature.spec.SpecificationSubModel; 009 import org.maltparser.core.feature.value.FeatureValue; 010 011 /** 012 * 013 * 014 * @author Johan Hall 015 */ 016 public class FeatureVector extends ArrayList<FeatureFunction> implements Serializable { 017 public final static long serialVersionUID = 3256444702936019250L; 018 protected SpecificationSubModel specSubModel; 019 protected FeatureModel featureModel; 020 021 022 /** 023 * Constructs a feature vector 024 * 025 * @param featureModel the parent feature model 026 * @param specSubModel the subspecifiction-model 027 * @throws MaltChainedException 028 */ 029 public FeatureVector(FeatureModel featureModel, SpecificationSubModel specSubModel) throws MaltChainedException { 030 setSpecSubModel(specSubModel); 031 setFeatureModel(featureModel); 032 for (String spec : specSubModel) { 033 add(featureModel.identifyFeature(spec)); 034 } 035 } 036 037 /** 038 * Returns the subspecifiction-model. 039 * 040 * @return the subspecifiction-model 041 */ 042 public SpecificationSubModel getSpecSubModel() { 043 return specSubModel; 044 } 045 046 protected void setSpecSubModel(SpecificationSubModel specSubModel) { 047 this.specSubModel = specSubModel; 048 } 049 050 /** 051 * Returns the feature model that the feature vector belongs to. 052 * 053 * @return the feature model that the feature vector belongs to 054 */ 055 public FeatureModel getFeatureModel() { 056 return featureModel; 057 } 058 059 protected void setFeatureModel(FeatureModel featureModel) { 060 this.featureModel = featureModel; 061 } 062 063 /** 064 * Updates all feature value in the feature vector according to the current state. 065 * 066 * @throws MaltChainedException 067 */ 068 public void update() throws MaltChainedException { 069 final int size = size(); 070 for (int i = 0; i < size; i++) { 071 get(i).update(); 072 } 073 } 074 075 /** 076 * Updates the cardinality (number of distinct values) of a feature value associated with the feature function 077 * 078 * @throws MaltChainedException 079 */ 080 // public void updateCardinality() throws MaltChainedException { 081 // final int size = size(); 082 // for (int i = 0; i < size; i++) { 083 // get(i).updateCardinality(); 084 // } 085 // } 086 087 public FeatureValue getFeatureValue(int index) { 088 if (index < 0 || index >= size()) { 089 return null; 090 } 091 return get(index).getFeatureValue(); 092 } 093 094 /* (non-Javadoc) 095 * @see java.util.AbstractCollection#toString() 096 */ 097 public String toString() { 098 StringBuilder sb = new StringBuilder(); 099 for (FeatureFunction function : this) { 100 if (function != null) { 101 sb.append(function.getFeatureValue().toString()); 102 sb.append('\n'); 103 } 104 } 105 return sb.toString(); 106 } 107 }