    001    package calhoun.analysis.crf;
    002
    003    import java.util.List;
    004
    005    import org.apache.commons.lang.StringUtils;
    006
    007    import calhoun.util.Assert;
    008
    009    /** a feature manager that combines it's composite feature types together into a single feature. */
    010    public class ConstrainedFeatureManager extends CompositeFeatureManager {
    011            private static final long serialVersionUID = 5061912595256694050L;
    012
    013            /** Composite feature managers only return 1 feature. */
    014            @Override
    015            public int getNumFeatures() {
    016                    return 1;
    017            }
    018
    019            /** Return a concatenated name */
    020            @Override
    021            public String getFeatureName(int featureIndex) {
    022                    String[] names = new String[allFeatureTypes.size()];
    023                    for(int i=0; i<names.length; ++i) {
    024                            names[i] = allFeatureTypes.get(i).getFeatureName(featureIndex);
    025                    }
    026                    return "Composite: "+StringUtils.join(names, ",");
    027            }
    028
    029            @Override
    030            public void train(int startingIndex, ModelManager modelInfo, List data) {
    031                    Assert.a(allFeatureTypes.size() > 0, "No features types have been assigned.");
    032                    Assert.a(startIndexes == null, "FeatureManager has already been trained.");
    033                    startIx = startingIndex;
    034
    035                    // Train each of the individual FeatureManagers and calculate offsets
    036                    for(int i = 0; i<allFeatureTypes.size(); ++i) {
    037                            FeatureManager fm = allFeatureTypes.get(i);
    038                            List compData = fm.getInputComponent() == null ? data : new ComponentList(data, fm.getInputComponent());
    039                            fm.train(startIx, modelInfo, compData);
    040                            Assert.a(fm.getNumFeatures()==1, "Constrained FeatureManagers must all have 1 feature.  ",fm, " had ", fm.getNumFeatures());
    041                    }
    042            }
    043    }