use of edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer in project CoreNLP by stanfordnlp.
the class XMLToAnnotation method processCoreNLPIfDoesNotExist.
public static void processCoreNLPIfDoesNotExist(File processedFile, Properties coreNLPProps, String text) {
if (!processedFile.exists()) {
try {
StanfordCoreNLP coreNLP = new StanfordCoreNLP(coreNLPProps);
// this document holds the split for paragraphs.
Annotation processedAnnotation = coreNLP.process(text);
ProtobufAnnotationSerializer pas = new ProtobufAnnotationSerializer(true);
OutputStream fos = new BufferedOutputStream(new FileOutputStream(processedFile.getAbsolutePath()));
pas.write(processedAnnotation, fos);
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer in project CoreNLP by stanfordnlp.
the class ProcessTokensRegexRequestTest method buildRequest.
public static CoreNLPProtos.TokensRegexRequest buildRequest(Annotation ann, String... patterns) {
ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer();
CoreNLPProtos.TokensRegexRequest.Builder builder = CoreNLPProtos.TokensRegexRequest.newBuilder();
for (String pattern : patterns) {
builder.addPattern(pattern);
}
CoreNLPProtos.Document doc = serializer.toProto(ann);
builder.setDoc(doc);
return builder.build();
}
use of edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer in project CoreNLP by stanfordnlp.
the class ProcessSemgrexRequest method processRequest.
/**
* For a single request, iterate through the SemanticGraphs it
* includes, and add the results of each Semgrex operation included
* in the request.
*/
public static CoreNLPProtos.SemgrexResponse processRequest(CoreNLPProtos.SemgrexRequest request) {
ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer();
CoreNLPProtos.SemgrexResponse.Builder responseBuilder = CoreNLPProtos.SemgrexResponse.newBuilder();
List<SemgrexPattern> patterns = request.getSemgrexList().stream().map(SemgrexPattern::compile).collect(Collectors.toList());
for (CoreNLPProtos.SemgrexRequest.Dependencies sentence : request.getQueryList()) {
CoreNLPProtos.SemgrexResponse.GraphResult.Builder graphResultBuilder = CoreNLPProtos.SemgrexResponse.GraphResult.newBuilder();
List<CoreLabel> tokens = sentence.getTokenList().stream().map(serializer::fromProto).collect(Collectors.toList());
SemanticGraph graph = ProtobufAnnotationSerializer.fromProto(sentence.getGraph(), tokens, "semgrex");
for (SemgrexPattern pattern : patterns) {
graphResultBuilder.addResult(matchSentence(pattern, graph));
}
responseBuilder.addResult(graphResultBuilder.build());
}
return responseBuilder.build();
}
use of edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer in project CoreNLP by stanfordnlp.
the class ExtractQuotesUtil method readSerializedProtobufFile.
public static Annotation readSerializedProtobufFile(File fileIn) {
Annotation annotation;
try {
ProtobufAnnotationSerializer pas = new ProtobufAnnotationSerializer();
InputStream is = new BufferedInputStream(new FileInputStream(fileIn));
Pair<Annotation, InputStream> pair = pas.read(is);
pair.second.close();
annotation = pair.first;
IOUtils.closeIgnoringExceptions(is);
return annotation;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer in project CoreNLP by stanfordnlp.
the class ProcessUniversalEnhancerRequest method processRequest.
/**
* Process all sentences in the document, enhancing the basic dependencies on each sentence
*/
public static CoreNLPProtos.Document processRequest(Pattern relativePronounsPattern, CoreNLPProtos.DependencyEnhancerRequest request) {
ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer();
Annotation annotation = serializer.fromProto(request.getDocument());
if (request.hasLanguage() && (request.getLanguage() == CoreNLPProtos.Language.English || request.getLanguage() == CoreNLPProtos.Language.UniversalEnglish)) {
enhanceEnglishDependencies(annotation);
} else {
enhanceDependencies(relativePronounsPattern, annotation);
}
return serializer.toProto(annotation);
}
Aggregations