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();
}
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;
}
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();
}
Aggregations