001/** 
002 * Copyright (c) 2007-2011, 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.examples.documentclassification.basic;
025
026import java.io.File;
027
028import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
029import org.apache.uima.collection.CollectionReader;
030import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
031import org.apache.uima.fit.factory.AggregateBuilder;
032import org.apache.uima.fit.factory.AnalysisEngineFactory;
033import org.apache.uima.fit.pipeline.SimplePipeline;
034import org.apache.uima.fit.util.JCasUtil;
035import org.apache.uima.jcas.JCas;
036import org.cleartk.examples.type.UsenetDocument;
037import org.cleartk.ml.CleartkAnnotator;
038import org.cleartk.ml.jar.GenericJarClassifierFactory;
039import org.cleartk.ml.jar.JarClassifierBuilder;
040import org.cleartk.opennlp.tools.SentenceAnnotator;
041import org.cleartk.snowball.DefaultSnowballStemmer;
042import org.cleartk.token.tokenizer.TokenAnnotator;
043import org.cleartk.util.ViewUriUtil;
044import org.cleartk.util.ae.UriToDocumentTextAnnotator;
045import org.cleartk.util.cr.UriCollectionReader;
046
047import com.lexicalscope.jewel.cli.CliFactory;
048import com.lexicalscope.jewel.cli.Option;
049
050/**
051 * Copyright (c) 2012, Regents of the University of Colorado <br>
052 * All rights reserved. <br>
053 * 
054 * Illustrates how to run a basic document classification annotator, once it is trained using the
055 * TrainModel class. For a more in-depth example that demonstrates ClearTK best practices including
056 * the use of more sophisticated feature extractors and the evaluation framework refer to the
057 * examples in org.cleartk.examples.document.classification.advanced.
058 * 
059 * @author Lee Becker
060 * 
061 */
062public class RunModel {
063
064  public interface Options {
065    @Option(
066        longName = "test-dir",
067        description = "Specify the directory containing the documents to label.",
068        defaultValue = "data/3news-bydate/test")
069    public File getTestDirectory();
070
071    @Option(
072        longName = "models-dir",
073        description = "specify the directory containing the trained model jar",
074        defaultValue = "target/simple_document_classification/models")
075    public File getModelsDirectory();
076  }
077
078  public static void main(String[] args) throws Exception {
079    Options options = CliFactory.parseArguments(Options.class, args);
080
081    // ////////////////////////////////////////
082    // Create collection reader to load URIs
083    // ////////////////////////////////////////
084    CollectionReader reader = UriCollectionReader.getCollectionReaderFromDirectory(
085        options.getTestDirectory(),
086        UriCollectionReader.RejectSystemFiles.class,
087        UriCollectionReader.RejectSystemDirectories.class);
088
089    // ////////////////////////////////////////
090    // Create document classification pipeline
091    // ////////////////////////////////////////
092    AggregateBuilder builder = new AggregateBuilder();
093
094    // Convert URIs in CAS URI View to Plain Text
095    builder.add(UriToDocumentTextAnnotator.getDescription());
096
097    // NLP pre-processing components
098    builder.add(SentenceAnnotator.getDescription()); // Sentence segmentation
099    builder.add(TokenAnnotator.getDescription()); // Tokenization
100    builder.add(DefaultSnowballStemmer.getDescription("English")); // Stemming
101
102    // Simple document classification annotator
103    builder.add(AnalysisEngineFactory.createEngineDescription(
104        BasicDocumentClassificationAnnotator.class,
105        CleartkAnnotator.PARAM_IS_TRAINING,
106        false,
107        GenericJarClassifierFactory.PARAM_CLASSIFIER_JAR_PATH,
108        JarClassifierBuilder.getModelJarFile(options.getModelsDirectory())));
109
110    // //////////////////////////////////////////////////////////////////////////////
111    // Run pipeline and classify documents
112    // //////////////////////////////////////////////////////////////////////////////
113    SimplePipeline.runPipeline(
114        reader,
115        builder.createAggregateDescription(),
116        AnalysisEngineFactory.createEngineDescription(PrintClassificationsAnnotator.class));
117  }
118
119  public static class PrintClassificationsAnnotator extends JCasAnnotator_ImplBase {
120
121    @Override
122    public void process(JCas jCas) throws AnalysisEngineProcessException {
123      UsenetDocument document = JCasUtil.select(jCas, UsenetDocument.class).iterator().next();
124      System.out.println("classified " + ViewUriUtil.getURI(jCas) + " as " + document.getCategory()
125          + ".");
126    }
127  }
128}