Search in sources :

Example 1 with HasIndex

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

the class Tree method makeDependencyLabel.

/**
   * Convert a constituency label to a dependency label. Options are provided for selecting annotations
   * to copy.
   *
   * @param oldLabel
   * @param copyLabel
   * @param copyIndex
   * @param copyPosTag
   */
private static Label makeDependencyLabel(Label oldLabel, boolean copyLabel, boolean copyIndex, boolean copyPosTag) {
    if (!copyLabel)
        return oldLabel;
    String wordForm = (oldLabel instanceof HasWord) ? ((HasWord) oldLabel).word() : oldLabel.value();
    Label newLabel = oldLabel.labelFactory().newLabel(wordForm);
    if (newLabel instanceof HasWord)
        ((HasWord) newLabel).setWord(wordForm);
    if (copyPosTag && newLabel instanceof HasTag && oldLabel instanceof HasTag) {
        String tag = ((HasTag) oldLabel).tag();
        ((HasTag) newLabel).setTag(tag);
    }
    if (copyIndex && newLabel instanceof HasIndex && oldLabel instanceof HasIndex) {
        int index = ((HasIndex) oldLabel).index();
        ((HasIndex) newLabel).setIndex(index);
    }
    return newLabel;
}
Also used : HasWord(edu.stanford.nlp.ling.HasWord) CoreLabel(edu.stanford.nlp.ling.CoreLabel) Label(edu.stanford.nlp.ling.Label) HasTag(edu.stanford.nlp.ling.HasTag) HasIndex(edu.stanford.nlp.ling.HasIndex)

Example 2 with HasIndex

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

the class CreateClauseDataset method toSpan.

private static Span toSpan(List<? extends HasIndex> chunk) {
    int min = Integer.MAX_VALUE;
    int max = -1;
    for (HasIndex word : chunk) {
        min = Math.min(word.index() - 1, min);
        max = Math.max(word.index(), max);
    }
    assert min >= 0;
    assert max < Integer.MAX_VALUE && max > 0;
    return new Span(min, max);
}
Also used : Span(edu.stanford.nlp.ie.machinereading.structure.Span) HasIndex(edu.stanford.nlp.ling.HasIndex)

Example 3 with HasIndex

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

the class Util method tokensToSpan.

/**
   * Construct the spanning span of the given list of tokens.
   *
   * @param tokens The tokens that should define the span.
   * @return A span (0-indexed) that covers all of the tokens.
   */
public static Span tokensToSpan(List<? extends HasIndex> tokens) {
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    for (HasIndex token : tokens) {
        min = Math.min(token.index() - 1, min);
        max = Math.max(token.index(), max);
    }
    if (min < 0 || max == Integer.MAX_VALUE) {
        throw new IllegalArgumentException("Could not compute span from tokens!");
    } else if (min >= max) {
        throw new IllegalStateException("Either logic is broken or Gabor can't code.");
    } else {
        return new Span(min, max);
    }
}
Also used : Span(edu.stanford.nlp.ie.machinereading.structure.Span) HasIndex(edu.stanford.nlp.ling.HasIndex)

Example 4 with HasIndex

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

the class LeafAncestorEval method makeLineages.

/**
   * Depth-first (post-order) search through the tree, recording the stack state as the
   * lineage every time a terminal is reached.
   *
   * This implementation uses the Index annotation to store depth. If CoreLabels are
   * not present in the trees (or at least something that implements HasIndex), an exception will result.
   *
   * @param t The tree
   * @return A list of lineages
   */
private static List<List<CoreLabel>> makeLineages(final Tree t) {
    if (t == null)
        return null;
    ((HasIndex) t.label()).setIndex(0);
    final Stack<Tree> treeStack = new Stack<>();
    treeStack.push(t);
    final Stack<CoreLabel> labelStack = new Stack<>();
    CoreLabel rootLabel = new CoreLabel(t.label());
    rootLabel.setIndex(0);
    labelStack.push(rootLabel);
    final List<List<CoreLabel>> lineages = new ArrayList<>();
    while (!treeStack.isEmpty()) {
        Tree node = treeStack.pop();
        int nodeDepth = ((HasIndex) node.label()).index();
        while (!labelStack.isEmpty() && labelStack.peek().index() != nodeDepth - 1) labelStack.pop();
        if (node.isPreTerminal()) {
            List<CoreLabel> lin = new ArrayList<>(labelStack);
            lineages.add(lin);
        } else {
            for (Tree kid : node.children()) {
                ((HasIndex) kid.label()).setIndex(nodeDepth + 1);
                treeStack.push(kid);
            }
            CoreLabel nodeLabel = new CoreLabel(node.label());
            nodeLabel.setIndex(nodeDepth);
            labelStack.add(nodeLabel);
        }
    }
    if (DEBUG) {
        System.out.println("Lineages:");
        for (List<CoreLabel> lin : lineages) {
            for (CoreLabel cl : lin) System.out.print(cl.value() + " <- ");
            System.out.println();
        }
    }
    return lineages;
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) ArrayList(java.util.ArrayList) Tree(edu.stanford.nlp.trees.Tree) ArrayList(java.util.ArrayList) List(java.util.List) HasIndex(edu.stanford.nlp.ling.HasIndex) Stack(java.util.Stack)

Example 5 with HasIndex

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

the class Tree method indexLeaves.

/**
   * Assign sequential integer indices to the leaves of the subtree
   * rooted at this <code>Tree</code>, beginning with
   * <code>startIndex</code>, and traversing the leaves from left
   * to right. If node is already indexed, then it uses the existing index.
   * This method only works if the labels of the tree implement
   * CoreLabel!
   *
   * @param startIndex index for this node
   * @param overWrite Whether to replace an existing index for a leaf.
   * @return the next index still unassigned
   */
public int indexLeaves(int startIndex, boolean overWrite) {
    if (isLeaf()) {
        if (label() instanceof HasIndex) {
            HasIndex hi = (HasIndex) label();
            int oldIndex = hi.index();
            if (!overWrite && oldIndex >= 0) {
                startIndex = oldIndex;
            } else {
                hi.setIndex(startIndex);
            }
            startIndex++;
        }
    } else {
        for (Tree kid : children()) {
            startIndex = kid.indexLeaves(startIndex, overWrite);
        }
    }
    return startIndex;
}
Also used : HasIndex(edu.stanford.nlp.ling.HasIndex)

Aggregations

HasIndex (edu.stanford.nlp.ling.HasIndex)9 CoreLabel (edu.stanford.nlp.ling.CoreLabel)5 HasWord (edu.stanford.nlp.ling.HasWord)3 Label (edu.stanford.nlp.ling.Label)3 Span (edu.stanford.nlp.ie.machinereading.structure.Span)2 RuntimeIOException (edu.stanford.nlp.io.RuntimeIOException)2 HasTag (edu.stanford.nlp.ling.HasTag)2 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)1 Tree (edu.stanford.nlp.trees.Tree)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Stack (java.util.Stack)1