001/* 002 * Copyright (c) 2012, 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.chunking; 025 026import java.io.File; 027import java.util.Arrays; 028 029import org.apache.uima.analysis_engine.AnalysisEngineProcessException; 030import org.apache.uima.collection.CollectionReader; 031import org.apache.uima.jcas.JCas; 032import org.cleartk.ml.CleartkSequenceAnnotator; 033import org.cleartk.ml.jar.GenericJarClassifierFactory; 034import org.cleartk.ml.jar.JarClassifierBuilder; 035import org.cleartk.ne.type.NamedEntityMention; 036import org.cleartk.opennlp.tools.PosTaggerAnnotator; 037import org.cleartk.opennlp.tools.SentenceAnnotator; 038import org.cleartk.token.tokenizer.TokenAnnotator; 039import org.cleartk.util.ae.UriToDocumentTextAnnotator; 040import org.cleartk.util.cr.UriCollectionReader; 041import org.apache.uima.fit.component.JCasAnnotator_ImplBase; 042import org.apache.uima.fit.factory.AggregateBuilder; 043import org.apache.uima.fit.factory.AnalysisEngineFactory; 044import org.apache.uima.fit.pipeline.SimplePipeline; 045import org.apache.uima.fit.util.JCasUtil; 046 047import com.lexicalscope.jewel.cli.CliFactory; 048import com.lexicalscope.jewel.cli.Option; 049 050/** 051 * This class provides a main method that demonstrates how to run a trained 052 * {@link NamedEntityChunker} on new files. 053 * 054 * <br> 055 * Copyright (c) 2012, Regents of the University of Colorado <br> 056 * All rights reserved. 057 * 058 * @author Steven Bethard 059 */ 060public class RunNamedEntityChunker { 061 062 public interface Options { 063 @Option( 064 longName = "model-dir", 065 description = "The directory where the model was trained", 066 defaultValue = "target/chunking/ne-model") 067 public File getModelDirectory(); 068 069 @Option( 070 longName = "text-file", 071 description = "The file to label with named entities.", 072 defaultValue = "data/sample/2008_Sichuan_earthquake.txt") 073 public File getTextFile(); 074 } 075 076 public static void main(String[] args) throws Exception { 077 Options options = CliFactory.parseArguments(Options.class, args); 078 079 // a reader that loads the URIs of the text file 080 CollectionReader reader = UriCollectionReader.getCollectionReaderFromFiles(Arrays.asList(options.getTextFile())); 081 082 // assemble the classification pipeline 083 AggregateBuilder aggregate = new AggregateBuilder(); 084 085 // an annotator that loads the text from the training file URIs 086 aggregate.add(UriToDocumentTextAnnotator.getDescription()); 087 088 // annotators that identify sentences, tokens and part-of-speech tags in the text 089 aggregate.add(SentenceAnnotator.getDescription()); 090 aggregate.add(TokenAnnotator.getDescription()); 091 aggregate.add(PosTaggerAnnotator.getDescription()); 092 093 // our NamedEntityChunker annotator, configured to classify on the new texts 094 aggregate.add(AnalysisEngineFactory.createEngineDescription( 095 NamedEntityChunker.class, 096 CleartkSequenceAnnotator.PARAM_IS_TRAINING, 097 false, 098 GenericJarClassifierFactory.PARAM_CLASSIFIER_JAR_PATH, 099 JarClassifierBuilder.getModelJarFile(options.getModelDirectory()))); 100 101 // a very simple annotator that just prints out any named entities we found 102 aggregate.add(AnalysisEngineFactory.createEngineDescription(PrintNamedEntityMentions.class)); 103 104 // run the classification pipeline on the new texts 105 SimplePipeline.runPipeline(reader, aggregate.createAggregateDescription()); 106 } 107 108 /** 109 * A simple annotator that just prints out any {@link NamedEntityMention}s in the CAS. 110 * 111 * A real pipeline would probably decide on an appropriate output format and write files instead 112 * of printing to standard output. 113 */ 114 public static class PrintNamedEntityMentions extends JCasAnnotator_ImplBase { 115 116 @Override 117 public void process(JCas jCas) throws AnalysisEngineProcessException { 118 for (NamedEntityMention mention : JCasUtil.select(jCas, NamedEntityMention.class)) { 119 System.out.printf("%s (%s)\n", mention.getCoveredText(), mention.getMentionType()); 120 } 121 } 122 123 } 124}