Search in sources :

Example 6 with ProtobufAnnotationSerializer

use of edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer in project CoreNLP by stanfordnlp.

the class ProcessTsurgeonRequest method processRequest.

/**
 * For a single request, iterate through the Trees it includes,
 * perform each Tsurgeon operation on each tree, and return
 * a result with one tree per input tree.
 */
public static CoreNLPProtos.TsurgeonResponse processRequest(CoreNLPProtos.TsurgeonRequest request) {
    ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer();
    CoreNLPProtos.TsurgeonResponse.Builder responseBuilder = CoreNLPProtos.TsurgeonResponse.newBuilder();
    List<Pair<TregexPattern, TsurgeonPattern>> operations = parseOperations(request.getOperationsList());
    List<Tree> trees = request.getTreesList().stream().map(ProtobufAnnotationSerializer::fromProto).collect(Collectors.toList());
    for (Tree tree : trees) {
        tree = Tsurgeon.processPatternsOnTree(operations, tree);
        responseBuilder.addTrees(ProtobufAnnotationSerializer.toFlattenedTree(tree));
    }
    return responseBuilder.build();
}
Also used : ProtobufAnnotationSerializer(edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer) Tree(edu.stanford.nlp.trees.Tree) Pair(edu.stanford.nlp.util.Pair)

Example 7 with ProtobufAnnotationSerializer

use of edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer in project CoreNLP by stanfordnlp.

the class LuceneSentenceIndex method readProtoBufAnnotation.

private List<CoreLabel> readProtoBufAnnotation(byte[] sent) throws IOException {
    ProtobufAnnotationSerializer p = new ProtobufAnnotationSerializer();
    List<CoreLabel> toks = new ArrayList<>();
    ByteArrayInputStream is = new ByteArrayInputStream(sent);
    CoreNLPProtos.Token d;
    do {
        d = CoreNLPProtos.Token.parseDelimitedFrom(is);
        if (d != null)
            toks.add(p.fromProto(d));
    } while (d != null);
    return toks;
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) ProtobufAnnotationSerializer(edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer) CoreNLPProtos(edu.stanford.nlp.pipeline.CoreNLPProtos)

Example 8 with ProtobufAnnotationSerializer

use of edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer in project CoreNLP by stanfordnlp.

the class ProcessTokensRegexRequest method processRequest.

public static CoreNLPProtos.TokensRegexResponse processRequest(CoreNLPProtos.TokensRegexRequest request) {
    ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer();
    CoreNLPProtos.TokensRegexResponse.Builder responseBuilder = CoreNLPProtos.TokensRegexResponse.newBuilder();
    List<TokenSequencePattern> patterns = request.getPatternList().stream().map(TokenSequencePattern::compile).collect(Collectors.toList());
    Annotation annotation = serializer.fromProto(request.getDoc());
    List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
    for (TokenSequencePattern pattern : patterns) {
        CoreNLPProtos.TokensRegexResponse.PatternMatch match = matchPattern(pattern, sentences);
        responseBuilder.addMatch(match);
    }
    return responseBuilder.build();
}
Also used : ProtobufAnnotationSerializer(edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) CoreMap(edu.stanford.nlp.util.CoreMap) TokenSequencePattern(edu.stanford.nlp.ling.tokensregex.TokenSequencePattern) 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