use of edu.stanford.nlp.pipeline.Annotation in project CoreNLP by stanfordnlp.
the class SUTimeSimpleParser method parse.
/**
* Parse a string with SUTime.
*
* @throws SUTimeParsingError if anything goes wrong
*/
public static Temporal parse(String str) throws SUTimeParsingError {
try {
Annotation doc = new Annotation(str);
pipeline.annotate(doc);
assert doc.get(CoreAnnotations.SentencesAnnotation.class) != null;
assert doc.get(CoreAnnotations.SentencesAnnotation.class).size() > 0;
List<CoreMap> timexAnnotations = doc.get(TimeAnnotations.TimexAnnotations.class);
if (timexAnnotations.size() > 1) {
throw new RuntimeException("Too many timexes for '" + str + "'");
}
CoreMap timex = timexAnnotations.get(0);
return timex.get(TimeExpression.Annotation.class).getTemporal();
} catch (Exception e) {
SUTimeSimpleParser.SUTimeParsingError parsingError = new SUTimeSimpleParser.SUTimeParsingError(str);
parsingError.initCause(e);
throw parsingError;
}
}
use of edu.stanford.nlp.pipeline.Annotation in project CoreNLP by stanfordnlp.
the class DependencyParserITest method testCCProcess.
/**
* Test that postprocessing like CC-processing can handle the parser
* output properly
*/
public void testCCProcess() {
Properties props = PropertiesUtils.fromString("annotators=tokenize,ssplit,pos,depparse");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String text = "Chris and John went to the store.";
Annotation document = new Annotation(text);
pipeline.annotate(document);
SemanticGraph ccProcessed = document.get(CoreAnnotations.SentencesAnnotation.class).get(0).get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
Collection<TypedDependency> dependencies = ccProcessed.typedDependencies();
GrammaticalRelation expected = UniversalEnglishGrammaticalRelations.getConj("and");
assertTrue(dependencies.stream().map(TypedDependency::reln).collect(Collectors.toList()).contains(expected));
}
use of edu.stanford.nlp.pipeline.Annotation in project CoreNLP by stanfordnlp.
the class DependencyParserITest method testSerializationAnnotation.
/**
* Test that Java serialization works properly.
*/
public void testSerializationAnnotation() throws IOException, ClassNotFoundException {
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,depparse");
String text = "Barack Obama, a Yale professor, is president.";
Annotation document = new Annotation(text);
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
pipeline.annotate(document);
// Serialization should not bork.
File tempfile = IOUtils.writeObjectToTempFile(document.get(CoreAnnotations.SentencesAnnotation.class), "temp");
// Deserialization should not bork.
List<CoreMap> readSentences = IOUtils.readObjectFromFile(tempfile);
// Make sure we didn't lose any information
assertEquals(document.get(CoreAnnotations.SentencesAnnotation.class), readSentences);
}
use of edu.stanford.nlp.pipeline.Annotation in project CoreNLP by stanfordnlp.
the class NumberSequenceClassifierITest method checkLabels.
private static void checkLabels(StanfordCoreNLP pipe, String text, String[] labels, String[] normed) {
Annotation doc = new Annotation(text);
pipe.annotate(doc);
assertTrue(doc.get(CoreAnnotations.SentencesAnnotation.class) != null);
assertTrue(doc.get(CoreAnnotations.SentencesAnnotation.class).size() > 0);
CoreMap sent = doc.get(CoreAnnotations.SentencesAnnotation.class).get(0);
assertTrue(sent.get(CoreAnnotations.TokensAnnotation.class) != null);
List<CoreLabel> tokens = sent.get(CoreAnnotations.TokensAnnotation.class);
if (VERBOSE) {
for (CoreLabel token : tokens) {
System.out.println('\t' + token.word() + ' ' + token.tag() + ' ' + token.ner() + ' ' + (token.containsKey(CoreAnnotations.NumericCompositeTypeAnnotation.class) ? token.get(CoreAnnotations.NumericCompositeValueAnnotation.class) + " " : "") + (token.containsKey(TimeAnnotations.TimexAnnotation.class) ? token.get(TimeAnnotations.TimexAnnotation.class) + " " : ""));
}
}
// check NER labels
assertTrue(tokens.size() == labels.length);
for (int i = 0; i < labels.length; i++) {
if (labels[i] == null) {
assertTrue(tokens.get(i).ner() == null);
} else {
Pattern p = Pattern.compile(labels[i]);
System.err.println("COMPARING NER " + labels[i] + " with " + tokens.get(i).ner());
System.err.flush();
assertTrue("NER should not be null for token " + tokens.get(i) + " in sentence " + tokens, tokens.get(i).ner() != null);
assertTrue(tokens.get(i).ner() + " does not match " + p + " for token " + tokens.get(i) + " in sentence " + tokens, p.matcher(tokens.get(i).ner()).matches());
}
}
// check normalized values, if gold is given
if (normed != null) {
assertTrue(tokens.size() == normed.length);
for (int i = 0; i < normed.length; i++) {
if (normed[i] == null) {
assertTrue(tokens.get(i).get(CoreAnnotations.NormalizedNamedEntityTagAnnotation.class) == null);
} else {
Pattern p = Pattern.compile(normed[i]);
String n = tokens.get(i).get(CoreAnnotations.NormalizedNamedEntityTagAnnotation.class);
String message = "COMPARING NORMED \"" + normed[i] + "\" with \"" + n + "\"";
assertTrue(message + "; latter should not be null", n != null);
assertTrue(message + "; latter should match", p.matcher(n).matches());
}
}
}
}
use of edu.stanford.nlp.pipeline.Annotation in project CoreNLP by stanfordnlp.
the class NaturalLogicAnnotatorITest method testAnnotatorRuns.
@Test
public void testAnnotatorRuns() {
// Run pipeline
StanfordCoreNLP pipeline = new StanfordCoreNLP(new Properties() {
{
setProperty("annotators", "tokenize,ssplit,pos,lemma,parse,natlog");
setProperty("ssplit.isOneSentence", "true");
setProperty("tokenize.class", "PTBTokenizer");
setProperty("tokenize.language", "en");
setProperty("enforceRequirements", "false");
}
});
Annotation ann = new Annotation("All cats have tails");
pipeline.annotate(ann);
// Check output
List<CoreLabel> tokens = ann.get(CoreAnnotations.SentencesAnnotation.class).get(0).get(CoreAnnotations.TokensAnnotation.class);
assertTrue(tokens.get(0).containsKey(NaturalLogicAnnotations.OperatorAnnotation.class));
assertTrue(tokens.get(0).get(NaturalLogicAnnotations.PolarityAnnotation.class).isUpwards());
assertTrue(tokens.get(1).get(NaturalLogicAnnotations.PolarityAnnotation.class).isDownwards());
assertTrue(tokens.get(2).get(NaturalLogicAnnotations.PolarityAnnotation.class).isUpwards());
assertTrue(tokens.get(3).get(NaturalLogicAnnotations.PolarityAnnotation.class).isUpwards());
}
Aggregations