001/** 
002 * Copyright (c) 2007-2008, 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.opennlp.tools;
025
026import java.io.IOException;
027import java.io.InputStream;
028import java.util.List;
029
030import opennlp.tools.postag.POSModel;
031import opennlp.tools.postag.POSTagger;
032import opennlp.tools.postag.POSTaggerME;
033
034import org.apache.uima.UimaContext;
035import org.apache.uima.analysis_engine.AnalysisEngineDescription;
036import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
037import org.apache.uima.jcas.JCas;
038import org.apache.uima.resource.ResourceInitializationException;
039import org.cleartk.token.type.Sentence;
040import org.cleartk.token.type.Token;
041import org.cleartk.util.IoUtil;
042import org.cleartk.util.ParamUtil;
043import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
044import org.apache.uima.fit.descriptor.ConfigurationParameter;
045import org.apache.uima.fit.descriptor.TypeCapability;
046import org.apache.uima.fit.factory.AnalysisEngineFactory;
047import org.apache.uima.fit.util.JCasUtil;
048
049import com.google.common.annotations.Beta;
050
051/**
052 * <br>
053 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
054 * All rights reserved.
055 * 
056 * 
057 * @author Philip Ogren
058 * 
059 */
060@Beta
061@TypeCapability(inputs = { "org.cleartk.token.type.Sentence", "org.cleartk.token.type.Token" })
062public class PosTaggerAnnotator extends JCasAnnotator_ImplBase {
063
064  // public static AnalysisEngineDescription getDescription() throws ResourceInitializationException
065  // {
066  // AnalysisEngineDescription aed = AnalysisEngineFactory.createEngineDescription(
067  // opennlp.uima.postag.POSTagger.class,
068  // SyntaxComponents.TYPE_SYSTEM_DESCRIPTION,
069  // UimaUtil.TOKEN_TYPE_PARAMETER, Token.class.getName(),
070  // UimaUtil.POS_FEATURE_PARAMETER, "pos",
071  // UimaUtil.SENTENCE_TYPE_PARAMETER, Sentence.class.getName());
072  //
073  // this is wrong!
074  // ExternalResourceDescription erd =
075  // ExternalResourceFactory.createExternalResourceDescription("opennlp.uima.ModelName",
076  // POSModel.class,"/models/en-pos-maxent.bin");
077  //
078  // try {
079  // bindResource(aed, "opennlp.uima.ModelName", erd);
080  // } catch (InvalidXMLException e) {
081  // // TODO Auto-generated catch block
082  // throw new ResourceInitializationException(e);
083  // }
084  // return aed;
085  // }
086
087  public static AnalysisEngineDescription getDescription() throws ResourceInitializationException {
088    return AnalysisEngineFactory.createEngineDescription(
089        PosTaggerAnnotator.class,
090        PARAM_POSTAG_MODEL_FILE,
091        ParamUtil.getParameterValue(PARAM_POSTAG_MODEL_FILE, "/models/en-pos-maxent.bin"));
092  }
093
094  public static final String PARAM_POSTAG_MODEL_FILE = "postagModelFile";
095
096  @ConfigurationParameter(
097      name = PARAM_POSTAG_MODEL_FILE,
098      mandatory = true,
099      description = "provides the path of the OpenNLP part-of-speech tagger model file, e.g.  resources/models/OpenNLP.POSTags.English.bin.gz.  See javadoc for opennlp.maxent.io.SuffixSensitiveGISModelReader.")
100  private String postagModelFile;
101
102  protected POSTagger posTagger;
103
104  @Override
105  public void initialize(UimaContext uimaContext) throws ResourceInitializationException {
106    super.initialize(uimaContext);
107    try {
108      InputStream modelInputStream = IoUtil.getInputStream(
109          PosTaggerAnnotator.class,
110          postagModelFile);
111      POSModel posModel = new POSModel(modelInputStream);
112      posTagger = new POSTaggerME(posModel);
113    } catch (IOException ioe) {
114      throw new ResourceInitializationException(ioe);
115    }
116  }
117
118  @Override
119  public void process(JCas jCas) throws AnalysisEngineProcessException {
120    for (Sentence sentence : JCasUtil.select(jCas, Sentence.class)) {
121      List<Token> tokens = JCasUtil.selectCovered(jCas, Token.class, sentence);
122      String[] tokenTexts = new String[tokens.size()];
123      for (int i = 0; i < tokenTexts.length; ++i) {
124        tokenTexts[i] = tokens.get(i).getCoveredText();
125      }
126      String[] tags = posTagger.tag(tokenTexts);
127      for (int i = 0; i < tags.length; ++i) {
128        tokens.get(i).setPos(tags[i]);
129      }
130    }
131  }
132}