001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.text.nlp.textpipe.annotators; 031 032import java.io.IOException; 033import java.io.InputStream; 034import java.util.ArrayList; 035import java.util.List; 036 037import opennlp.tools.postag.POSModel; 038import opennlp.tools.postag.POSTaggerME; 039 040import org.openimaj.text.nlp.textpipe.annotations.POSAnnotation.PartOfSpeech; 041import org.openimaj.text.nlp.textpipe.annotations.RawTextAnnotation; 042import org.openimaj.text.nlp.textpipe.annotations.SentenceAnnotation; 043 044/** 045 * Uses a {@link POSTaggerME} backed by a {@link POSModel} 046 * 047 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 048 * 049 */ 050public class OpenNLPPOSAnnotator extends AbstractPOSAnnotator { 051 052 /** 053 * Name of system property pointing to the POS model 054 */ 055 public static final String POS_MODEL_PROP = "org.openimaj.text.opennlp.models.pos"; 056 public static final String POS_MODEL_DEFAULT = "/org/openimaj/text/opennlp/models/en-pos-maxent.bin"; 057 POSTaggerME tagger; 058 059 public OpenNLPPOSAnnotator() { 060 super(); 061 InputStream modelIn = null; 062 POSModel model = null; 063 try { 064 modelIn = OpenNLPPOSAnnotator.class 065 .getResourceAsStream(System.getProperty(POS_MODEL_PROP, POS_MODEL_DEFAULT)); 066 model = new POSModel(modelIn); 067 } catch (final IOException e) { 068 // Model loading failed, handle the error 069 e.printStackTrace(); 070 } finally { 071 if (modelIn != null) { 072 try { 073 modelIn.close(); 074 } catch (final IOException e) { 075 } 076 } 077 } 078 tagger = new POSTaggerME(model); 079 } 080 081 @Override 082 public void annotate(RawTextAnnotation annotation) 083 throws MissingRequiredAnnotationException 084 { 085 if (!annotation.getAnnotationKeyList().contains( 086 SentenceAnnotation.class)) 087 throw new MissingRequiredAnnotationException( 088 "No SentenceAnnotations found : OpenNLPPOSAnnotator requires sentance splitting"); 089 super.annotate(annotation); 090 } 091 092 @Override 093 protected List<PartOfSpeech> pos(List<String> tokenList) { 094 final List<PartOfSpeech> result = new ArrayList<PartOfSpeech>(); 095 String[] p = null; 096 final String[] sentence = new String[tokenList.size()]; 097 for (int i = 0; i < sentence.length; i++) { 098 sentence[i] = tokenList.get(i); 099 } 100 p = tagger.tag(sentence); 101 for (final String pos : p) { 102 if (PartOfSpeech.getPOSfromString(pos) == null) 103 System.out.println("no matching pos " + pos); 104 result.add(PartOfSpeech.getPOSfromString(pos)); 105 } 106 return result; 107 } 108 109 @Override 110 void checkForRequiredAnnotations(RawTextAnnotation annotation) 111 throws MissingRequiredAnnotationException 112 { 113 // TODO Auto-generated method stub 114 115 } 116 117}