@Override public int index() { return label.index(); }
/** * Get the index for the current node. */ public int index() { return label.index(); }
@Override public int hashCode() { int hash = 1; hash *= (31 + modifier.index()); hash *= 138 * head.value().hashCode(); return hash; }
/** Return the given relation triple as a flat sentence */ public List<CoreLabel> asSentence() { PriorityQueue<CoreLabel> orderedSentence = new FixedPrioritiesPriorityQueue<>(); double defaultIndex = 0.0; for (CoreLabel token : subject) { orderedSentence.add(token, token.index() >= 0 ? (double) -token.index() : -defaultIndex); defaultIndex += 1.0; } for (CoreLabel token : relation) { orderedSentence.add(token, token.index() >= 0 ? (double) -token.index() : -defaultIndex); defaultIndex += 1.0; } for (CoreLabel token : object) { orderedSentence.add(token, token.index() >= 0 ? (double) -token.index() : -defaultIndex); defaultIndex += 1.0; } return orderedSentence.toSortedList(); }
/** * Modifier must have IndexAnnotation. If head has 0 as its index, then it is * the start symbol ("boundary symbol" in the Dan Klein code). * * @param modifier * @param head * @param rel */ public CollinsDependency(CoreLabel modifier, CoreLabel head, CollinsRelation rel) { if(modifier.index() == 0) throw new RuntimeException("No index annotation for " + modifier.toString()); this.modifier = modifier; this.head = head; relation = rel; }
/** * Return the tokens in this fragment, but padded with null so that the index in this * sentence matches the index of the parse tree. */ public List<CoreLabel> paddedWords() { int maxIndex = -1; for (IndexedWord vertex : parseTree.vertexSet()) { maxIndex = Math.max(maxIndex, vertex.index()); } List<CoreLabel> tokens = new ArrayList<>(maxIndex); for (int i = 0; i < maxIndex; ++i) { tokens.add(null); } for (CoreLabel token : this.words) { tokens.set(token.index() - 1, token); } return tokens; }
/** Print in the format expected by Gabriel Stanovsky and Ido Dagan, Creating a Large Benchmark for Open * Information Extraction, EMNLP 2016. https://gabrielstanovsky.github.io/assets/papers/emnlp16a/paper.pdf , * with equivalence classes. */ public String toQaSrlString(CoreMap sentence) { String equivalenceClass = subjectHead().index() + "." + relationHead().index() + '.' + objectHead().index(); return equivalenceClass + '\t' + subjectGloss().replace('\t', ' ') + '\t' + relationGloss().replace('\t', ' ') + '\t' + objectGloss().replace('\t', ' ') + '\t' + confidence + '\t' + StringUtils.join(sentence.get(CoreAnnotations.TokensAnnotation.class).stream().map(x -> x.word().replace('\t', ' ').replace(" ", "")), " "); }
/** The head of the subject of this relation triple. */ @Override public CoreLabel subjectHead() { if (subject.size() == 1) { return subject.get(0); } Span subjectSpan = Span.fromValues(subject.get(0).index(), subject.get(subject.size() - 1).index()); for (int i = subject.size() - 1; i >= 0; --i) { for (SemanticGraphEdge edge : sourceTree.incomingEdgeIterable(new IndexedWord(subject.get(i)))) { if (edge.getGovernor().index() < subjectSpan.start() || edge.getGovernor().index() >= subjectSpan.end()) { return subject.get(i); } } } return subject.get(subject.size() - 1); }
/** The head of the object of this relation triple. */ @Override public CoreLabel objectHead() { if (object.size() == 1) { return object.get(0); } Span objectSpan = Span.fromValues(object.get(0).index(), object.get(object.size() - 1).index()); for (int i = object.size() - 1; i >= 0; --i) { for (SemanticGraphEdge edge : sourceTree.incomingEdgeIterable(new IndexedWord(object.get(i)))) { if (edge.getGovernor().index() < objectSpan.start() || edge.getGovernor().index() >= objectSpan.end()) { return object.get(i); } } } return object.get(object.size() - 1); }
/** * Returns the indices of all imperative verbs in the * tree t. */ private static Set<Integer> getImperatives(Tree t) { Set<Integer> imps = new HashSet<>(); TregexMatcher matcher = IMPERATIVE_PATTERN.matcher(t); while (matcher.find()) { List<Label> verbs = matcher.getMatch().yield(); CoreLabel cl = (CoreLabel) verbs.get(0); imps.add(cl.index()); } return imps; }
public ArrayList<ArrayList<IndexedWord>> getPremodifiers(){ ArrayList<ArrayList<IndexedWord>> premod = new ArrayList<>(); if(headIndexedWord == null) return premod; for(Pair<GrammaticalRelation,IndexedWord> child : dependency.childPairs(headIndexedWord)){ String function = child.first().getShortName(); if(child.second().index() < headWord.index() && !child.second.tag().equals("DT") && !child.second.tag().equals("WRB") && !function.endsWith("det") && !function.equals("nummod") && !function.startsWith("acl") && !function.startsWith("advcl") && !function.equals("punct")){ ArrayList<IndexedWord> phrase = new ArrayList<>(dependency.descendants(child.second())); Collections.sort(phrase); premod.add(phrase); } } return premod; }
public ArrayList<ArrayList<IndexedWord>> getPremodifiers(){ ArrayList<ArrayList<IndexedWord>> premod = new ArrayList<>(); if(headIndexedWord == null) return premod; for(Pair<GrammaticalRelation,IndexedWord> child : enhancedDependency.childPairs(headIndexedWord)){ String function = child.first().getShortName(); if(child.second().index() < headWord.index() && !child.second.tag().equals("DT") && !child.second.tag().equals("WRB") && !function.endsWith("det") && !function.equals("nummod") && !function.startsWith("acl") && !function.startsWith("advcl") && !function.equals("punct")){ ArrayList<IndexedWord> phrase = new ArrayList<>(enhancedDependency.descendants(child.second())); Collections.sort(phrase); premod.add(phrase); } } return premod; }
/** Returns a 0-based index of the head of the tree. Assumes the leaves had been indexed from 1 */ static int headIndex(Tree tree) { CoreLabel label = ErasureUtils.uncheckedCast(tree.label()); CoreLabel headLabel = label.get(TreeCoreAnnotations.HeadWordLabelAnnotation.class); return headLabel.index() - 1; }
/** * Return a Protobuf RelationTriple from a RelationTriple. */ public static CoreNLPProtos.SentenceFragment toProto(SentenceFragment fragment) { return CoreNLPProtos.SentenceFragment.newBuilder() .setAssumedTruth(fragment.assumedTruth) .setScore(fragment.score) .addAllTokenIndex(fragment.words.stream().map(x -> x.index() - 1).collect(Collectors.toList())) .setRoot(fragment.parseTree.getFirstRoot().index() - 1) .build(); }
/** Returns a 0-based index of the left leaf of the tree. Assumes the leaves had been indexed from 1 */ static int leftIndex(Tree tree) { if (tree.isLeaf()) { CoreLabel label = ErasureUtils.uncheckedCast(tree.label()); return label.index() - 1; } return leftIndex(tree.children()[0]); }
/** Returns a 0-based index of the right leaf of the tree. Assumes the leaves had been indexed from 1 */ static int rightIndex(Tree tree) { if (tree.isLeaf()) { CoreLabel label = ErasureUtils.uncheckedCast(tree.label()); return label.index() - 1; } return rightIndex(tree.children()[tree.children().length - 1]); }
/** Set attributes of a mention: * head string, mention type, NER label, Number, Gender, Animacy * @throws Exception */ public void process(Dictionaries dict, Semantics semantics, MentionExtractor mentionExtractor) throws Exception { setHeadString(); setType(dict); setNERString(); List<String> mStr = getMentionString(); setNumber(dict); setGender(dict, getGender(dict, mStr)); setAnimacy(dict); setPerson(dict); setDiscourse(); headIndexedWord = dependency.getNodeByIndexSafe(headWord.index()); if(semantics!=null) setSemantics(dict, semantics, mentionExtractor); }