Skip to content

Commit 3671ced

Browse files
committed
First step of allowing uniq for a ProcessSemgrexRequest - need to decode all sentences from the request first, then turn that into a response
1 parent 93060e1 commit 3671ced

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/edu/stanford/nlp/semgraph/semgrex/ProcessSemgrexRequest.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@
1010
import java.io.InputStream;
1111
import java.io.IOException;
1212
import java.io.OutputStream;
13+
import java.util.ArrayList;
1314
import java.util.List;
1415
import java.util.stream.Collectors;
1516

17+
import edu.stanford.nlp.ling.CoreAnnotations;
1618
import edu.stanford.nlp.ling.CoreLabel;
1719
import edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer;
1820
import edu.stanford.nlp.pipeline.CoreNLPProtos;
1921
import edu.stanford.nlp.semgraph.SemanticGraph;
22+
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
2023
import edu.stanford.nlp.semgraph.SemanticGraphEdge;
2124
import edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher;
2225
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;
2329
import edu.stanford.nlp.util.ProcessProtobufRequest;
2430

2531
public class ProcessSemgrexRequest extends ProcessProtobufRequest {
@@ -83,19 +89,28 @@ public static CoreNLPProtos.SemgrexResponse processRequest(CoreNLPProtos.Semgrex
8389
ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer();
8490
CoreNLPProtos.SemgrexResponse.Builder responseBuilder = CoreNLPProtos.SemgrexResponse.newBuilder();
8591

86-
List<SemgrexPattern> patterns = request.getSemgrexList().stream().map(SemgrexPattern::compile).collect(Collectors.toList());
87-
int graphIdx = 0;
92+
List<CoreMap> sentences = new ArrayList<>();
8893
for (CoreNLPProtos.SemgrexRequest.Dependencies sentence : request.getQueryList()) {
89-
CoreNLPProtos.SemgrexResponse.GraphResult.Builder graphResultBuilder = CoreNLPProtos.SemgrexResponse.GraphResult.newBuilder();
90-
9194
final List<CoreLabel> tokens;
9295
if (sentence.getGraph().getTokenList().size() > 0) {
9396
tokens = sentence.getGraph().getTokenList().stream().map(serializer::fromProto).collect(Collectors.toList());
9497
} else {
9598
tokens = sentence.getTokenList().stream().map(serializer::fromProto).collect(Collectors.toList());
9699
}
97100
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+
98112
int patternIdx = 0;
113+
SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
99114
for (SemgrexPattern pattern : patterns) {
100115
graphResultBuilder.addResult(matchSentence(pattern, graph, patternIdx, graphIdx));
101116
++patternIdx;

0 commit comments

Comments
 (0)