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.corpus.penntreebank;
025
026import java.util.List;
027
028import org.apache.uima.UimaContext;
029import org.apache.uima.analysis_engine.AnalysisEngineDescription;
030import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
031import org.apache.uima.cas.CAS;
032import org.apache.uima.cas.CASException;
033import org.apache.uima.jcas.JCas;
034import org.apache.uima.resource.ResourceInitializationException;
035import org.cleartk.syntax.constituent.type.TerminalTreebankNode;
036import org.cleartk.syntax.constituent.type.TopTreebankNode;
037import org.cleartk.token.type.Sentence;
038import org.cleartk.token.type.Token;
039import org.cleartk.util.treebank.TreebankFormatParser;
040import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
041import org.apache.uima.fit.descriptor.ConfigurationParameter;
042import org.apache.uima.fit.descriptor.SofaCapability;
043import org.apache.uima.fit.factory.AnalysisEngineFactory;
044import org.apache.uima.fit.util.JCasUtil;
045
046/**
047 * <br>
048 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
049 * All rights reserved.
050 * 
051 * 
052 * <p>
053 * The TreebankFormatParser AnalysisEngine populates the "_InitialView" SOFA from the "TreebankView"
054 * SOFA, creating all the appropriate Treebank annotations.
055 * </p>
056 * 
057 * @author Philipp Wetzler
058 */
059
060@SofaCapability(
061    inputSofas = { PennTreebankReader.TREEBANK_VIEW, CAS.NAME_DEFAULT_SOFA },
062    outputSofas = {})
063public class TreebankGoldAnnotator extends JCasAnnotator_ImplBase {
064
065  public static AnalysisEngineDescription getDescription() throws ResourceInitializationException {
066    return AnalysisEngineFactory.createEngineDescription(TreebankGoldAnnotator.class);
067  }
068
069  public static AnalysisEngineDescription getDescriptionPOSTagsOnly()
070      throws ResourceInitializationException {
071    return AnalysisEngineFactory.createEngineDescription(
072        TreebankGoldAnnotator.class,
073        TreebankGoldAnnotator.PARAM_POST_TREES,
074        false);
075  }
076
077  public static final String PARAM_POST_TREES = "postTrees";
078
079  private static final String POST_TREES_DESCRIPTION = "specifies whether or not to post trees (i.e. annotations of type TreebankNode) to the CAS.  "
080      + "Sometimes treebank data is used only for the part-of-speech data that it contains.  "
081      + "For such uses, it is not necessary to post the entire constituent parse to the CAS. "
082      + "Instead, this parameter can be set to false which results in  only the part-of-speech data being added.";
083
084  @ConfigurationParameter(
085      name = PARAM_POST_TREES,
086      description = POST_TREES_DESCRIPTION,
087      mandatory = false,
088      defaultValue = "true")
089  private boolean postTrees;
090
091  @Override
092  public void initialize(UimaContext context) throws ResourceInitializationException {
093    super.initialize(context);
094  }
095
096  @Override
097  public void process(JCas jCas) throws AnalysisEngineProcessException {
098    JCas docView;
099    String tbText;
100    try {
101      docView = jCas.getView(CAS.NAME_DEFAULT_SOFA);
102      tbText = jCas.getView(PennTreebankReader.TREEBANK_VIEW).getDocumentText();
103    } catch (CASException e) {
104      throw new AnalysisEngineProcessException(e);
105    }
106    String docText = docView.getDocumentText();
107
108    if (docText == null) {
109      docText = TreebankFormatParser.inferPlainText(tbText);
110      docView.setSofaDataString(docText, "text/plain");
111    }
112    List<org.cleartk.util.treebank.TopTreebankNode> topNodes;
113    topNodes = TreebankFormatParser.parseDocument(tbText, 0, docText);
114
115    for (org.cleartk.util.treebank.TopTreebankNode topNode : topNodes) {
116      TopTreebankNode uimaNode = org.cleartk.corpus.penntreebank.TreebankNodeConverter.convert(
117          topNode,
118          docView,
119          postTrees);
120      Sentence uimaSentence = new Sentence(docView, uimaNode.getBegin(), uimaNode.getEnd());
121      uimaSentence.addToIndexes();
122
123      int tokenIndex = 0;
124      for (TerminalTreebankNode terminal : JCasUtil.select(
125          uimaNode.getTerminals(),
126          TerminalTreebankNode.class)) {
127        if (terminal.getBegin() != terminal.getEnd()) {
128          terminal.setTokenIndex(tokenIndex++);
129          Token uimaToken = new Token(docView, terminal.getBegin(), terminal.getEnd());
130          uimaToken.setPos(terminal.getNodeType());
131          uimaToken.addToIndexes();
132        } else {
133          terminal.setTokenIndex(-1);
134        }
135      }
136    }
137  }
138}