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.parser;
025
026import java.util.List;
027
028import opennlp.tools.postag.POSTagger;
029import opennlp.tools.util.Sequence;
030
031import org.apache.uima.jcas.tcas.Annotation;
032import org.cleartk.opennlp.tools.ParserAnnotator;
033
034import com.google.common.annotations.Beta;
035
036/**
037 * <br>
038 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
039 * All rights reserved.
040 * 
041 * @author Philipp Wetzler, Phili Ogren
042 * 
043 *         <p>
044 *         This class provides a simple implementation of the OpenNLP POSTagger interface. This
045 *         implementation doesn't perform part-of-speech tagging, per se. Instead, it retrieves
046 *         part-of-speech tags from the CAS using the {@link InputTypesHelper} that it is
047 *         instantiated with. This class is not intended for use outside of the
048 *         {@link ParserAnnotator}.
049 */
050@Beta
051public class CasPosTagger<TOKEN_TYPE extends Annotation, SENTENCE_TYPE extends Annotation>
052    implements POSTagger {
053
054  private List<TOKEN_TYPE> tokens;
055
056  InputTypesHelper<TOKEN_TYPE, SENTENCE_TYPE> inputTypesHelper;
057
058  public CasPosTagger(InputTypesHelper<TOKEN_TYPE, SENTENCE_TYPE> inputTypesHelper) {
059    this.inputTypesHelper = inputTypesHelper;
060  }
061
062  public void setTokens(List<TOKEN_TYPE> tokens) {
063    this.tokens = tokens;
064  }
065
066  public Sequence[] topKSequences(List<String> sentence) {
067    return topKSequences(sentence.toArray(new String[sentence.size()]));
068  }
069
070  /**
071   * This method will only return a single sequence corresponding to the part-of-speech tags that
072   * have already been assigned to the tokens in the CAS as part of some upstream processing that
073   * precedes running of the {@link ParserAnnotator}.
074   */
075  public Sequence[] topKSequences(String[] sentence) {
076    Sequence[] s = new Sequence[1];
077
078    s[0] = new Sequence();
079    for (TOKEN_TYPE token : this.tokens) {
080      String pos = inputTypesHelper.getPosTag(token);
081      if (pos == null) {
082        throw new RuntimeException("no part of speech for token: " + token.getCoveredText());
083      }
084      s[0].add(pos, 1.0);
085    }
086
087    return s;
088  }
089
090  public List<String> tag(List<String> sentence) {
091    throw new UnsupportedOperationException();
092  }
093
094  public String[] tag(String[] sentence) {
095    throw new UnsupportedOperationException();
096  }
097
098  public String tag(String sentence) {
099    throw new UnsupportedOperationException();
100  }
101
102  public String[] tag(String[] arg0, Object[] arg1) {
103    throw new UnsupportedOperationException();
104  }
105
106  public Sequence[] topKSequences(String[] arg0, Object[] arg1) {
107    throw new UnsupportedOperationException();
108  }
109
110}