/** * Set the parse tree of the {@code sentenceId}th sentence. */ public void setScoredParseTree(int sentenceId, Tree<String> tree, Tree<Double> scores) { safeInitializeTrees(); if (!this.getViewName().startsWith("PARSE")) { throw new IllegalStateException("Cannot set a Tree<String> object " + "as the dependency tree." + " Need a Tree<String, Integer> " + "to recover dependency token information. "); } this.trees.set(sentenceId, tree); Tree<Pair<String, IntPair>> spanLabeledTree = ParseUtils.getSpanLabeledTree(tree); int sentenceStart = getSentenceStart(sentenceId); Pair<String, IntPair> rootLabel = spanLabeledTree.getLabel(); IntPair rootSpan = rootLabel.getSecond(); int rootStart = rootSpan.getFirst() + sentenceStart; int rootEnd = rootSpan.getSecond() + sentenceStart; Constituent root = createNewConstituent(rootStart, rootEnd, rootLabel.getFirst(), scores.getLabel()); this.addConstituent(root); addScoredParseTree(spanLabeledTree, scores, root, this.getTextAnnotation().getSentence(sentenceId).getStartSpan()); }
/** * Set the parse tree of the {@code sentenceId}th sentence. */ public void setScoredParseTree(int sentenceId, Tree<String> tree, Tree<Double> scores) { safeInitializeTrees(); if (!this.getViewName().startsWith("PARSE")) { throw new IllegalStateException("Cannot set a Tree<String> object " + "as the dependency tree." + " Need a Tree<String, Integer> " + "to recover dependency token information. "); } this.trees.set(sentenceId, tree); Tree<Pair<String, IntPair>> spanLabeledTree = ParseUtils.getSpanLabeledTree(tree); int sentenceStart = getSentenceStart(sentenceId); Pair<String, IntPair> rootLabel = spanLabeledTree.getLabel(); IntPair rootSpan = rootLabel.getSecond(); int rootStart = rootSpan.getFirst() + sentenceStart; int rootEnd = rootSpan.getSecond() + sentenceStart; Constituent root = createNewConstituent(rootStart, rootEnd, rootLabel.getFirst(), scores.getLabel()); this.addConstituent(root); addScoredParseTree(spanLabeledTree, scores, root, this.getTextAnnotation().getSentence(sentenceId).getStartSpan()); }
/** * Transforms a scored input tree into the constituent-relation graph */ protected void addScoredParseTree(Tree<Pair<String, IntPair>> spanLabeledTree, Tree<Double> scores, Constituent root, int sentenceStartPosition) { for (int childId = 0; childId < spanLabeledTree.getNumberOfChildren(); childId++) { Tree<Pair<String, IntPair>> child = spanLabeledTree.getChild(childId); String edgeLabel; double edgeScore = 0; edgeLabel = PARENT_OF_STRING; double constituentScore = scores.getChild(childId).getLabel(); int start = child.getLabel().getSecond().getFirst() + sentenceStartPosition; int end = child.getLabel().getSecond().getSecond() + sentenceStartPosition; String constituentLabel = child.getLabel().getFirst(); Constituent childConstituent; if (start >= end) { // Ignore constituents with incorrect span bounds logger.debug("Constituent with incorrect span found in " + root.getViewName()); } else { childConstituent = createNewConstituent(start, end, constituentLabel, constituentScore); this.addConstituent(childConstituent); this.addRelation(new Relation(edgeLabel, root, childConstituent, edgeScore)); Tree<Double> scoresChild = scores.getChild(childId); this.addScoredParseTree(child, scoresChild, childConstituent, sentenceStartPosition); } } }
/** * Transforms a scored input tree into the constituent-relation graph */ protected void addScoredParseTree(Tree<Pair<String, IntPair>> spanLabeledTree, Tree<Double> scores, Constituent root, int sentenceStartPosition) { for (int childId = 0; childId < spanLabeledTree.getNumberOfChildren(); childId++) { Tree<Pair<String, IntPair>> child = spanLabeledTree.getChild(childId); String edgeLabel; double edgeScore = 0; edgeLabel = PARENT_OF_STRING; double constituentScore = scores.getChild(childId).getLabel(); int start = child.getLabel().getSecond().getFirst() + sentenceStartPosition; int end = child.getLabel().getSecond().getSecond() + sentenceStartPosition; String constituentLabel = child.getLabel().getFirst(); Constituent childConstituent; if (start >= end) { // Ignore constituents with incorrect span bounds logger.debug("Constituent with incorrect span found in " + root.getViewName()); } else { childConstituent = createNewConstituent(start, end, constituentLabel, constituentScore); this.addConstituent(childConstituent, true); this.addRelation(new Relation(edgeLabel, root, childConstituent, edgeScore)); Tree<Double> scoresChild = scores.getChild(childId); this.addScoredParseTree(child, scoresChild, childConstituent, sentenceStartPosition); } } }