/** * Ugly hack to initialize the trees arraylist with the correct number elements. There should be * as many trees as there are sentences. Later on, we can say trees.set(sentenceId, tree); */ private void safeInitializeTrees() { if (this.trees == null) { trees = new ArrayList<>(); for (int i = 0; i < this.getTextAnnotation().getNumberOfSentences(); i++) { trees.add(null); } } }
/** * Ugly hack to initialize the trees arraylist with the correct number elements. There should be * as many trees as there are sentences. Later on, we can say trees.set(sentenceId, tree); */ private void safeInitializeTrees() { if (this.trees == null) { trees = new ArrayList<>(); for (int i = 0; i < this.getTextAnnotation().getNumberOfSentences(); i++) { trees.add(null); } } }
@Override public String toString() { if (trees == null) makeTrees(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < this.getTextAnnotation().getNumberOfSentences(); i++) { if (this.trees.get(i) != null) sb.append(this.getTree(i).toString()).append("\n"); } return sb.toString(); }
@Override public String toString() { if (trees == null) makeTrees(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < this.getTextAnnotation().getNumberOfSentences(); i++) { if (this.trees.get(i) != null) sb.append(this.getTree(i).toString()).append("\n"); } return sb.toString(); }
/** * Get the root constituent of the tree that covers a sentence */ public Constituent getTreeRoot(int sentenceId) { return this.getTreeRoot(this.getTextAnnotation().getSentence(sentenceId)); }
/** * Makes a new constituent spanning {@code start} to {@code end} with the label * {@code constituentLabel} and score {@code constituentScore}. */ private Constituent createNewConstituent(int start, int end, String constituentLabel, double constituentScore) { return new Constituent(constituentLabel, constituentScore, this.getViewName(), this.getTextAnnotation(), start, end); }
/** * Makes a new constituent spanning {@code start} to {@code end} with the label * {@code constituentLabel} and score {@code constituentScore}. */ private Constituent createNewConstituent(int start, int end, String constituentLabel, double constituentScore) { return new Constituent(constituentLabel, constituentScore, this.getViewName(), this.getTextAnnotation(), start, end); }
/** * Get the root constituent of the tree that covers a sentence */ public Constituent getTreeRoot(int sentenceId) { return this.getTreeRoot(this.getTextAnnotation().getSentence(sentenceId)); }
private int getSentenceStart(int sentenceId) { Sentence sentence = this.getTextAnnotation().getSentence(sentenceId); return sentence.getStartSpan(); }
private int getSentenceStart(int sentenceId) { Sentence sentence = this.getTextAnnotation().getSentence(sentenceId); return sentence.getStartSpan(); }
/** * Makes the tree objects. This creates one tree per sentence. */ protected void makeTrees() { safeInitializeTrees(); for (int sentenceId = 0; sentenceId < this.getTextAnnotation().getNumberOfSentences(); sentenceId++) { Constituent root = this.getRootConstituent(sentenceId); if (root == null) { // throw new IllegalStateException( // "Unable to find the root constituent. " // + "Maybe the view is not restricted to a single sentence."); trees.set(sentenceId, null); } else trees.set(sentenceId, makeTree(root)); } firstTree = false; }
/** * Makes the tree objects. This creates one tree per sentence. */ protected void makeTrees() { safeInitializeTrees(); for (int sentenceId = 0; sentenceId < this.getTextAnnotation().getNumberOfSentences(); sentenceId++) { Constituent root = this.getRootConstituent(sentenceId); if (root == null) { // throw new IllegalStateException( // "Unable to find the root constituent. " // + "Maybe the view is not restricted to a single sentence."); trees.set(sentenceId, null); } else trees.set(sentenceId, makeTree(root)); } firstTree = false; }
/** * Gets the root constituent of the tree for the given sentence */ public Constituent getRootConstituent(Sentence sentence) { if (this.roots == null) { findRoots(); } return this.roots.get(this.getTextAnnotation().getSentenceId( sentence.getSentenceConstituent())); }
/** * Gets the root constituent of the tree for the given sentence */ public Constituent getRootConstituent(Sentence sentence) { if (this.roots == null) { findRoots(); } return this.roots.get(this.getTextAnnotation().getSentenceId( sentence.getSentenceConstituent())); }
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); } }
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 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); } }
/** * 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()); }