use of edu.illinois.cs.cogcomp.nlp.utility.TokenizerTextAnnotationBuilder in project cogcomp-nlp by CogComp.
the class TokenizerTextAnnotationBuilderTest method testBuilder.
@Test
public void testBuilder() {
TokenizerTextAnnotationBuilder bldr = new TokenizerTextAnnotationBuilder(new IllinoisTokenizer());
final String sentA = "Mr. Dawkins -- a liberal professor -- doesn't like fundamentalists. ";
final String sentB = "He is intolerant of intolerance!";
String lineSep = System.lineSeparator();
int lineSepLength = lineSep.length();
final int refSentStartOffset = 70 + lineSepLength;
final int refSentEndOffset = 102 + lineSepLength;
final int refTokStartOffset = 76 + lineSepLength;
final int refTokEndOffset = refTokStartOffset + 10;
final String text = sentA + lineSep + sentB;
TextAnnotation ta = bldr.createTextAnnotation("test", "test", text);
assertTrue(ta.hasView(ViewNames.SENTENCE));
assertTrue(ta.hasView(ViewNames.TOKENS));
// second
Constituent sent = ta.getView(ViewNames.SENTENCE).getConstituents().get(1);
// sentence
assertEquals(sentB, sent.getSurfaceForm());
assertEquals(refSentStartOffset, sent.getStartCharOffset());
assertEquals(refSentEndOffset, sent.getEndCharOffset());
Constituent tok = ta.getView(ViewNames.TOKENS).getConstituents().get(14);
assertEquals("intolerant", tok.getSurfaceForm());
assertEquals(refTokStartOffset, tok.getStartCharOffset());
assertEquals(refTokEndOffset, tok.getEndCharOffset());
}
use of edu.illinois.cs.cogcomp.nlp.utility.TokenizerTextAnnotationBuilder in project cogcomp-nlp by CogComp.
the class TestTemporalChunker method testTemporalChunkerWithPlainText.
@Test
public void testTemporalChunkerWithPlainText() throws Exception {
String text = "The flu season is winding down, and it has killed 105 children so far - about the average toll.\n" + "\n" + "The season started about a month earlier than usual, sparking concerns it might turn into the worst in " + "a decade. It ended up being very hard on the elderly, but was moderately severe overall, according to " + "the Centers for Disease Control and Prevention.\n" + "\n" + "Six of the pediatric deaths were reported in the last week, and it's possible there will be more, said " + "the CDC's Dr. Michael Jhung said Friday.\n" + "\n" + "Roughly 100 children die in an average flu season. One exception was the swine flu pandemic of " + "2009-2010, when 348 children died.\n" + "\n" + "The CDC recommends that all children ages 6 months and older be vaccinated against flu each season, " + "though only about half get a flu shot or nasal spray.\n" + "\n" + "All but four of the children who died were old enough to be vaccinated, but 90 percent of them did " + "not get vaccinated, CDC officials said.\n" + "\n" + "This year's vaccine was considered effective in children, though it didn't work very well in older " + "people. And the dominant flu strain early in the season was one that tends to " + "cause more severe illness.\n" + "\n" + "The government only does a national flu death count for children. But it does track hospitalization " + "rates for people 65 and older, and those statistics have been grim.\n" + "\n" + "In that group, 177 out of every 100,000 were hospitalized with flu-related illness in the past " + "several months. That's more than 2 1/2 times higher than any other recent season.\n" + "\n" + "This flu season started in early December, a month earlier than usual, and peaked by the end " + "of year. Since then, flu reports have been dropping off throughout the country.\n" + "\n" + "\"We appear to be getting close to the end of flu season,\" Jhung said.";
TextAnnotationBuilder tab = new TokenizerTextAnnotationBuilder(new StatefulTokenizer());
TextAnnotation ta = tab.createTextAnnotation("corpus", "id", text);
POSAnnotator annotator = new POSAnnotator();
try {
annotator.getView(ta);
} catch (AnnotatorException e) {
fail("AnnotatorException thrown!\n" + e.getMessage());
}
tca.addView(ta);
View temporalViews = ta.getView(ViewNames.TIMEX3);
List<Constituent> constituents = temporalViews.getConstituents();
assertEquals("<TIMEX3 type=\"DURATION\" value=\"P1M\">", constituents.get(0).getLabel());
}
use of edu.illinois.cs.cogcomp.nlp.utility.TokenizerTextAnnotationBuilder in project cogcomp-nlp by CogComp.
the class Demo method main.
public static void main(String[] args) throws IOException, AnnotatorException {
Options options = new Options();
Option inputtext = new Option("t", "text", true, "input text to be processed");
inputtext.setRequired(false);
options.addOption(inputtext);
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
try {
CommandLine cmd = parser.parse(options, args);
String defaultText = "The flu season is winding down, and it has killed 105 children so far - about the average toll.\n" + "\n" + "The season started about a month earlier than usual, sparking concerns it might turn into the worst in " + "a decade. It ended up being very hard on the elderly, but was moderately severe overall, according to " + "the Centers for Disease Control and Prevention.\n" + "\n" + "Six of the pediatric deaths were reported in the last week, and it's possible there will be more, said " + "the CDC's Dr. Michael Jhung said Friday.\n" + "\n" + "Roughly 100 children die in an average flu season. One exception was the swine flu pandemic of " + "2009-2010, when 348 children died.\n" + "\n" + "The CDC recommends that all children ages 6 months and older be vaccinated against flu each season, " + "though only about half get a flu shot or nasal spray.\n" + "\n" + "All but four of the children who died were old enough to be vaccinated, but 90 percent of them did " + "not get vaccinated, CDC officials said.\n" + "\n" + "This year's vaccine was considered effective in children, though it didn't work very well in older " + "people. And the dominant flu strain early in the season was one that tends to " + "cause more severe illness.\n" + "\n" + "The government only does a national flu death count for children. But it does track hospitalization " + "rates for people 65 and older, and those statistics have been grim.\n" + "\n" + "In that group, 177 out of every 100,000 were hospitalized with flu-related illness in the past " + "several months. That's more than 2 1/2 times higher than any other recent season.\n" + "\n" + "This flu season started in early December, a month earlier than usual, and peaked by the end " + "of year. Since then, flu reports have been dropping off throughout the country.\n" + "\n" + "\"We appear to be getting close to the end of flu season,\" Jhung said.";
String text = cmd.getOptionValue("text", defaultText);
TextAnnotationBuilder tab = new TokenizerTextAnnotationBuilder(new StatefulTokenizer());
TextAnnotation ta = tab.createTextAnnotation("corpus", "id", text);
POSAnnotator annotator = new POSAnnotator();
try {
annotator.getView(ta);
} catch (AnnotatorException e) {
fail("AnnotatorException thrown!\n" + e.getMessage());
}
Properties rmProps = new TemporalChunkerConfigurator().getDefaultConfig().getProperties();
rmProps.setProperty("useHeidelTime", "False");
TemporalChunkerAnnotator tca = new TemporalChunkerAnnotator(new ResourceManager(rmProps));
tca.addView(ta);
View temporalViews = ta.getView(ViewNames.TIMEX3);
List<Constituent> constituents = temporalViews.getConstituents();
System.out.printf("There're %d time expressions (TIMEX) in total.\n", constituents.size());
for (Constituent c : constituents) {
System.out.printf("TIMEX #%d: Text=%s, Type=%s, Value=%s\n", constituents.indexOf(c), c, c.getAttribute("type"), c.getAttribute("value"));
}
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp("Temporal Normalizer Demo", options);
System.exit(1);
}
}
use of edu.illinois.cs.cogcomp.nlp.utility.TokenizerTextAnnotationBuilder in project cogcomp-nlp by CogComp.
the class OntonotesNamedEntityReader method nextAnnotation.
/**
* parse the pen treebank parse file, producing an annotation covering the entire file.
* @param data the data from the file, each line.
* @param docid the id representing the document name.
* @return the text annotation.
* @throws AnnotatorException
*/
private XmlTextAnnotation nextAnnotation(String data, String docid) throws AnnotatorException {
// we keep everything.
XmlDocumentProcessor xmlProcessor = new XmlDocumentProcessor(tagsWithText, tagsWithAtts, dropTags, true);
StatefulTokenizer st = new StatefulTokenizer();
TokenizerTextAnnotationBuilder taBuilder = new TokenizerTextAnnotationBuilder(st);
XmlTextAnnotationMaker xtam = new XmlTextAnnotationMaker(taBuilder, xmlProcessor);
// read the file and create the annotation.
XmlTextAnnotation xta = xtam.createTextAnnotation(data, "OntoNotes 5.0", docid);
TextAnnotation ta = xta.getTextAnnotation();
List<SpanInfo> fudge = xta.getXmlMarkup();
// create the named entity vi
View nerView = new SpanLabelView(VIEW_NAME, ta);
for (SpanInfo si : fudge) {
if ("enamex".equalsIgnoreCase(si.label)) {
IntPair charOffsets = si.spanOffsets;
Pair<String, IntPair> neLabelPair = si.attributes.get("type");
String neLabel = neLabelPair.getFirst();
int cleanTextCharStart = xta.getXmlSt().computeModifiedOffsetFromOriginal(charOffsets.getFirst());
int cleanTextCharEnd = xta.getXmlSt().computeModifiedOffsetFromOriginal(charOffsets.getSecond());
int cleanTextNeTokStart = ta.getTokenIdFromCharacterOffset(cleanTextCharStart);
// StringTransformation returns one-past-the-end index; TextAnnotation maps at-the-end index
int cleanTextNeTokEnd = ta.getTokenIdFromCharacterOffset(cleanTextCharEnd - 1);
if (cleanTextNeTokStart == -1 || cleanTextNeTokEnd == -1) {
for (Constituent c : nerView.getConstituents()) {
System.err.println(c);
}
System.err.println("Something wonky in \"" + docid + "\", at " + charOffsets + ", " + cleanTextCharStart + " - " + cleanTextCharEnd + " = " + ta.text.substring(cleanTextCharStart, cleanTextCharEnd));
} else {
if (entityCounts.containsKey(neLabel)) {
entityCounts.put(neLabel, (entityCounts.get(neLabel) + 1));
} else {
entityCounts.put(neLabel, 1);
}
// constituent token indexing uses one-past-the-end
Constituent neCon = new Constituent(neLabel, nerView.getViewName(), ta, cleanTextNeTokStart, cleanTextNeTokEnd + 1);
nerView.addConstituent(neCon);
}
}
}
ta.addView(VIEW_NAME, nerView);
return xta;
}
use of edu.illinois.cs.cogcomp.nlp.utility.TokenizerTextAnnotationBuilder in project cogcomp-nlp by CogComp.
the class W2VDatalessTest method getTextAnnotation.
private TextAnnotation getTextAnnotation(String text) {
TokenizerTextAnnotationBuilder taBuilder = new TokenizerTextAnnotationBuilder(new StatefulTokenizer());
TextAnnotation ta = taBuilder.createTextAnnotation(text);
return ta;
}
Aggregations