001/** 002 * Copyright (c) 2009-2010, 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.io.IOException; 028 029import org.apache.uima.UimaContext; 030import org.apache.uima.UimaContextAdmin; 031import org.apache.uima.fit.component.initialize.ConfigurationParameterInitializer; 032import org.apache.uima.fit.descriptor.ConfigurationParameter; 033import org.apache.uima.fit.factory.initializable.Initializable; 034import org.apache.uima.fit.factory.initializable.InitializableFactory; 035import org.apache.uima.resource.ConfigurationManager; 036import org.apache.uima.resource.ResourceInitializationException; 037import org.cleartk.ml.DataWriter; 038import org.cleartk.ml.DataWriterFactory; 039import org.cleartk.ml.SequenceDataWriter; 040import org.cleartk.ml.SequenceDataWriterFactory; 041import org.cleartk.ml.jar.DirectoryDataWriterFactory; 042 043/** 044 * <br> 045 * Copyright (c) 2009-2010, Regents of the University of Colorado <br> 046 * All rights reserved. 047 * 048 * @author Philip Ogren, Philipp Wetzler 049 * 050 */ 051 052public class ViterbiDataWriterFactory<OUTCOME_TYPE> extends DirectoryDataWriterFactory implements 053 SequenceDataWriterFactory<OUTCOME_TYPE>, Initializable { 054 055 public static final String PARAM_OUTCOME_FEATURE_EXTRACTOR_NAMES = "outcomeFeatureExtractorNames"; 056 057 @ConfigurationParameter( 058 name = PARAM_OUTCOME_FEATURE_EXTRACTOR_NAMES, 059 mandatory = false, 060 description = "An optional, multi-valued, string parameter that " 061 + "specifies which OutcomeFeatureExtractors should be used. " 062 + "Each value of this parameter should be the name of a " 063 + "class that implements OutcomeFeatureExtractor. One valid " 064 + "value that you might use is " 065 + "org.cleartk.ml.feature.extractor.outcome.DefaultOutcomeFeatureExtractor") 066 protected String outcomeFeatureExtractorNames[]; 067 068 public static final String PARAM_DELEGATED_DATA_WRITER_FACTORY_CLASS = "delegatedDataWriterFactoryClass"; 069 070 @ConfigurationParameter( 071 name = PARAM_DELEGATED_DATA_WRITER_FACTORY_CLASS, 072 mandatory = true, 073 defaultValue = "org.cleartk.ml.jar.DefaultDataWriterFactory", 074 description = "A single, required, string parameter that provides " 075 + "the full name of the DataWriterFactory class that will be " + "wrapped.") 076 protected String delegatedDataWriterFactoryClass; 077 078 public void initialize(UimaContext context) throws ResourceInitializationException { 079 super.initialize(context); 080 ConfigurationParameterInitializer.initialize(this, context); 081 082 OutcomeFeatureExtractor outcomeFeatureExtractors[]; 083 if (outcomeFeatureExtractorNames == null) { 084 outcomeFeatureExtractors = new OutcomeFeatureExtractor[0]; 085 } else { 086 outcomeFeatureExtractors = new OutcomeFeatureExtractor[outcomeFeatureExtractorNames.length]; 087 for (int i = 0; i < outcomeFeatureExtractorNames.length; i++) { 088 outcomeFeatureExtractors[i] = InitializableFactory.create( 089 context, 090 outcomeFeatureExtractorNames[i], 091 OutcomeFeatureExtractor.class); 092 } 093 } 094 095 dataWriter = new ViterbiDataWriter<OUTCOME_TYPE>(outputDirectory, outcomeFeatureExtractors); 096 097 // set the output directory parameter to the delegated directory 098 UimaContextAdmin contextAdmin = (UimaContextAdmin) context; 099 ConfigurationManager manager = contextAdmin.getConfigurationManager(); 100 ViterbiClassifierBuilder<OUTCOME_TYPE> builder = dataWriter.getClassifierBuilder(); 101 File delegatedDir = builder.getDelegatedModelDirectory(this.outputDirectory); 102 manager.setConfigParameterValue(contextAdmin.getQualifiedContextName() 103 + DirectoryDataWriterFactory.PARAM_OUTPUT_DIRECTORY, delegatedDir.getPath()); 104 105 // initialize the delegated data writer 106 try { 107 DataWriterFactory<OUTCOME_TYPE> delegatedDataWriterFactory = createDelegatedDataWriterFactory( 108 delegatedDataWriterFactoryClass, 109 context); 110 DataWriter<OUTCOME_TYPE> delegatedDataWriter = delegatedDataWriterFactory.createDataWriter(); 111 dataWriter.setDelegatedDataWriter(delegatedDataWriter); 112 } catch (IOException e) { 113 throw new ResourceInitializationException(e); 114 } 115 116 // restore the output directory parameter 117 finally { 118 manager.setConfigParameterValue(contextAdmin.getQualifiedContextName() 119 + DirectoryDataWriterFactory.PARAM_OUTPUT_DIRECTORY, this.outputDirectory.getPath()); 120 } 121 } 122 123 public SequenceDataWriter<OUTCOME_TYPE> createDataWriter() { 124 return dataWriter; 125 } 126 127 @SuppressWarnings("unchecked") 128 private DataWriterFactory<OUTCOME_TYPE> createDelegatedDataWriterFactory( 129 String delegatedDataWriterFactoryClassName, 130 UimaContext context) throws ResourceInitializationException { 131 return InitializableFactory.create( 132 context, 133 delegatedDataWriterFactoryClassName, 134 DataWriterFactory.class); 135 } 136 137 private ViterbiDataWriter<OUTCOME_TYPE> dataWriter; 138}