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.BufferedOutputStream; 027import java.io.File; 028import java.io.FileOutputStream; 029import java.io.IOException; 030import java.io.ObjectInputStream; 031import java.io.ObjectOutputStream; 032import java.util.jar.JarInputStream; 033import java.util.jar.JarOutputStream; 034import java.util.jar.Manifest; 035 036import org.cleartk.ml.Classifier; 037import org.cleartk.ml.jar.JarClassifierBuilder; 038import org.cleartk.ml.jar.JarStreams; 039import org.cleartk.ml.jar.Train; 040 041/** 042 * <br> 043 * Copyright (c) 2009, Regents of the University of Colorado <br> 044 * All rights reserved. 045 * 046 * @author Philip Ogren 047 * 048 */ 049 050public class ViterbiClassifierBuilder<OUTCOME_TYPE> extends 051 JarClassifierBuilder<ViterbiClassifier<OUTCOME_TYPE>> { 052 053 private static final String OUTCOME_FEATURE_EXTRACTOR_FILE_NAME = "outcome-features-extractors.ser"; 054 055 private static final String DELEGATED_MODEL_DIRECTORY_NAME = "delegated-model"; 056 057 private static final String DELEGATED_MODEL_FILE_NAME = "delegated-model.jar"; 058 059 private static File getOutcomeFeatureExtractorsFile(File dir) { 060 return new File(dir, OUTCOME_FEATURE_EXTRACTOR_FILE_NAME); 061 } 062 063 public File getDelegatedModelDirectory(File dir) { 064 return new File(dir, DELEGATED_MODEL_DIRECTORY_NAME); 065 } 066 067 private OutcomeFeatureExtractor[] outcomeFeatureExtractors; 068 069 public void setOutcomeFeatureExtractors(OutcomeFeatureExtractor[] outcomeFeatureExtractors) { 070 this.outcomeFeatureExtractors = outcomeFeatureExtractors; 071 } 072 073 private Classifier<OUTCOME_TYPE> delegatedClassifier; 074 075 @Override 076 public void saveToTrainingDirectory(File dir) throws IOException { 077 super.saveToTrainingDirectory(dir); 078 ObjectOutputStream extractorsStream = new ObjectOutputStream(new BufferedOutputStream( 079 new FileOutputStream(getOutcomeFeatureExtractorsFile(dir)))); 080 extractorsStream.writeObject(this.outcomeFeatureExtractors); 081 extractorsStream.close(); 082 } 083 084 @Override 085 public void trainClassifier(File dir, String... args) throws Exception { 086 String[] delegatedArgs = new String[args.length + 1]; 087 System.arraycopy(args, 0, delegatedArgs, 1, args.length); 088 delegatedArgs[0] = this.getDelegatedModelDirectory(dir).getPath(); 089 Train.main(delegatedArgs); 090 } 091 092 @Override 093 public void packageClassifier(File dir, JarOutputStream modelStream) throws IOException { 094 super.packageClassifier(dir, modelStream); 095 096 JarStreams.putNextJarEntry( 097 modelStream, 098 DELEGATED_MODEL_FILE_NAME, 099 JarClassifierBuilder.getModelJarFile(this.getDelegatedModelDirectory(dir))); 100 101 JarStreams.putNextJarEntry( 102 modelStream, 103 OUTCOME_FEATURE_EXTRACTOR_FILE_NAME, 104 getOutcomeFeatureExtractorsFile(dir)); 105 } 106 107 @Override 108 public void unpackageClassifier(JarInputStream modelStream) throws IOException { 109 JarStreams.getNextJarEntry(modelStream, DELEGATED_MODEL_FILE_NAME); 110 JarInputStream delegatedModelStream = new JarInputStream(modelStream); 111 Manifest delegatedManifest = delegatedModelStream.getManifest(); 112 JarClassifierBuilder<?> delegatedBuilder = JarClassifierBuilder.fromManifest(delegatedManifest); 113 this.delegatedClassifier = this.cast(delegatedBuilder.loadClassifier(delegatedModelStream)); 114 115 JarStreams.getNextJarEntry(modelStream, OUTCOME_FEATURE_EXTRACTOR_FILE_NAME); 116 ObjectInputStream objectStream = new ObjectInputStream(modelStream); 117 try { 118 this.outcomeFeatureExtractors = (OutcomeFeatureExtractor[]) objectStream.readObject(); 119 } catch (ClassNotFoundException e) { 120 throw new IOException(e); 121 } 122 } 123 124 @Override 125 public ViterbiClassifier<OUTCOME_TYPE> newClassifier() { 126 return new ViterbiClassifier<OUTCOME_TYPE>( 127 this.delegatedClassifier, 128 this.outcomeFeatureExtractors); 129 } 130 131 @SuppressWarnings("unchecked") 132 private Classifier<OUTCOME_TYPE> cast(Object object) { 133 Classifier<OUTCOME_TYPE> classifier = (Classifier<OUTCOME_TYPE>) object; 134 // Can't check that OUTCOME_TYPE matches - ViterbiClassifier is generic and so has no concrete 135 // OUTCOME_TYPE at runtime. But ViterbiClassifier should work with any classifier, so this 136 // should be okay. 137 return classifier; 138 } 139 140}