001package org.maltparser.core.feature; 002 003import java.io.Serializable; 004import java.util.ArrayList; 005 006import org.maltparser.core.exception.MaltChainedException; 007import org.maltparser.core.feature.function.FeatureFunction; 008import org.maltparser.core.feature.spec.SpecificationSubModel; 009import org.maltparser.core.feature.value.FeatureValue; 010 011/** 012* 013* 014* @author Johan Hall 015*/ 016public class FeatureVector extends ArrayList<FeatureFunction> implements Serializable { 017 public final static long serialVersionUID = 3256444702936019250L; 018 private final SpecificationSubModel specSubModel; 019 private final 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 this.specSubModel = _specSubModel; 031 this.featureModel = _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 /** 047 * Returns the feature model that the feature vector belongs to. 048 * 049 * @return the feature model that the feature vector belongs to 050 */ 051 public FeatureModel getFeatureModel() { 052 return featureModel; 053 } 054 055 /** 056 * Updates all feature value in the feature vector according to the current state. 057 * 058 * @throws MaltChainedException 059 */ 060 public void update() throws MaltChainedException { 061 final int size = size(); 062 for (int i = 0; i < size; i++) { 063 get(i).update(); 064 } 065 } 066 067 068 public FeatureValue getFeatureValue(int index) { 069 if (index < 0 || index >= size()) { 070 return null; 071 } 072 return get(index).getFeatureValue(); 073 } 074 075 public FeatureValue[] getFeatureValues() { 076 final int size = size(); 077 FeatureValue[] featureValues = new FeatureValue[size]; 078 for (int i = 0; i < size; i++) { 079 featureValues[i] = get(i).getFeatureValue(); 080 } 081 return featureValues; 082 } 083 084 /* (non-Javadoc) 085 * @see java.util.AbstractCollection#toString() 086 */ 087 public String toString() { 088 final StringBuilder sb = new StringBuilder(); 089 for (FeatureFunction function : this) { 090 if (function != null) { 091 sb.append(function.getFeatureValue().toString()); 092 sb.append('\n'); 093 } 094 } 095 return sb.toString(); 096 } 097}