Skip to content

Commit cfd1f21

Browse files
committed
Refactor the ProcessSemgrexRequest and make the CoreNLPServer use it as well
1 parent 83e9d4b commit cfd1f21

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

src/edu/stanford/nlp/pipeline/StanfordCoreNLPServer.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,18 +1348,12 @@ public void handle(HttpExchange httpExchange) throws IOException {
13481348
return Pair.makePair("".getBytes(), null);
13491349
}
13501350

1351-
CoreNLPProtos.SemgrexResponse.Builder responseBuilder = CoreNLPProtos.SemgrexResponse.newBuilder();
1352-
int sentenceIdx = 0;
1353-
for (CoreMap sentence : doc.get(CoreAnnotations.SentencesAnnotation.class)) {
1354-
SemanticGraph graph = sentence.get(dependenciesType.annotation());
1355-
CoreNLPProtos.SemgrexResponse.GraphResult.Builder graphResultBuilder = CoreNLPProtos.SemgrexResponse.GraphResult.newBuilder();
1356-
graphResultBuilder.addResult(ProcessSemgrexRequest.matchSentence(regex, graph, 0, sentenceIdx));
1357-
responseBuilder.addResult(graphResultBuilder.build());
1358-
++sentenceIdx;
1359-
}
1351+
List<CoreMap> sentences = doc.get(CoreAnnotations.SentencesAnnotation.class);
1352+
List<SemgrexPattern> patterns = Collections.singletonList(regex);
1353+
CoreNLPProtos.SemgrexResponse semgrexResponse = ProcessSemgrexRequest.processRequest(sentences, patterns);
13601354

13611355
ByteArrayOutputStream os = new ByteArrayOutputStream();
1362-
responseBuilder.build().writeTo(os);
1356+
semgrexResponse.writeTo(os);
13631357
os.close();
13641358

13651359
return Pair.makePair(os.toByteArray(), doc);

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,31 +79,8 @@ public static CoreNLPProtos.SemgrexResponse.SemgrexResult matchSentence(SemgrexP
7979
return semgrexResultBuilder.build();
8080
}
8181

82-
/**
83-
* For a single request, iterate through the SemanticGraphs it
84-
* includes, and add the results of each Semgrex operation included
85-
* in the request.
86-
*/
87-
public static CoreNLPProtos.SemgrexResponse processRequest(CoreNLPProtos.SemgrexRequest request) {
88-
ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer();
82+
public static CoreNLPProtos.SemgrexResponse processRequest(List<CoreMap> sentences, List<SemgrexPattern> patterns) {
8983
CoreNLPProtos.SemgrexResponse.Builder responseBuilder = CoreNLPProtos.SemgrexResponse.newBuilder();
90-
91-
List<CoreMap> sentences = new ArrayList<>();
92-
for (CoreNLPProtos.SemgrexRequest.Dependencies sentence : request.getQueryList()) {
93-
final List<CoreLabel> tokens;
94-
if (sentence.getGraph().getTokenList().size() > 0) {
95-
tokens = sentence.getGraph().getTokenList().stream().map(serializer::fromProto).collect(Collectors.toList());
96-
} else {
97-
tokens = sentence.getTokenList().stream().map(serializer::fromProto).collect(Collectors.toList());
98-
}
99-
SemanticGraph graph = ProtobufAnnotationSerializer.fromProto(sentence.getGraph(), tokens, "semgrex");
100-
CoreMap coremap = new ArrayCoreMap();
101-
coremap.set(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class, graph);
102-
coremap.set(CoreAnnotations.TokensAnnotation.class, tokens);
103-
sentences.add(coremap);
104-
}
105-
106-
List<SemgrexPattern> patterns = request.getSemgrexList().stream().map(SemgrexPattern::compile).collect(Collectors.toList());
10784
List<Pair<CoreMap, List<Pair<SemgrexPattern, List<SemgrexMatch>>>>> allMatches = new ArrayList<>();
10885
for (CoreMap sentence : sentences) {
10986
allMatches.add(new Pair<>(sentence, new ArrayList<>()));
@@ -134,6 +111,33 @@ public static CoreNLPProtos.SemgrexResponse processRequest(CoreNLPProtos.Semgrex
134111
return responseBuilder.build();
135112
}
136113

114+
/**
115+
* For a single request, iterate through the SemanticGraphs it
116+
* includes, and add the results of each Semgrex operation included
117+
* in the request.
118+
*/
119+
public static CoreNLPProtos.SemgrexResponse processRequest(CoreNLPProtos.SemgrexRequest request) {
120+
ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer();
121+
122+
List<CoreMap> sentences = new ArrayList<>();
123+
for (CoreNLPProtos.SemgrexRequest.Dependencies sentence : request.getQueryList()) {
124+
final List<CoreLabel> tokens;
125+
if (sentence.getGraph().getTokenList().size() > 0) {
126+
tokens = sentence.getGraph().getTokenList().stream().map(serializer::fromProto).collect(Collectors.toList());
127+
} else {
128+
tokens = sentence.getTokenList().stream().map(serializer::fromProto).collect(Collectors.toList());
129+
}
130+
SemanticGraph graph = ProtobufAnnotationSerializer.fromProto(sentence.getGraph(), tokens, "semgrex");
131+
CoreMap coremap = new ArrayCoreMap();
132+
coremap.set(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class, graph);
133+
coremap.set(CoreAnnotations.TokensAnnotation.class, tokens);
134+
sentences.add(coremap);
135+
}
136+
137+
List<SemgrexPattern> patterns = request.getSemgrexList().stream().map(SemgrexPattern::compile).collect(Collectors.toList());
138+
return processRequest(sentences, patterns);
139+
}
140+
137141
/**
138142
* Reads a single request from the InputStream, then writes back a single response.
139143
*/

0 commit comments

Comments
 (0)