Search in sources :

Example 1 with HasLemma

use of edu.stanford.nlp.ling.HasLemma in project CoreNLP by stanfordnlp.

the class SpanishXMLTreeReader method buildWordNode.

/**
   * Build a parse tree node corresponding to the word in the given XML node.
   */
private Tree buildWordNode(Node root) {
    Element eRoot = (Element) root;
    String posStr = getPOS(eRoot);
    posStr = treeNormalizer.normalizeNonterminal(posStr);
    String lemma = eRoot.getAttribute(ATTR_LEMMA);
    String word = getWord(eRoot);
    String leafStr = treeNormalizer.normalizeTerminal(word);
    Tree leafNode = treeFactory.newLeaf(leafStr);
    if (leafNode.label() instanceof HasWord)
        ((HasWord) leafNode.label()).setWord(leafStr);
    if (leafNode.label() instanceof HasLemma && lemma != null)
        ((HasLemma) leafNode.label()).setLemma(lemma);
    List<Tree> kids = new ArrayList<>();
    kids.add(leafNode);
    Tree t = treeFactory.newTreeNode(posStr, kids);
    if (t.label() instanceof HasTag)
        ((HasTag) t.label()).setTag(posStr);
    return t;
}
Also used : HasWord(edu.stanford.nlp.ling.HasWord) HasLemma(edu.stanford.nlp.ling.HasLemma) Element(org.w3c.dom.Element) Tree(edu.stanford.nlp.trees.Tree) HasTag(edu.stanford.nlp.ling.HasTag)

Example 2 with HasLemma

use of edu.stanford.nlp.ling.HasLemma in project CoreNLP by stanfordnlp.

the class TreeLemmatizer method transformTree.

@Override
public Tree transformTree(Tree t) {
    Morphology morphology = new Morphology();
    List<TaggedWord> tagged = null;
    int index = 0;
    for (Tree leaf : t.getLeaves()) {
        Label label = leaf.label();
        if (label == null) {
            continue;
        }
        String tag;
        if (!(label instanceof HasTag) || ((HasTag) label).tag() == null) {
            if (tagged == null) {
                tagged = t.taggedYield();
            }
            tag = tagged.get(index).tag();
        } else {
            tag = ((HasTag) label).tag();
        }
        if (!(label instanceof HasLemma)) {
            throw new IllegalArgumentException("Got a tree with labels which do not support lemma");
        }
        ((HasLemma) label).setLemma(morphology.lemma(label.value(), tag, true));
        ++index;
    }
    return t;
}
Also used : HasLemma(edu.stanford.nlp.ling.HasLemma) TaggedWord(edu.stanford.nlp.ling.TaggedWord) Morphology(edu.stanford.nlp.process.Morphology) Label(edu.stanford.nlp.ling.Label) HasTag(edu.stanford.nlp.ling.HasTag)

Aggregations

HasLemma (edu.stanford.nlp.ling.HasLemma)2 HasTag (edu.stanford.nlp.ling.HasTag)2 HasWord (edu.stanford.nlp.ling.HasWord)1 Label (edu.stanford.nlp.ling.Label)1 TaggedWord (edu.stanford.nlp.ling.TaggedWord)1 Morphology (edu.stanford.nlp.process.Morphology)1 Tree (edu.stanford.nlp.trees.Tree)1 Element (org.w3c.dom.Element)1