001    /*
002     * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003     *
004     * This file is part of the Java Data Mining Package (JDMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * JDMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * JDMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with JDMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.jdmp.stanfordpos;
025    
026    import java.io.File;
027    import java.io.StringReader;
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    import org.jdmp.core.algorithm.tagger.AbstractTagger;
032    import org.ujmp.core.Matrix;
033    
034    import edu.stanford.nlp.ling.HasWord;
035    import edu.stanford.nlp.ling.Sentence;
036    import edu.stanford.nlp.ling.TaggedWord;
037    import edu.stanford.nlp.ling.Word;
038    import edu.stanford.nlp.tagger.maxent.MaxentTagger;
039    
040    public class StanfordPOSTagger extends AbstractTagger {
041            private static final long serialVersionUID = -2655534427624643477L;
042    
043            public static final String MODELPATH = System.getProperty("user.home")
044                            + "/datasets/models/pos";
045    
046            public static final String DEFAULTMODEL = "left3words-wsj-0-18.tagger";
047    
048            private MaxentTagger tagger = null;
049    
050            private static StanfordPOSTagger instance = null;
051    
052            public StanfordPOSTagger() throws Exception {
053                    this(new File(MODELPATH + "/" + DEFAULTMODEL));
054            }
055    
056            public StanfordPOSTagger(File modelFile) throws Exception {
057                    if (!modelFile.exists()) {
058                            throw new Exception("model file not found: " + modelFile);
059                    }
060                    tagger = new MaxentTagger(modelFile.getAbsolutePath());
061            }
062    
063            @Override
064            public List<Matrix> tag(String input) throws Exception {
065                    List<Matrix> list = new ArrayList<Matrix>();
066    
067                    StringReader sr = new StringReader(input);
068                    List<Sentence<? extends HasWord>> sentences = MaxentTagger
069                                    .tokenizeText(sr);
070    
071                    for (Sentence<? extends HasWord> sentence : sentences) {
072                            Sentence<TaggedWord> tSentence = tagger.processSentence(sentence);
073                            Matrix m = new StanfordSentenceMatrix(tSentence);
074                            list.add(m);
075                    }
076    
077                    return list;
078            }
079    
080            @Override
081            public List<Matrix> tag(Matrix input) throws Exception {
082                    List<Matrix> list = new ArrayList<Matrix>();
083    
084                    Sentence<? extends HasWord> sentence = null;
085                    if (input instanceof StanfordSentenceMatrix) {
086                            sentence = ((StanfordSentenceMatrix) input).getWrappedObject();
087                    } else {
088                            List<HasWord> words = new ArrayList<HasWord>();
089                            for (long[] c : input.availableCoordinates()) {
090                                    String s = input.getAsString(c);
091                                    words.add(new Word(s));
092                            }
093                            sentence = new Sentence<HasWord>(words);
094                    }
095                    Sentence<TaggedWord> tSentence = tagger.processSentence(sentence);
096                    Matrix m = new StanfordSentenceMatrix(tSentence);
097                    list.add(m);
098    
099                    return list;
100            }
101    
102            public static StanfordPOSTagger getInstance() {
103                    if (instance == null) {
104                            try {
105                                    instance = new StanfordPOSTagger();
106                            } catch (Exception e) {
107                                    e.printStackTrace();
108                            }
109                    }
110                    return instance;
111            }
112    
113    }