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 010 /** 011 * 012 * 013 * @author Johan Hall 014 */ 015 public class FeatureVector extends ArrayList<FeatureFunction> implements Serializable { 016 public final static long serialVersionUID = 3256444702936019250L; 017 protected SpecificationSubModel specSubModel; 018 protected FeatureModel featureModel; 019 020 021 /** 022 * Constructs a feature vector 023 * 024 * @param featureModel the parent feature model 025 * @param specSubModel the subspecifiction-model 026 * @throws MaltChainedException 027 */ 028 public FeatureVector(FeatureModel featureModel, SpecificationSubModel specSubModel) throws MaltChainedException { 029 setSpecSubModel(specSubModel); 030 setFeatureModel(featureModel); 031 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 for (int i = 0; i < size(); i++) { 070 get(i).update(); 071 } 072 } 073 074 /** 075 * Updates the cardinality (number of distinct values) of a feature value associated with the feature function 076 * 077 * @throws MaltChainedException 078 */ 079 public void updateCardinality() throws MaltChainedException { 080 for (int i = 0; i < size(); i++) { 081 get(i).updateCardinality(); 082 } 083 } 084 085 086 /* (non-Javadoc) 087 * @see java.util.AbstractCollection#toString() 088 */ 089 public String toString() { 090 StringBuilder sb = new StringBuilder(); 091 for (FeatureFunction function : this) { 092 sb.append(function.getFeatureValue().toString()); 093 sb.append('\n'); 094 } 095 return sb.toString(); 096 } 097 }