001/** 
002 * Copyright (c) 2009, Regents of the University of Colorado 
003 * All rights reserved.
004 * 
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions are met:
007 * 
008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
011 * 
012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
022 * POSSIBILITY OF SUCH DAMAGE. 
023 */
024package org.cleartk.ml.encoder.features;
025
026import java.io.File;
027import java.io.IOException;
028import java.util.ArrayList;
029import java.util.List;
030
031import org.cleartk.ml.Feature;
032import org.cleartk.ml.encoder.CleartkEncoderException;
033import org.cleartk.ml.encoder.features.normalizer.NameNumberNormalizer;
034import org.cleartk.ml.encoder.features.normalizer.NoOpNormalizer;
035import org.cleartk.ml.util.featurevector.FeatureVector;
036import org.cleartk.ml.util.featurevector.InvalidFeatureVectorValueException;
037import org.cleartk.ml.util.featurevector.SparseFeatureVector;
038import org.cleartk.util.collection.GenericStringMapper;
039import org.cleartk.util.collection.StringMapper;
040import org.cleartk.util.collection.UnknownKeyException;
041import org.cleartk.util.collection.Writable;
042
043/**
044 * <br>
045 * Copyright (c) 2009, Regents of the University of Colorado <br>
046 * All rights reserved.
047 * 
048 * @author Philipp Wetzler
049 */
050public class FeatureVectorFeaturesEncoder extends
051    FeaturesEncoder_ImplBase<FeatureVector, NameNumber> {
052
053  private static final long serialVersionUID = 6714456694285732480L;
054
055  public static final String LOOKUP_FILE_NAME = "features-lookup.txt";
056
057  public FeatureVectorFeaturesEncoder(int cutoff, NameNumberNormalizer normalizer) {
058    this.normalizer = normalizer;
059    this.stringMapper = new GenericStringMapper(cutoff);
060  }
061
062  public FeatureVectorFeaturesEncoder(int cutoff) {
063    this(cutoff, new NoOpNormalizer());
064  }
065
066  public FeatureVectorFeaturesEncoder() {
067    this(0, new NoOpNormalizer());
068  }
069
070  @Override
071  public FeatureVector encodeAll(Iterable<Feature> features) throws CleartkEncoderException {
072    List<NameNumber> fves = new ArrayList<NameNumber>();
073    for (Feature feature : features) {
074      fves.addAll(this.encode(feature));
075    }
076
077    normalizer.normalize(fves);
078
079    SparseFeatureVector fv = new SparseFeatureVector();
080    for (NameNumber fve : fves) {
081      String name = fve.name;
082      Number value = fve.number;
083
084      if (value.doubleValue() == 0.0)
085        continue;
086
087      try {
088        if (expandIndex) {
089          int i = stringMapper.getOrGenerateInteger(name);
090          double v = fv.get(i) + value.doubleValue();
091          fv.set(i, v);
092        } else {
093          try {
094            int i = stringMapper.getInteger(name);
095            double v = fv.get(i) + value.doubleValue();
096            fv.set(i, v);
097          } catch (UnknownKeyException e) {
098          }
099        }
100      } catch (InvalidFeatureVectorValueException e) {
101        throw CleartkEncoderException.invalidFeatureVectorValue(e, e.getIndex(), e.getValue());
102      }
103    }
104
105    return fv;
106  }
107
108  @Override
109  public void finalizeFeatureSet(File outputDirectory) throws IOException {
110    expandIndex = false;
111    stringMapper.finalizeMap();
112
113    if (stringMapper instanceof Writable) {
114      Writable writableMap = (Writable) stringMapper;
115      File outputFile = new File(outputDirectory, LOOKUP_FILE_NAME);
116      writableMap.write(outputFile);
117    }
118  }
119
120  public void setNormalizer(NameNumberNormalizer normalizer) {
121    this.normalizer = normalizer;
122  }
123
124  private boolean expandIndex = true;
125
126  private StringMapper stringMapper;
127
128  private NameNumberNormalizer normalizer;
129
130}