use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView in project cogcomp-nlp by CogComp.
the class VerbSenseLabeler method initializeDummySentenceVerb.
protected TextAnnotation initializeDummySentenceVerb() {
List<String[]> listOfTokens = new ArrayList<>();
listOfTokens.add(new String[] { "I", "do", "." });
TextAnnotation ta = BasicTextAnnotationBuilder.createTextAnnotationFromTokens("", "", listOfTokens);
TokenLabelView tlv = new TokenLabelView(ViewNames.POS, "Test", ta, 1.0);
tlv.addTokenLabel(0, "PRP", 1d);
tlv.addTokenLabel(1, "VBP", 1d);
tlv.addTokenLabel(2, ".", 1d);
ta.addView(ViewNames.POS, tlv);
ta.addView(ViewNames.NER, new SpanLabelView(ViewNames.NER, "test", ta, 1d));
SpanLabelView chunks = new SpanLabelView(ViewNames.SHALLOW_PARSE, "test", ta, 1d);
chunks.addSpanLabel(0, 1, "NP", 1d);
chunks.addSpanLabel(1, 2, "VP", 1d);
ta.addView(ViewNames.SHALLOW_PARSE, chunks);
TokenLabelView view = new TokenLabelView(ViewNames.LEMMA, "test", ta, 1d);
view.addTokenLabel(0, "i", 1d);
view.addTokenLabel(1, "do", 1d);
view.addTokenLabel(2, ".", 1d);
ta.addView(ViewNames.LEMMA, view);
return ta;
}
use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView in project cogcomp-nlp by CogComp.
the class TokenizerTextAnnotationBuilder method buildTextAnnotation.
/**
* instantiate a TextAnnotation using a SentenceViewGenerator to create an explicit Sentence
* view
*
* @param corpusId a field in TextAnnotation that can be used by the client for book-keeping
* (e.g. track texts from the same corpus)
* @param textId a field in TextAnnotation that can be used by the client for book-keeping (e.g.
* identify a specific document by some reference string)
* @param text the plain English text to process
* @param tokens the token Strings, in order from original text
* @param sentenceEndPositions token offsets of sentence ends (one-past-the-end indexing)
* @param sentenceViewGenerator the name of the source of the sentence split
* @param sentenceViewScore a score that may indicate how reliable the sentence split
* information is
* @return a TextAnnotation object with {@link ViewNames#TOKENS} and {@link ViewNames#SENTENCE}
* views.
*/
public static TextAnnotation buildTextAnnotation(String corpusId, String textId, String text, String[] tokens, int[] sentenceEndPositions, String sentenceViewGenerator, double sentenceViewScore) {
if (sentenceEndPositions[sentenceEndPositions.length - 1] != tokens.length)
throw new IllegalArgumentException("Invalid sentence boundary. Last element should be the number of tokens");
IntPair[] offsets = TokenUtils.getTokenOffsets(text, tokens);
assert offsets.length == tokens.length;
TextAnnotation ta = new TextAnnotation(corpusId, textId, text, offsets, tokens, sentenceEndPositions);
SpanLabelView view = new SpanLabelView(ViewNames.SENTENCE, sentenceViewGenerator, ta, sentenceViewScore);
int start = 0;
for (int s : sentenceEndPositions) {
view.addSpanLabel(start, s, ViewNames.SENTENCE, 1d);
start = s;
}
ta.addView(ViewNames.SENTENCE, view);
SpanLabelView tokView = new SpanLabelView(ViewNames.TOKENS, sentenceViewGenerator, ta, sentenceViewScore);
for (int tokIndex = 0; tokIndex < tokens.length; ++tokIndex) {
tokView.addSpanLabel(tokIndex, tokIndex + 1, tokens[tokIndex], 1d);
}
ta.addView(ViewNames.TOKENS, tokView);
return ta;
}
use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView in project cogcomp-nlp by CogComp.
the class TokenizerUtilities method addTokenView.
public static SpanLabelView addTokenView(TextAnnotation input, Tokenizer tokenizer, String source) {
SentenceSplitter splitter = new SentenceSplitter(new String[] { input.getText() });
Sentence[] sentences = splitter.splitAll();
List<String> tokens = new ArrayList<>();
List<IntPair> charOffsets = new ArrayList<>();
List<IntPair> sentenceSpans = new ArrayList<>();
int start = 0;
for (Sentence s : sentences) {
Pair<String[], IntPair[]> toks = tokenizer.tokenizeSentence(s.text);
for (int i = 0; i < toks.getFirst().length; i++) {
tokens.add(toks.getFirst()[i]);
IntPair charOffset = toks.getSecond()[i];
IntPair translatedCharOffset = new IntPair(charOffset.getFirst() + s.start, charOffset.getSecond() + s.start);
charOffsets.add(translatedCharOffset);
}
sentenceSpans.add(new IntPair(start, tokens.size()));
start = tokens.size();
}
if (tokens.size() != charOffsets.size())
throw new IllegalArgumentException("tokens (" + tokens.size() + ") must equal charOffsets (" + charOffsets.size() + "), but does not.");
SpanLabelView tokView = new SpanLabelView(ViewNames.TOKENS, source, input, 1.0);
SpanLabelView view = new SpanLabelView(ViewNames.SENTENCE, source, input, 1.0);
for (int i = 0; i < tokens.size(); ++i) {
tokView.addSpanLabel(i, i + 1, tokens.get(i), 1d);
}
for (IntPair span : sentenceSpans) {
view.addSpanLabel(span.getFirst(), span.getSecond(), ViewNames.SENTENCE, 1d);
}
return tokView;
}
use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView in project cogcomp-nlp by CogComp.
the class StanfordRelationsHandler method addView.
@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);
}
Aggregations