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 */
024
025package org.cleartk.ml.viterbi;
026
027import java.util.ArrayList;
028import java.util.Collections;
029import java.util.List;
030
031import org.apache.uima.UimaContext;
032import org.apache.uima.resource.ResourceInitializationException;
033import org.cleartk.ml.Feature;
034import org.cleartk.util.CleartkInitializationException;
035import org.apache.uima.fit.component.initialize.ConfigurationParameterInitializer;
036import org.apache.uima.fit.descriptor.ConfigurationParameter;
037
038/**
039 * <br>
040 * Copyright (c) 2009, Regents of the University of Colorado <br>
041 * All rights reserved.
042 * 
043 * 
044 * @author Philip Ogren
045 */
046
047public class DefaultOutcomeFeatureExtractor implements OutcomeFeatureExtractor {
048
049  private static final long serialVersionUID = 7476684786572310025L;
050
051  public static final String PARAM_MOST_RECENT_OUTCOME = "mostRecentOutcome";
052
053  @ConfigurationParameter(
054      name = PARAM_MOST_RECENT_OUTCOME,
055      description = "indicates the position of the first (most recent) outcome to include. For example, the default value of 1 means that if the outcomes produced so far by the classifier were [A, B, C, D], then the first outcome to be used as a feature would be D since it is the most recent.",
056      defaultValue = "1")
057  private int mostRecentOutcome = 1;
058
059  public static final String PARAM_LEAST_RECENT_OUTCOME = "leastRecentOutcome";
060
061  @ConfigurationParameter(
062      name = PARAM_LEAST_RECENT_OUTCOME,
063      description = "indicates the position of the last (least recent) outcome to include. For example, the default value of 3 means that if the outcomes produced so far by the classifier were [A, B, C, D], then the last outcome to be used as a feature would be B since and is considered the least recent.",
064      defaultValue = "3")
065  private int leastRecentOutcome = 3;
066
067  public static final String PARAM_USE_BIGRAM = "useBigram";
068
069  @ConfigurationParameter(
070      name = PARAM_USE_BIGRAM,
071      description = "when true indicates that bigrams of outcomes should be included as features",
072      defaultValue = "true")
073  private boolean useBigram = true;
074
075  public static final String PARAM_USE_TRIGRAM = "useTrigram";
076
077  @ConfigurationParameter(
078      name = PARAM_USE_TRIGRAM,
079      defaultValue = "true",
080      description = "indicates that trigrams of outcomes should be included as features")
081  private boolean useTrigram = true;
082
083  public static final String PARAM_USE4GRAM = "use4gram";
084
085  @ConfigurationParameter(
086      name = PARAM_USE4GRAM,
087      defaultValue = "false",
088      description = "indicates that 4-grams of outcomes should be included as features")
089  private boolean use4gram = false;
090
091  public void initialize(UimaContext context) throws ResourceInitializationException {
092    ConfigurationParameterInitializer.initialize(this, context);
093
094    if (mostRecentOutcome < 1) {
095      throw CleartkInitializationException.parameterLessThan(
096          PARAM_MOST_RECENT_OUTCOME,
097          1,
098          mostRecentOutcome);
099    }
100
101    if (leastRecentOutcome < mostRecentOutcome) {
102      throw CleartkInitializationException.parameterLessThan(
103          PARAM_LEAST_RECENT_OUTCOME,
104          mostRecentOutcome,
105          leastRecentOutcome);
106    }
107
108  }
109
110  public List<Feature> extractFeatures(List<Object> previousOutcomes) {
111    if (previousOutcomes == null || previousOutcomes.size() == 0) {
112      return Collections.emptyList();
113    }
114
115    List<Feature> features = new ArrayList<Feature>();
116
117    for (int i = mostRecentOutcome; i <= leastRecentOutcome; i++) {
118      int index = previousOutcomes.size() - i;
119      if (index >= 0) {
120        Feature feature = new Feature("PreviousOutcome_L" + i, previousOutcomes.get(index));
121        features.add(feature);
122      }
123    }
124
125    if (useBigram && previousOutcomes.size() >= 2) {
126      int size = previousOutcomes.size();
127      String featureValue = previousOutcomes.get(size - 1).toString() + "_"
128          + previousOutcomes.get(size - 2);
129      Feature feature = new Feature("PreviousOutcomes_L1_2gram_L2R", featureValue);
130      features.add(feature);
131    }
132
133    if (useTrigram && previousOutcomes.size() >= 3) {
134      int size = previousOutcomes.size();
135      String featureValue = previousOutcomes.get(size - 1).toString() + "_"
136          + previousOutcomes.get(size - 2) + "_" + previousOutcomes.get(size - 3);
137      Feature feature = new Feature("PreviousOutcomes_L1_3gram_L2R", featureValue);
138      features.add(feature);
139    }
140
141    if (use4gram && previousOutcomes.size() >= 4) {
142      int size = previousOutcomes.size();
143      String featureValue = previousOutcomes.get(size - 1).toString() + "_"
144          + previousOutcomes.get(size - 2) + "_" + previousOutcomes.get(size - 3) + "_"
145          + previousOutcomes.get(size - 4);
146      Feature feature = new Feature("PreviousOutcomes_L1_4gram_L2R", featureValue);
147      features.add(feature);
148    }
149
150    return features;
151  }
152
153}