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 public FeatureValue getFeatureValue(int index) {
077 if (index < 0 || index >= size()) {
078 return null;
079 }
080 return get(index).getFeatureValue();
081 }
082
083 public FeatureValue[] getFeatureValues() {
084 final int size = size();
085 FeatureValue[] featureValues = new FeatureValue[size];
086 for (int i = 0; i < size; i++) {
087 featureValues[i] = get(i).getFeatureValue();
088 }
089 return featureValues;
090 }
091
092 /* (non-Javadoc)
093 * @see java.util.AbstractCollection#toString()
094 */
095 public String toString() {
096 final StringBuilder sb = new StringBuilder();
097 for (FeatureFunction function : this) {
098 if (function != null) {
099 sb.append(function.getFeatureValue().toString());
100 sb.append('\n');
101 }
102 }
103 return sb.toString();
104 }
105 }