|
10 | 10 | import java.io.InputStream;
|
11 | 11 | import java.io.IOException;
|
12 | 12 | import java.io.OutputStream;
|
| 13 | +import java.util.ArrayList; |
13 | 14 | import java.util.List;
|
14 | 15 | import java.util.stream.Collectors;
|
15 | 16 |
|
| 17 | +import edu.stanford.nlp.ling.CoreAnnotations; |
16 | 18 | import edu.stanford.nlp.ling.CoreLabel;
|
17 | 19 | import edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer;
|
18 | 20 | import edu.stanford.nlp.pipeline.CoreNLPProtos;
|
19 | 21 | import edu.stanford.nlp.semgraph.SemanticGraph;
|
| 22 | +import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations; |
20 | 23 | import edu.stanford.nlp.semgraph.SemanticGraphEdge;
|
21 | 24 | import edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher;
|
22 | 25 | import edu.stanford.nlp.semgraph.semgrex.SemgrexPattern;
|
| 26 | +import edu.stanford.nlp.util.ArrayCoreMap; |
| 27 | +import edu.stanford.nlp.util.CoreMap; |
| 28 | +import edu.stanford.nlp.util.Pair; |
23 | 29 | import edu.stanford.nlp.util.ProcessProtobufRequest;
|
24 | 30 |
|
25 | 31 | public class ProcessSemgrexRequest extends ProcessProtobufRequest {
|
@@ -83,19 +89,28 @@ public static CoreNLPProtos.SemgrexResponse processRequest(CoreNLPProtos.Semgrex
|
83 | 89 | ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer();
|
84 | 90 | CoreNLPProtos.SemgrexResponse.Builder responseBuilder = CoreNLPProtos.SemgrexResponse.newBuilder();
|
85 | 91 |
|
86 |
| - List<SemgrexPattern> patterns = request.getSemgrexList().stream().map(SemgrexPattern::compile).collect(Collectors.toList()); |
87 |
| - int graphIdx = 0; |
| 92 | + List<CoreMap> sentences = new ArrayList<>(); |
88 | 93 | for (CoreNLPProtos.SemgrexRequest.Dependencies sentence : request.getQueryList()) {
|
89 |
| - CoreNLPProtos.SemgrexResponse.GraphResult.Builder graphResultBuilder = CoreNLPProtos.SemgrexResponse.GraphResult.newBuilder(); |
90 |
| - |
91 | 94 | final List<CoreLabel> tokens;
|
92 | 95 | if (sentence.getGraph().getTokenList().size() > 0) {
|
93 | 96 | tokens = sentence.getGraph().getTokenList().stream().map(serializer::fromProto).collect(Collectors.toList());
|
94 | 97 | } else {
|
95 | 98 | tokens = sentence.getTokenList().stream().map(serializer::fromProto).collect(Collectors.toList());
|
96 | 99 | }
|
97 | 100 | SemanticGraph graph = ProtobufAnnotationSerializer.fromProto(sentence.getGraph(), tokens, "semgrex");
|
| 101 | + CoreMap coremap = new ArrayCoreMap(); |
| 102 | + coremap.set(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class, graph); |
| 103 | + coremap.set(CoreAnnotations.TokensAnnotation.class, tokens); |
| 104 | + sentences.add(coremap); |
| 105 | + } |
| 106 | + |
| 107 | + List<SemgrexPattern> patterns = request.getSemgrexList().stream().map(SemgrexPattern::compile).collect(Collectors.toList()); |
| 108 | + int graphIdx = 0; |
| 109 | + for (CoreMap sentence : sentences) { |
| 110 | + CoreNLPProtos.SemgrexResponse.GraphResult.Builder graphResultBuilder = CoreNLPProtos.SemgrexResponse.GraphResult.newBuilder(); |
| 111 | + |
98 | 112 | int patternIdx = 0;
|
| 113 | + SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class); |
99 | 114 | for (SemgrexPattern pattern : patterns) {
|
100 | 115 | graphResultBuilder.addResult(matchSentence(pattern, graph, patternIdx, graphIdx));
|
101 | 116 | ++patternIdx;
|
|
0 commit comments