getConstituentRelativeToSentenceStart(depTree, treeScore, sentenceStart, "ROOT"); this.addConstituent(root);
getConstituentRelativeToSentenceStart(depTree, treeScore, sentenceStart, "ROOT"); this.addConstituent(root);
protected void addDependencyTree(Tree<Pair<String, Integer>> depTree, int sentStart, Constituent parent) { String word = depTree.getLabel().getFirst(); String token = this.getTextAnnotation().getToken(parent.getStartSpan()); word = treebankTokenHacks(word); token = treebankTokenHacks(token); assert word.trim().length() > 0; assert token.trim().length() > 0; if (!word.equals(token)) { logger.error(parent.getTextAnnotation().toString()); logger.error(depTree.toString()); throw new IllegalStateException("Expecting " + token + ", found " + word + " instead while constructing the dependency tree"); } for (int i = 0; i < depTree.getNumberOfChildren(); i++) { String relationLabel = depTree.getEdgeLabel(i).getFirst(); Tree<Pair<String, Integer>> child = depTree.getChild(i); Constituent childConstituent = getConstituentRelativeToSentenceStart(child, 1.0, sentStart, relationLabel); this.addConstituent(childConstituent); this.addRelation(new Relation(relationLabel, parent, childConstituent, 1.0)); addDependencyTree(child, sentStart, childConstituent); } }
protected void addDependencyTree(Tree<Pair<String, Integer>> depTree, int sentStart, Constituent parent) { String word = depTree.getLabel().getFirst(); String token = this.getTextAnnotation().getToken(parent.getStartSpan()); word = treebankTokenHacks(word); token = treebankTokenHacks(token); assert word.trim().length() > 0; assert token.trim().length() > 0; if (!word.equals(token)) { logger.error(parent.getTextAnnotation().toString()); logger.error(depTree.toString()); throw new IllegalStateException("Expecting " + token + ", found " + word + " instead while constructing the dependency tree"); } for (int i = 0; i < depTree.getNumberOfChildren(); i++) { String relationLabel = depTree.getEdgeLabel(i).getFirst(); Tree<Pair<String, Integer>> child = depTree.getChild(i); Constituent childConstituent = getConstituentRelativeToSentenceStart(child, 1.0, sentStart, relationLabel); this.addConstituent(childConstituent, true); this.addRelation(new Relation(relationLabel, parent, childConstituent, 1.0)); addDependencyTree(child, sentStart, childConstituent); } }
protected void addDependencyTreeWithHack(Tree<Pair<String, Integer>> depTree, Constituent parent, int sentenceStart) { String word = depTree.getLabel().getFirst(); String token = this.getTextAnnotation().getToken(parent.getStartSpan()); word = treebankTokenHacks(word); token = treebankTokenHacks(token); assert word.trim().length() > 0; assert token.trim().length() > 0; if (!word.equals(token)) { logger.info(parent.getTextAnnotation().toString()); logger.info(depTree.toString()); throw new IllegalStateException("Expecting " + token + ", found " + word + " instead while constructing the dependency tree"); } for (int i = 0; i < depTree.getNumberOfChildren(); i++) { String relationLabel = depTree.getEdgeLabel(i).getFirst(); Tree<Pair<String, Integer>> child = depTree.getChild(i); Constituent childConstituent = getConstituentRelativeToSentenceStart(child, 1.0, sentenceStart, relationLabel); this.addConstituent(childConstituent, true); this.addRelation(new Relation(relationLabel, parent, childConstituent, 1.0)); addDependencyTreeWithHack(child, childConstituent, sentenceStart); } }
protected void addDependencyTreeWithHack(Tree<Pair<String, Integer>> depTree, Constituent parent, int sentenceStart) { String word = depTree.getLabel().getFirst(); String token = this.getTextAnnotation().getToken(parent.getStartSpan()); word = treebankTokenHacks(word); token = treebankTokenHacks(token); assert word.trim().length() > 0; assert token.trim().length() > 0; if (!word.equals(token)) { logger.info(parent.getTextAnnotation().toString()); logger.info(depTree.toString()); throw new IllegalStateException("Expecting " + token + ", found " + word + " instead while constructing the dependency tree"); } for (int i = 0; i < depTree.getNumberOfChildren(); i++) { String relationLabel = depTree.getEdgeLabel(i).getFirst(); Tree<Pair<String, Integer>> child = depTree.getChild(i); Constituent childConstituent = getConstituentRelativeToSentenceStart(child, 1.0, sentenceStart, relationLabel); this.addConstituent(childConstituent); this.addRelation(new Relation(relationLabel, parent, childConstituent, 1.0)); addDependencyTreeWithHack(child, childConstituent, sentenceStart); } }
/** * Set the parse tree of the {@code sentenceId}<sup>th</sup> sentence. * <p> * <b>Note</b>: The same TreeView cannot contain both the parse tree and the dependency tree and * will throw an exception if an attempt is made to set the parse tree in a view that has a * dependency tree. */ public void setParseTree(int sentenceId, Tree<String> tree) { safeInitializeTrees(); if (firstTree) { firstTree = false; this.isDependencyTree = false; } if (this.isDependencyTree) { throw new IllegalArgumentException("Not expecting a dependency tree, but found " + tree); } this.trees.set(sentenceId, tree); int sentenceStart = getSentenceStart(sentenceId); Tree<Pair<String, IntPair>> spanLabeledTree = ParseUtils.getSpanLabeledTree(tree); 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(), 1.0); this.addConstituent(root); addParseTree(spanLabeledTree, root, sentenceStart); }
/** * Set the parse tree of the {@code sentenceId}<sup>th</sup> sentence. * <p> * <b>Note</b>: The same TreeView cannot contain both the parse tree and the dependency tree and * will throw an exception if an attempt is made to set the parse tree in a view that has a * dependency tree. */ public void setParseTree(int sentenceId, Tree<String> tree) { safeInitializeTrees(); if (firstTree) { firstTree = false; this.isDependencyTree = false; } if (this.isDependencyTree) { throw new IllegalArgumentException("Not expecting a dependency tree, but found " + tree); } this.trees.set(sentenceId, tree); int sentenceStart = getSentenceStart(sentenceId); Tree<Pair<String, IntPair>> spanLabeledTree = ParseUtils.getSpanLabeledTree(tree); 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(), 1.0); this.addConstituent(root); addParseTree(spanLabeledTree, root, sentenceStart); }
/** * 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()); }
this.addConstituent(childConstituent); this.addRelation(new Relation(edgeLabel, root, childConstituent, 1.0)); this.addParseTree(child, childConstituent, sentenceStartPosition);
this.addConstituent(childConstituent, true); this.addRelation(new Relation(edgeLabel, root, childConstituent, 1.0)); this.addParseTree(child, 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); } } }
/** * 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); } } }