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.viterbi; 025 026import java.io.File; 027import java.lang.reflect.Type; 028import java.util.ArrayList; 029import java.util.List; 030import java.util.Map; 031 032import org.cleartk.ml.CleartkProcessingException; 033import org.cleartk.ml.DataWriter; 034import org.cleartk.ml.Feature; 035import org.cleartk.ml.Instance; 036import org.cleartk.ml.SequenceDataWriter; 037import org.cleartk.ml.jar.DirectoryDataWriter; 038import org.cleartk.util.ReflectionUtil; 039import org.cleartk.util.ReflectionUtil.TypeArgumentDelegator; 040 041/** 042 * <br> 043 * Copyright (c) 2009, Regents of the University of Colorado <br> 044 * All rights reserved. 045 * <p> 046 */ 047 048public class ViterbiDataWriter<OUTCOME_TYPE> extends 049 DirectoryDataWriter<ViterbiClassifierBuilder<OUTCOME_TYPE>, ViterbiClassifier<OUTCOME_TYPE>> 050 implements SequenceDataWriter<OUTCOME_TYPE>, TypeArgumentDelegator { 051 052 public ViterbiDataWriter(File outputDirectory, OutcomeFeatureExtractor outcomeFeatureExtractors[]) { 053 super(outputDirectory); 054 this.outcomeFeatureExtractors = outcomeFeatureExtractors; 055 this.classifierBuilder.setOutcomeFeatureExtractors(this.outcomeFeatureExtractors); 056 } 057 058 @Override 059 protected ViterbiClassifierBuilder<OUTCOME_TYPE> newClassifierBuilder() { 060 return new ViterbiClassifierBuilder<OUTCOME_TYPE>(); 061 } 062 063 public void setDelegatedDataWriter(DataWriter<OUTCOME_TYPE> delegatedDataWriter) { 064 this.delegatedDataWriter = delegatedDataWriter; 065 } 066 067 public void write(List<Instance<OUTCOME_TYPE>> instances) throws CleartkProcessingException { 068 if (this.delegatedDataWriter == null) 069 throw new IllegalStateException( 070 "delegatedDataWriter must be set before calling writeSequence"); 071 072 List<Object> outcomes = new ArrayList<Object>(); 073 for (Instance<OUTCOME_TYPE> instance : instances) { 074 List<Feature> instanceFeatures = instance.getFeatures(); 075 for (OutcomeFeatureExtractor outcomeFeatureExtractor : outcomeFeatureExtractors) { 076 instanceFeatures.addAll(outcomeFeatureExtractor.extractFeatures(outcomes)); 077 } 078 outcomes.add(instance.getOutcome()); 079 delegatedDataWriter.write(instance); 080 } 081 082 } 083 084 public void finish() throws CleartkProcessingException { 085 if (this.delegatedDataWriter == null) 086 throw new IllegalStateException("delegatedDataWriter must be set before calling finish"); 087 088 this.delegatedDataWriter.finish(); 089 super.finish(); 090 } 091 092 public Map<String, Type> getTypeArguments(Class<?> genericType) { 093 if (this.delegatedDataWriter == null) 094 throw new IllegalStateException( 095 "delegatedDataWriter must be set before calling getTypeArguments"); 096 097 if (genericType.equals(SequenceDataWriter.class)) { 098 genericType = DataWriter.class; 099 } 100 return ReflectionUtil.getTypeArguments(genericType, this.delegatedDataWriter); 101 } 102 103 protected File outputDirectory; 104 105 protected OutcomeFeatureExtractor outcomeFeatureExtractors[]; 106 107 protected DataWriter<OUTCOME_TYPE> delegatedDataWriter = null; 108}