protected SpanLabelView makeSpanLabeledView(List<String> chunkLabels, List<Integer> chunkStart, List<Integer> chunkEnd, TextAnnotation ta, String viewName) { SpanLabelView view = new SpanLabelView(viewName, "GoldStandard", ta, 1.0); for (int i = 0; i < chunkLabels.size(); i++) { view.addSpanLabel(chunkStart.get(i), chunkEnd.get(i), chunkLabels.get(i), 1.0); } return view; }
SpanLabelView offsetView = new SpanLabelView(TOKEN_WITH_CHAR_OFFSET, "Default", ta, 1.0, false); String[] tokens = ta.getTokens(); con.addAttribute(CHAR_START, new Integer(start + textOffset).toString()); con.addAttribute(CHAR_END, new Integer(end + textOffset).toString()); offsetView.addConstituent(con); pointer = end;
@Override public String toString() { List<Constituent> constituents = new ArrayList<>(this.getConstituents()); Collections.sort(constituents, TextAnnotationUtilities.constituentStartComparator); StringBuilder sb = new StringBuilder(); for (Constituent c : constituents) sb.append("[").append(c.getLabel()).append(" ").append(c.getTokenizedSurfaceForm()) .append(" ] "); return sb.toString(); } }
/** * Adds a new span to this view with a given label and score and returns the newly created * constituent. * <p> * If this {@code SpanLabelView} was defined not to accept overlapping spans (in the * constructor), then this function will throw an {@link IllegalArgumentException} when an * attempt is made to label an already existing span. * * @param start the start of the span * @param end the end of the span * @param label the label of the span * @param score the score assigned to this label * @return the newly created constituent that labels the given span. */ public Constituent addSpanLabel(int start, int end, String label, double score) { Constituent c = new Constituent(label, score, this.getViewName(), this.getTextAnnotation(), start, end); if (!allowOverlappingSpans && this.getConstituentsCoveringSpan(start, end).size() != 0) throw new IllegalArgumentException("Span [" + start + ", " + end + "] already labeled."); this.addConstituent(c); return c; }
/** * Helper for create a SpanLabelView from a stream of span labels */ private static int createSpanLabelView( Stream<Pair<IntPair, String>> spans, TextAnnotation ta, String viewName, boolean allowOverlapping) { SpanLabelView view = new SpanLabelView(viewName, "GoldStandard", ta, 1.0, allowOverlapping); spans.forEach(span -> view.addSpanLabel( span.getFirst().getFirst(), span.getFirst().getSecond(), span.getSecond(), 1.0)); ta.addView(viewName, view); return view.count(); }
tokenizedSentences); SpanLabelView sentview = new SpanLabelView(ViewNames.SENTENCE, "UserSpecified", ta, 1d); ta.addView(ViewNames.SENTENCE, sentview); sentview.addSpanLabel(sentstart, s, ViewNames.SENTENCE, 1d); sentstart = s; SpanLabelView emptyview = new SpanLabelView(ViewNames.NER_CONLL, "UserSpecified", ta, 1d); ta.addView(ViewNames.NER_CONLL, emptyview); new Constituent(label, ViewNames.NER_CONLL, ta, span.getFirst(), span.getSecond()); emptyview.addConstituent(c);
private void addNERView(List<String> neLabels, List<Integer> neStart, List<Integer> neEnd, TextAnnotation ta) { SpanLabelView nerView = new SpanLabelView(ViewNames.NER_ONTONOTES, "GoldStandard", ta, 1.0); for (int j = 0; j < neEnd.size(); j++) { int endWord = neEnd.get(j); continue; if (startWord <= endWord && nerView.getConstituentsCoveringSpan(startWord, endWord + 1).isEmpty()) nerView.addSpanLabel(startWord, endWord + 1, nelabel, 1.0);
/** * add constituent, creating view if needed (using constituent's viewname) * @param ta * @param c */ private static void addConstituentToTextAnnotation(TextAnnotation ta, Constituent c) { String viewName = c.getViewName(); View v = null; if ( !ta.hasView( viewName ) ) { v = new SpanLabelView( viewName, NAME, ta, 1.0 ); ta.addView( viewName, v ); } else v = ta.getView( viewName ); v.addConstituent( c ); return; }
@Override protected void addView(TextAnnotation ta) throws AnnotatorException { Annotation document = new Annotation(ta.text); pipeline.annotate(document); SpanLabelView vu = new SpanLabelView(viewName, ta); for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) { for(RelationMention rm : sentence.get(MachineReadingAnnotations.RelationMentionsAnnotation.class)) { if(rm.getType().equals("_NR")) continue; Map<String, Double> scores = new HashMap<>(); for(String label : rm.getTypeProbabilities().keySet()) scores.put(label, rm.getTypeProbabilities().getCount(label)); Constituent c1 = createConstituentGivenMention(rm.getEntityMentionArgs().get(0), ta); Constituent c2 = createConstituentGivenMention(rm.getEntityMentionArgs().get(1), ta); Relation r = new Relation(scores, c1, c2); vu.addRelation(r); if(!vu.containsConstituent(c1)) vu.addConstituent(c1); if(!vu.containsConstituent(c2)) vu.addConstituent(c2); } } for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) { for (EntityMention rm : sentence.get(MachineReadingAnnotations.EntityMentionsAnnotation.class)) { Constituent c = createConstituentGivenMention(rm, ta); if(!vu.containsConstituent(c)) vu.addConstituent(c); } } ta.addView(viewName, vu); }
__stanfordDep.addView(ta); __mentionAnnotator.addView(ta); View annotatedTokenView = new SpanLabelView("RE_ANNOTATED", ta); for (Constituent co : ta.getView(ViewNames.TOKENS).getConstituents()){ Constituent c = co.cloneForNewView("RE_ANNOTATED"); e.printStackTrace(); SpanLabelView mentionView = new SpanLabelView("MENTIONS", "MENTIONS", ta, 1.0f); Constituent firstArg = new Constituent("MENTION", 1.0f, "MENTIONS", ta, firstArgStart, firstArgEnd); Constituent secondArg = new Constituent("MENTION", 1.0f, "MENTIONS", ta, secondArgStart, secondArgEnd); mentionView.addConstituent(firstArg); mentionView.addConstituent(secondArg); if (type.contains("e1,e2")){ Relation relation = new Relation(type.split("[(]")[0], firstArg, secondArg, 1.0f); relation.addAttribute("RelationSubtype", relation.getRelationName()); mentionView.addRelation(relation); mentionView.addRelation(relation); relationLeft.addAttribute("RelationSubtype", relationLeft.getRelationName()); relationRight.addAttribute("RelationSubtype", relationRight.getRelationName()); mentionView.addRelation(relationLeft); mentionView.addRelation(relationRight);
/** * The view will consist of potentially overlapping constituents representing those tokens that * matched entries in the gazetteers. Some tokens will match against several gazetteers. */ @Override public void addView(TextAnnotation ta) throws AnnotatorException { View view = ta.getView(ViewNames.TOKENS); List<Constituent> constituents = view.getConstituents(); SpanLabelView slv = new SpanLabelView(this.getViewName(), this.getClass().getName(), ta, 1d, true); for (int constindx = 0; constindx < constituents.size(); constindx++) { for (int dictindx = 0; dictindx < dictionaries.size(); dictindx++) { dictionaries.get(dictindx).match(constituents, constindx, slv); dictionariesIgnoreCase.get(dictindx).match(constituents, constindx, slv); } } ta.addView(slv.getViewName(), slv); } }
view.addSpanLabel(start, index + 1, dn, 1d);
for (Constituent entityConstituent : entityView.getConstituents()) { String entityId = entityConstituent.getAttribute(EntityIDAttribute); String entityMentionId = entityConstituent.getAttribute(EntityMentionIDAttribute); entityView.addRelation(entityRelation);
public static List<Constituent> getConstituentsInBetween(SpanLabelView view, int start, int end) { List<Constituent> output = view.getConstituentsCoveringSpan(start, end); List<Constituent> restrictedOutput = new ArrayList<>(); for (Constituent c : output) { if ((c.getStartSpan() >= start) && (c.getEndSpan() <= end)) restrictedOutput.add(c); } return restrictedOutput; }
tokenizedSentences); SpanLabelView sentview = new SpanLabelView(ViewNames.SENTENCE, "UserSpecified", ta, 1d); ta.addView(ViewNames.SENTENCE, sentview); sentview.addSpanLabel(sentstart, s, ViewNames.SENTENCE, 1d); sentstart = s; SpanLabelView emptyview = new SpanLabelView(ViewNames.NER_CONLL, "UserSpecified", ta, 1d); ta.addView(ViewNames.NER_CONLL, emptyview); new Constituent(label, ViewNames.NER_CONLL, ta, span.getFirst(), span.getSecond()); emptyview.addConstituent(c);
private void addNERView(List<String> neLabels, List<Integer> neStart, List<Integer> neEnd, TextAnnotation ta) { SpanLabelView nerView = new SpanLabelView(ViewNames.NER_ONTONOTES, "GoldStandard", ta, 1.0); for (int j = 0; j < neEnd.size(); j++) { int endWord = neEnd.get(j); continue; if (startWord <= endWord && nerView.getConstituentsCoveringSpan(startWord, endWord + 1).isEmpty()) nerView.addSpanLabel(startWord, endWord + 1, nelabel, 1.0);
SpanLabelView tokens = (SpanLabelView) ta.getView(ViewNames.TOKENS); compileOffsets(tokens); SpanLabelView nerView = new SpanLabelView(getMentionViewName(), NAME, ta, 1.0, false);
@Override protected void addView(TextAnnotation ta) throws AnnotatorException { Annotation document = new Annotation(ta.text); pipeline.annotate(document); SpanLabelView vu = new SpanLabelView(viewName, ta); for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) { Collection<RelationTriple> triples = sentence.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class); for (RelationTriple triple : triples) { Constituent subject = getConstituent(triple.subjectGloss(), triple.subjectTokenSpan(), sentence, ta); subject.addAttribute("subjectGloss", triple.subjectGloss()); subject.addAttribute("subjectLemmaGloss", triple.subjectLemmaGloss()); subject.addAttribute("subjectLink", triple.subjectLink()); Constituent object = getConstituent(triple.objectGloss(), triple.objectTokenSpan(), sentence, ta); object.addAttribute("objectGloss", triple.objectGloss()); object.addAttribute("objectLemmaGloss", triple.objectLemmaGloss()); object.addAttribute("objectLink", triple.objectLink()); Constituent relation = getConstituent(triple.relationGloss(), triple.relationTokenSpan(), sentence, ta); relation.addAttribute("relationGloss", triple.relationGloss()); relation.addAttribute("relationLemmaGloss", triple.relationLemmaGloss()); Relation subj = new Relation("subj", relation, subject, triple.confidence); Relation obj = new Relation("obj", relation, object, triple.confidence); vu.addRelation(subj); vu.addRelation(obj); vu.addConstituent(subject); vu.addConstituent(object); vu.addConstituent(relation); } } ta.addView(viewName, vu); }
/** * Helper for create a SpanLabelView from a stream of span labels */ private static int createSpanLabelView( Stream<Pair<IntPair, String>> spans, TextAnnotation ta, String viewName, boolean allowOverlapping) { SpanLabelView view = new SpanLabelView(viewName, "GoldStandard", ta, 1.0, allowOverlapping); spans.forEach(span -> view.addSpanLabel( span.getFirst().getFirst(), span.getFirst().getSecond(), span.getSecond(), 1.0)); ta.addView(viewName, view); return view.count(); }
/** * The view will consist of potentially overlapping constituents representing those tokens that * matched entries in the gazetteers. Some tokens will match against several gazetteers. */ @Override public void addView(TextAnnotation ta) throws AnnotatorException { View view = ta.getView(ViewNames.TOKENS); List<Constituent> constituents = view.getConstituents(); SpanLabelView slv = new SpanLabelView(this.getViewName(), this.getClass().getName(), ta, 1d, true); for (int constindx = 0; constindx < constituents.size(); constindx++) { for (int dictindx = 0; dictindx < dictionaries.size(); dictindx++) { dictionaries.get(dictindx).match(constituents, constindx, slv); dictionariesIgnoreCase.get(dictindx).match(constituents, constindx, slv); } } ta.addView(slv.getViewName(), slv); } }