PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/cleartk-ml/src/main/java/org/cleartk/classifier/viterbi/DefaultOutcomeFeatureExtractor.java

http://cleartk.googlecode.com/
Java | 177 lines | 116 code | 30 blank | 31 comment | 14 complexity | 02a431ad75dffaff32f74c67d3e0f7b2 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, MIT, Apache-2.0, BSD-2-Clause, CPL-1.0, LGPL-2.1
  1. /**
  2. * Copyright (c) 2009, Regents of the University of Colorado
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  9. * 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.
  10. * 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.
  11. *
  12. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  13. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  16. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  17. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  18. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  19. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  20. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  21. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  22. * POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. package org.cleartk.classifier.viterbi;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import org.apache.uima.UimaContext;
  29. import org.apache.uima.resource.ResourceInitializationException;
  30. import org.cleartk.classifier.Feature;
  31. import org.cleartk.classifier.feature.WindowFeature;
  32. import org.cleartk.classifier.feature.WindowNGramFeature;
  33. import org.cleartk.util.CleartkInitializationException;
  34. import org.uimafit.component.initialize.ConfigurationParameterInitializer;
  35. import org.uimafit.descriptor.ConfigurationParameter;
  36. import org.uimafit.factory.ConfigurationParameterFactory;
  37. /**
  38. * <br>
  39. * Copyright (c) 2009, Regents of the University of Colorado <br>
  40. * All rights reserved.
  41. *
  42. *
  43. * @author Philip Ogren
  44. */
  45. public class DefaultOutcomeFeatureExtractor implements OutcomeFeatureExtractor {
  46. private static final long serialVersionUID = 7476684786572310025L;
  47. public static final String PARAM_MOST_RECENT_OUTCOME = ConfigurationParameterFactory
  48. .createConfigurationParameterName(DefaultOutcomeFeatureExtractor.class, "mostRecentOutcome");
  49. @ConfigurationParameter(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.", defaultValue = "1")
  50. private int mostRecentOutcome = 1;
  51. public static final String PARAM_LEAST_RECENT_OUTCOME = ConfigurationParameterFactory
  52. .createConfigurationParameterName(DefaultOutcomeFeatureExtractor.class, "leastRecentOutcome");
  53. @ConfigurationParameter(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.", defaultValue = "3")
  54. private int leastRecentOutcome = 3;
  55. public static final String PARAM_USE_BIGRAM = ConfigurationParameterFactory
  56. .createConfigurationParameterName(DefaultOutcomeFeatureExtractor.class, "useBigram");
  57. @ConfigurationParameter(description = "when true indicates that bigrams of outcomes should be included as features", defaultValue = "true")
  58. private boolean useBigram = true;
  59. public static final String PARAM_USE_TRIGRAM = ConfigurationParameterFactory
  60. .createConfigurationParameterName(DefaultOutcomeFeatureExtractor.class, "useTrigram");
  61. @ConfigurationParameter(defaultValue = "true", description = "indicates that trigrams of outcomes should be included as features")
  62. private boolean useTrigram = true;
  63. public static final String PARAM_USE4GRAM = ConfigurationParameterFactory
  64. .createConfigurationParameterName(DefaultOutcomeFeatureExtractor.class, "use4gram");
  65. @ConfigurationParameter(defaultValue = "false", description = "indicates that 4-grams of outcomes should be included as features")
  66. private boolean use4gram = false;
  67. public void initialize(UimaContext context) throws ResourceInitializationException {
  68. ConfigurationParameterInitializer.initialize(this, context);
  69. if (mostRecentOutcome < 1) {
  70. throw CleartkInitializationException.parameterLessThan(
  71. PARAM_MOST_RECENT_OUTCOME,
  72. 1,
  73. mostRecentOutcome);
  74. }
  75. if (leastRecentOutcome < mostRecentOutcome) {
  76. throw CleartkInitializationException.parameterLessThan(
  77. PARAM_LEAST_RECENT_OUTCOME,
  78. mostRecentOutcome,
  79. leastRecentOutcome);
  80. }
  81. }
  82. public List<Feature> extractFeatures(List<Object> previousOutcomes) {
  83. if (previousOutcomes == null || previousOutcomes.size() == 0) {
  84. return Collections.emptyList();
  85. }
  86. List<Feature> features = new ArrayList<Feature>();
  87. for (int i = mostRecentOutcome; i <= leastRecentOutcome; i++) {
  88. int index = previousOutcomes.size() - i;
  89. if (index >= 0) {
  90. Feature feature = new WindowFeature(
  91. "PreviousOutcome",
  92. previousOutcomes.get(index),
  93. WindowFeature.ORIENTATION_LEFT,
  94. i,
  95. (Feature) null);
  96. features.add(feature);
  97. }
  98. }
  99. if (useBigram && previousOutcomes.size() >= 2) {
  100. int size = previousOutcomes.size();
  101. String featureValue = previousOutcomes.get(size - 1).toString() + "_"
  102. + previousOutcomes.get(size - 2);
  103. Feature feature = new WindowNGramFeature(
  104. "PreviousOutcomes",
  105. featureValue,
  106. WindowNGramFeature.ORIENTATION_LEFT,
  107. WindowNGramFeature.DIRECTION_LEFT_TO_RIGHT,
  108. "_",
  109. 2,
  110. 1,
  111. (List<Feature>) null);
  112. features.add(feature);
  113. }
  114. if (useTrigram && previousOutcomes.size() >= 3) {
  115. int size = previousOutcomes.size();
  116. String featureValue = previousOutcomes.get(size - 1).toString() + "_"
  117. + previousOutcomes.get(size - 2) + "_" + previousOutcomes.get(size - 3);
  118. Feature feature = new WindowNGramFeature(
  119. "PreviousOutcomes",
  120. featureValue,
  121. WindowNGramFeature.ORIENTATION_LEFT,
  122. WindowNGramFeature.DIRECTION_LEFT_TO_RIGHT,
  123. "_",
  124. 3,
  125. 1,
  126. (List<Feature>) null);
  127. features.add(feature);
  128. }
  129. if (use4gram && previousOutcomes.size() >= 4) {
  130. int size = previousOutcomes.size();
  131. String featureValue = previousOutcomes.get(size - 1).toString() + "_"
  132. + previousOutcomes.get(size - 2) + "_" + previousOutcomes.get(size - 3) + "_"
  133. + previousOutcomes.get(size - 4);
  134. Feature feature = new WindowNGramFeature(
  135. "PreviousOutcomes",
  136. featureValue,
  137. WindowNGramFeature.ORIENTATION_LEFT,
  138. WindowNGramFeature.DIRECTION_LEFT_TO_RIGHT,
  139. "_",
  140. 4,
  141. 1,
  142. (List<Feature>) null);
  143. features.add(feature);
  144. }
  145. return features;
  146. }
  147. }