001/*
002 * Copyright (c) 2013, 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.liblinear;
025
026import java.util.List;
027import java.util.Map;
028
029import org.cleartk.ml.CleartkProcessingException;
030import org.cleartk.ml.Feature;
031import org.cleartk.ml.encoder.features.FeaturesEncoder;
032import org.cleartk.ml.encoder.outcome.OutcomeEncoder;
033import org.cleartk.ml.jar.Classifier_ImplBase;
034
035import com.google.common.collect.Maps;
036
037import de.bwaldvogel.liblinear.FeatureNode;
038import de.bwaldvogel.liblinear.Linear;
039import de.bwaldvogel.liblinear.Model;
040
041/**
042 * <br>
043 * Copyright (c) 2013, Regents of the University of Colorado <br>
044 * All rights reserved.
045 * 
046 * @author Steven Bethard
047 */
048public class GenericLibLinearClassifier<OUTCOME_TYPE> extends
049    Classifier_ImplBase<FeatureNode[], OUTCOME_TYPE, Integer> {
050
051  private Model model;
052
053  public GenericLibLinearClassifier(
054      FeaturesEncoder<FeatureNode[]> featuresEncoder,
055      OutcomeEncoder<OUTCOME_TYPE, Integer> outcomeEncoder,
056      Model model) {
057    super(featuresEncoder, outcomeEncoder);
058    this.model = model;
059  }
060
061  @Override
062  public OUTCOME_TYPE classify(List<Feature> features) throws CleartkProcessingException {
063    FeatureNode[] encodedFeatures = this.featuresEncoder.encodeAll(features);
064    int encodedOutcome = (int)Linear.predict(this.model, encodedFeatures);
065    return this.outcomeEncoder.decode(encodedOutcome);
066  }
067
068  @Override
069  public Map<OUTCOME_TYPE, Double> score(List<Feature> features) throws CleartkProcessingException {
070    FeatureNode[] encodedFeatures = this.featuresEncoder.encodeAll(features);
071    
072    // get score for each outcome
073    int[] encodedOutcomes = this.model.getLabels();
074    double[] scores = new double[encodedOutcomes.length];
075    if (this.model.isProbabilityModel()) {
076      Linear.predictProbability(this.model, encodedFeatures, scores);
077    } else {
078      Linear.predictValues(this.model, encodedFeatures, scores);
079    }
080    
081    // handle 2-class model, which is special-cased by LIBLINEAR to only return one score
082    if (this.model.getNrClass() == 2 && scores[1] == 0.0) {
083      scores[1] = -scores[0];
084    }
085    
086    // create scored outcome objects
087    Map<OUTCOME_TYPE, Double> scoredOutcomes = Maps.newHashMap();
088    for (int i = 0; i < encodedOutcomes.length; ++i) {
089      OUTCOME_TYPE outcome = this.outcomeEncoder.decode(encodedOutcomes[i]);
090      scoredOutcomes.put(outcome, scores[i]);
091    }
092    return scoredOutcomes;
093  }
094}