Example usage for edu.stanford.nlp.process Morphology lemma

List of usage examples for edu.stanford.nlp.process Morphology lemma

Introduction

In this page you can find the example usage for edu.stanford.nlp.process Morphology lemma.

Prototype

public String lemma(String word, String tag) 

Source Link

Usage

From source file:org.knime.ext.textprocessing.nodes.preprocessing.stanfordlemmatizer.StanfordLemmatizer.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w ww  .ja v a 2 s .  c om*/
 */
@Override
public Term preprocessTerm(final Term term) {

    Morphology morph = new Morphology();
    final List<Tag> tags = term.getTags();
    String tag = "";
    // if term doesn't have any tags
    if (tags.isEmpty()) {
        // either skip or throw an exception
        if (!m_skipTerms) {
            m_warnMessage.set("Warning: Some terms have no POS tags.");
            return term;
        } else {
            throw new RuntimeException("Some terms have no POS tags.");
        }

    }
    // take the first POS tag found that is not UNKNOWN
    for (Tag elem : tags) {
        if (elem.getTagType().equals("POS") && !elem.getTagValue().equals("UNKNOWN")) {
            tag = elem.getTagValue();
            break;
        }
    }
    // also skip if no POS tag is found
    if (tag.isEmpty()) {
        return term;
    }

    final List<Word> words = term.getWords();
    final List<Word> newWords = new ArrayList<Word>();
    for (final Word w : words) {
        newWords.add(new Word(morph.lemma(w.getWord(), tag), w.getWhitespaceSuffix()));
    }
    return new Term(newWords, term.getTags(), term.isUnmodifiable());
}