use of edu.stanford.nlp.util.IntPair in project CoreNLP by stanfordnlp.
the class ScrollableTreeJPanel method renderRows.
private void renderRows(Graphics2D g2, FontMetrics fM, Color defaultColor2) {
double nodeHeight = fM.getHeight();
double layerMultiplier = (1.0 + belowLineSkip + aboveLineSkip + parentSkip);
double layerHeight = nodeHeight * layerMultiplier;
//Draw the yield
List<HasWord> sentence = tree.yieldHasWord();
for (int i = 0; i < sentence.size(); i++) {
g2.drawString(sentence.get(i).word(), yieldOffsets[i], (float) (yieldHeight + layerHeight));
}
//Greedily draw the constituents
final float rowOrigin = (float) (yieldHeight + 2.0 * layerHeight);
List<List<IntPair>> rows = new ArrayList<>();
for (Constituent c : diffConstituents) {
for (int rowIdx = 0; rowIdx < diffConstituents.size(); rowIdx++) {
float rowHeight = rowOrigin + (float) (rowIdx * layerHeight);
int ext = (c.end() == (yieldOffsets.length - 1)) ? 0 : 1;
if (rowIdx >= rows.size()) {
rows.add(new ArrayList<>());
rows.get(rowIdx).add(new IntPair(c.start(), c.end()));
double nodeWidth = fM.stringWidth(c.value());
g2.drawString(c.value(), yieldOffsets[c.start()], rowHeight);
try {
g2.drawLine((int) (yieldOffsets[c.start()] + nodeWidth) + 10, (int) rowHeight, (int) (yieldOffsets[c.end() + ext]) - 15, (int) rowHeight);
} catch (ArrayIndexOutOfBoundsException e) {
// This happens if yield of two compared trees do not match. Just ignore it for now
// System.err.printf("yieldOffsets.length is %d, c.start() is %d, c.end() is %d, ext is %d%n", yieldOffsets.length, c.start(), c.end(), ext);
}
break;
} else {
boolean foundOverlap = false;
for (IntPair span : rows.get(rowIdx)) {
if (doesOverlap(c, span)) {
foundOverlap = true;
break;
}
}
if (!foundOverlap) {
rows.get(rowIdx).add(new IntPair(c.start(), c.end()));
double nodeWidth = fM.stringWidth(c.value());
g2.drawString(c.value(), yieldOffsets[c.start()], rowHeight);
g2.drawLine((int) (yieldOffsets[c.start()] + nodeWidth) + 10, (int) rowHeight, (int) (yieldOffsets[c.end() + ext]) - 15, (int) rowHeight);
break;
}
}
}
}
}
use of edu.stanford.nlp.util.IntPair in project CoreNLP by stanfordnlp.
the class Tdiff method markDiff.
/**
* Marks bracketings in t2 not in t1 using the DoAnnotation field.
* Returns a list of brackets in t1 not in t2.
*
* @param t1
* @param t2
* @return A list of brackets in t1 not in t2;
*/
public static Set<Constituent> markDiff(Tree t1, Tree t2) {
// if (t1 == null || t2 == null || ! t1.value().equals(t2.value())) {
// System.err.printf("t1 value is %s; t2 value is %s; t1 is %s t2 is %s", t1.value(), t2.value(), t1, t2);
// }
Set<Constituent> t1Labels = (t1 == null) ? Generics.<Constituent>newHashSet() : t1.constituents(cf);
if (t2 != null) {
t2.setSpans();
for (Tree subTree : t2) {
if (subTree.isPhrasal()) {
IntPair span = subTree.getSpan();
Constituent c = cf.newConstituent(span.getSource(), span.getTarget(), subTree.label(), 0.0);
if (t1Labels.contains(c)) {
t1Labels.remove(c);
((CoreLabel) subTree.label()).set(CoreAnnotations.DoAnnotation.class, false);
} else {
((CoreLabel) subTree.label()).set(CoreAnnotations.DoAnnotation.class, true);
}
}
}
}
return t1Labels;
}
use of edu.stanford.nlp.util.IntPair in project CoreNLP by stanfordnlp.
the class CoNLLUDocumentWriter method printSemanticGraph.
public String printSemanticGraph(SemanticGraph sg, boolean unescapeParenthesis) {
StringBuilder sb = new StringBuilder();
/* Print comments. */
for (String comment : sg.getComments()) {
sb.append(comment).append("\n");
}
for (IndexedWord token : sg.vertexListSorted()) {
/* Check for multiword tokens. */
if (token.containsKey(CoreAnnotations.CoNLLUTokenSpanAnnotation.class)) {
IntPair tokenSpan = token.get(CoreAnnotations.CoNLLUTokenSpanAnnotation.class);
if (tokenSpan.getSource() == token.index()) {
String range = String.format("%d-%d", tokenSpan.getSource(), tokenSpan.getTarget());
sb.append(String.format("%s\t%s\t_\t_\t_\t_\t_\t_\t_\t_%n", range, token.originalText()));
}
}
/* Try to find main governor and additional dependencies. */
int govIdx = -1;
GrammaticalRelation reln = null;
HashMap<Integer, String> additionalDeps = new HashMap<>();
for (IndexedWord parent : sg.getParents(token)) {
SemanticGraphEdge edge = sg.getEdge(parent, token);
if (govIdx == -1 && !edge.isExtra()) {
govIdx = parent.index();
reln = edge.getRelation();
} else {
additionalDeps.put(parent.index(), edge.getRelation().toString());
}
}
String additionalDepsString = CoNLLUUtils.toExtraDepsString(additionalDeps);
String word = token.word();
String featuresString = CoNLLUUtils.toFeatureString(token.get(CoreAnnotations.CoNLLUFeats.class));
String pos = token.getString(CoreAnnotations.PartOfSpeechAnnotation.class, "_");
String upos = token.getString(CoreAnnotations.CoarseTagAnnotation.class, "_");
String misc = token.getString(CoreAnnotations.CoNLLUMisc.class, "_");
String lemma = token.getString(CoreAnnotations.LemmaAnnotation.class, "_");
String relnName = reln == null ? "_" : reln.toString();
/* Root. */
if (govIdx == -1 && sg.getRoots().contains(token)) {
govIdx = 0;
relnName = GrammaticalRelation.ROOT.toString();
}
if (unescapeParenthesis) {
word = word.replaceAll(LRB_PATTERN, "(");
word = word.replaceAll(RRB_PATTERN, ")");
lemma = lemma.replaceAll(LRB_PATTERN, "(");
lemma = lemma.replaceAll(RRB_PATTERN, ")");
}
sb.append(String.format("%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t%s%n", token.index(), word, lemma, upos, pos, featuresString, govIdx, relnName, additionalDepsString, misc));
}
sb.append("\n");
return sb.toString();
}
use of edu.stanford.nlp.util.IntPair in project CoreNLP by stanfordnlp.
the class SentimentAnnotator method annotate.
@Override
public void annotate(Annotation annotation) {
if (annotation.containsKey(CoreAnnotations.SentencesAnnotation.class)) {
// TODO: parallelize
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
for (CoreMap sentence : sentences) {
Tree binarized = sentence.get(TreeCoreAnnotations.BinarizedTreeAnnotation.class);
if (binarized == null) {
throw new AssertionError("Binarized sentences not built by parser");
}
Tree collapsedUnary = transformer.transformTree(binarized);
SentimentCostAndGradient scorer = new SentimentCostAndGradient(model, null);
scorer.forwardPropagateTree(collapsedUnary);
sentence.set(SentimentCoreAnnotations.SentimentAnnotatedTree.class, collapsedUnary);
int sentiment = RNNCoreAnnotations.getPredictedClass(collapsedUnary);
sentence.set(SentimentCoreAnnotations.SentimentClass.class, SentimentUtils.sentimentString(model, sentiment));
Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
if (tree != null) {
collapsedUnary.setSpans();
// map the sentiment annotations onto the tree
Map<IntPair, String> spanSentiment = Generics.newHashMap();
for (Tree bt : collapsedUnary) {
IntPair p = bt.getSpan();
int sen = RNNCoreAnnotations.getPredictedClass(bt);
String sentStr = SentimentUtils.sentimentString(model, sen);
if (!spanSentiment.containsKey(p)) {
// we'll take the first = highest one discovered
spanSentiment.put(p, sentStr);
}
}
if (((CoreLabel) tree.label()).containsKey(CoreAnnotations.SpanAnnotation.class)) {
throw new IllegalStateException("This code assumes you don't have SpanAnnotation");
}
tree.setSpans();
for (Tree t : tree) {
IntPair p = t.getSpan();
String str = spanSentiment.get(p);
if (str != null) {
CoreLabel cl = (CoreLabel) t.label();
cl.set(SentimentCoreAnnotations.SentimentClass.class, str);
cl.remove(CoreAnnotations.SpanAnnotation.class);
}
}
}
}
} else {
throw new RuntimeException("unable to find sentences in: " + annotation);
}
}
use of edu.stanford.nlp.util.IntPair in project CoreNLP by stanfordnlp.
the class CorefMentionFinder method addGoldMentions.
// temporary for debug
protected static void addGoldMentions(List<CoreMap> sentences, List<Set<IntPair>> mentionSpanSetList, List<List<Mention>> predictedMentions, List<List<Mention>> allGoldMentions) {
for (int i = 0, sz = sentences.size(); i < sz; i++) {
List<Mention> mentions = predictedMentions.get(i);
CoreMap sent = sentences.get(i);
List<CoreLabel> tokens = sent.get(TokensAnnotation.class);
Set<IntPair> mentionSpanSet = mentionSpanSetList.get(i);
List<Mention> golds = allGoldMentions.get(i);
for (Mention g : golds) {
IntPair pair = new IntPair(g.startIndex, g.endIndex);
if (!mentionSpanSet.contains(pair)) {
int dummyMentionId = -1;
Mention m = new Mention(dummyMentionId, g.startIndex, g.endIndex, tokens, sent.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class), sent.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class) != null ? sent.get(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class) : sent.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class), new ArrayList<>(tokens.subList(g.startIndex, g.endIndex)));
mentions.add(m);
mentionSpanSet.add(pair);
}
}
}
}
Aggregations