Search in sources :

Example 1 with ProtobufAnnotationSerializer

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();
        }
    }
}
Also used : ProtobufAnnotationSerializer(edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) Annotation(edu.stanford.nlp.pipeline.Annotation)

Example 2 with ProtobufAnnotationSerializer

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();
}
Also used : ProtobufAnnotationSerializer(edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer) CoreNLPProtos(edu.stanford.nlp.pipeline.CoreNLPProtos)

Example 3 with ProtobufAnnotationSerializer

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();
}
Also used : SemgrexPattern(edu.stanford.nlp.semgraph.semgrex.SemgrexPattern) CoreLabel(edu.stanford.nlp.ling.CoreLabel) ProtobufAnnotationSerializer(edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer) CoreNLPProtos(edu.stanford.nlp.pipeline.CoreNLPProtos) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph)

Example 4 with ProtobufAnnotationSerializer

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);
    }
}
Also used : ProtobufAnnotationSerializer(edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Annotation(edu.stanford.nlp.pipeline.Annotation) FileInputStream(java.io.FileInputStream)

Example 5 with ProtobufAnnotationSerializer

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);
}
Also used : ProtobufAnnotationSerializer(edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer) Annotation(edu.stanford.nlp.pipeline.Annotation)

Aggregations

ProtobufAnnotationSerializer (edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer)8 Annotation (edu.stanford.nlp.pipeline.Annotation)4 CoreNLPProtos (edu.stanford.nlp.pipeline.CoreNLPProtos)3 CoreLabel (edu.stanford.nlp.ling.CoreLabel)2 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)1 TokenSequencePattern (edu.stanford.nlp.ling.tokensregex.TokenSequencePattern)1 StanfordCoreNLP (edu.stanford.nlp.pipeline.StanfordCoreNLP)1 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)1 SemgrexPattern (edu.stanford.nlp.semgraph.semgrex.SemgrexPattern)1 Tree (edu.stanford.nlp.trees.Tree)1 CoreMap (edu.stanford.nlp.util.CoreMap)1 Pair (edu.stanford.nlp.util.Pair)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1