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.annotations;
031
032import org.openimaj.text.nlp.textpipe.annotations.POSAnnotation.PartOfSpeech;
033import org.openimaj.text.nlp.textpipe.annotations.PhraseAnnotation.Phrase;
034import org.openimaj.text.nlp.textpipe.annotators.MissingRequiredAnnotationException;
035import org.openimaj.text.nlp.textpipe.annotators.OpenNLPPOSAnnotator;
036import org.openimaj.text.nlp.textpipe.annotators.OpenNLPPhraseChunkAnnotator;
037import org.openimaj.text.nlp.textpipe.annotators.OpenNLPSentenceAnnotator;
038import org.openimaj.text.nlp.textpipe.annotators.OpenNLPTokenAnnotator;
039import org.openimaj.text.nlp.textpipe.annotators.YagoNEAnnotator;
040
041/**
042 * Simple demo and play area.
043 * 
044 * @author Laurence Willmore (lgw1e10@ecs.soton.ac.uk)
045 * 
046 */
047public class PipePlayground {
048
049        public static void main(String[] args) {
050                final RawTextAnnotation rta = new RawTextAnnotation(
051                                "The tall curtains");
052
053                // Set the properties
054                System.setProperty(OpenNLPTokenAnnotator.TOKEN_MODEL_PROP, "/org/openimaj/text/opennlp/models/en-token.bin");
055                System.setProperty(OpenNLPSentenceAnnotator.SENTENCE_MODEL_PROP, "/org/openimaj/text/opennlp/models/en-sent.bin");
056                System.setProperty(OpenNLPPOSAnnotator.POS_MODEL_PROP, "/org/openimaj/text/opennlp/models/en-pos-maxent.bin");
057                System.setProperty(OpenNLPPhraseChunkAnnotator.PHRASE_MODEL_PROP,
058                                "/org/openimaj/text/opennlp/models/en-chunker.bin");
059                final OpenNLPTokenAnnotator ta = new OpenNLPTokenAnnotator();
060                final OpenNLPSentenceAnnotator sa = new OpenNLPSentenceAnnotator();
061                final OpenNLPPOSAnnotator pa = new OpenNLPPOSAnnotator();
062                final OpenNLPPhraseChunkAnnotator pca = new OpenNLPPhraseChunkAnnotator();
063                final YagoNEAnnotator yna = new YagoNEAnnotator();
064                try {
065                        sa.annotate(rta);
066                        ta.annotate(rta);
067                        pa.annotate(rta);
068                        pca.annotate(rta);
069                        yna.annotate(rta);
070                } catch (final MissingRequiredAnnotationException e1) {
071                        // TODO Auto-generated catch block
072                        e1.printStackTrace();
073                }
074                for (final SentenceAnnotation sentence : rta
075                                .getAnnotationsFor(SentenceAnnotation.class))
076                {
077                        System.out.println(sentence.text);
078                        if (sentence.getAnnotationKeyList().contains(NamedEntityAnnotation.class))
079                                for (final NamedEntityAnnotation ne : sentence.getAnnotationsFor(NamedEntityAnnotation.class)) {
080                                        System.out.println(ne.namedEntity.rootName);
081                                        System.out.println(ne.namedEntity.type);
082                                }
083                        for (final TokenAnnotation token : sentence.getAnnotationsFor(TokenAnnotation.class)) {
084                                final PartOfSpeech pos = token.getAnnotationsFor(POSAnnotation.class).get(0).pos;
085                                final Phrase ph = token.getAnnotationsFor(PhraseAnnotation.class).get(0).phrase;
086                                final String phraseOrder = token.getAnnotationsFor(PhraseAnnotation.class).get(0).getOrder();
087                                System.out.println(token.stringToken + "  " + pos.toString() + "  " + pos.DESCRIPTION + "    "
088                                                + ph.toString() + "-" + phraseOrder);
089                                System.out.println(sentence.text.substring(token.start, token.stop));
090                                System.out.println("|" + token.getRawString() + "|");
091                                final String fromRaw = rta.text.substring(sentence.start + token.start, sentence.start + token.stop);
092                                System.out.print(fromRaw + "\n");
093                        }
094                }
095        }
096
097}