Skip to content

Semgrex uniqueness operation #1490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/edu/stanford/nlp/pipeline/StanfordCoreNLPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1348,18 +1348,12 @@ public void handle(HttpExchange httpExchange) throws IOException {
return Pair.makePair("".getBytes(), null);
}

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

ByteArrayOutputStream os = new ByteArrayOutputStream();
responseBuilder.build().writeTo(os);
semgrexResponse.writeTo(os);
os.close();

return Pair.makePair(os.toByteArray(), doc);
Expand Down
65 changes: 48 additions & 17 deletions src/edu/stanford/nlp/semgraph/semgrex/ProcessSemgrexRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,31 @@
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer;
import edu.stanford.nlp.pipeline.CoreNLPProtos;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations;
import edu.stanford.nlp.semgraph.SemanticGraphEdge;
import edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher;
import edu.stanford.nlp.semgraph.semgrex.SemgrexPattern;
import edu.stanford.nlp.util.ArrayCoreMap;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.Pair;
import edu.stanford.nlp.util.ProcessProtobufRequest;

public class ProcessSemgrexRequest extends ProcessProtobufRequest {
/**
* Builds a single inner SemgrexResult structure from the pair of a SemgrexPattern and a SemanticGraph
*/
public static CoreNLPProtos.SemgrexResponse.SemgrexResult matchSentence(SemgrexPattern pattern, SemanticGraph graph, int patternIdx, int graphIdx) {
public static CoreNLPProtos.SemgrexResponse.SemgrexResult matchSentence(SemgrexPattern pattern, SemanticGraph graph, List<SemgrexMatch> matches, int patternIdx, int graphIdx) {
CoreNLPProtos.SemgrexResponse.SemgrexResult.Builder semgrexResultBuilder = CoreNLPProtos.SemgrexResponse.SemgrexResult.newBuilder();
SemgrexMatcher matcher = pattern.matcher(graph);
while (matcher.find()) {
for (SemgrexMatch matcher : matches) {
CoreNLPProtos.SemgrexResponse.Match.Builder matchBuilder = CoreNLPProtos.SemgrexResponse.Match.newBuilder();
matchBuilder.setMatchIndex(matcher.getMatch().index());
matchBuilder.setSemgrexIndex(patternIdx);
Expand Down Expand Up @@ -74,37 +79,63 @@ public static CoreNLPProtos.SemgrexResponse.SemgrexResult matchSentence(SemgrexP
return semgrexResultBuilder.build();
}

public static CoreNLPProtos.SemgrexResponse processRequest(List<CoreMap> sentences, List<SemgrexPattern> patterns) {
CoreNLPProtos.SemgrexResponse.Builder responseBuilder = CoreNLPProtos.SemgrexResponse.newBuilder();
List<Pair<CoreMap, List<Pair<SemgrexPattern, List<SemgrexMatch>>>>> allMatches = new ArrayList<>();
for (CoreMap sentence : sentences) {
allMatches.add(new Pair<>(sentence, new ArrayList<>()));
}
for (SemgrexPattern pattern : patterns) {
List<Pair<CoreMap, List<SemgrexMatch>>> patternMatches = pattern.matchSentences(sentences, true);
for (int i = 0; i < sentences.size(); ++i) {
Pair<CoreMap, List<SemgrexMatch>> sentenceMatches = patternMatches.get(i);
allMatches.get(i).second().add(new Pair<>(pattern, sentenceMatches.second()));
}
}

int graphIdx = 0;
for (Pair<CoreMap, List<Pair<SemgrexPattern, List<SemgrexMatch>>>> sentenceMatches : allMatches) {
CoreNLPProtos.SemgrexResponse.GraphResult.Builder graphResultBuilder = CoreNLPProtos.SemgrexResponse.GraphResult.newBuilder();

int patternIdx = 0;
SemanticGraph graph = sentenceMatches.first().get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
for (Pair<SemgrexPattern, List<SemgrexMatch>> patternMatches : sentenceMatches.second()) {
SemgrexPattern pattern = patternMatches.first();
graphResultBuilder.addResult(matchSentence(pattern, graph, patternMatches.second(), patternIdx, graphIdx));
++patternIdx;
}

responseBuilder.addResult(graphResultBuilder.build());
++graphIdx;
}
return responseBuilder.build();
}

/**
* For a single request, iterate through the SemanticGraphs it
* includes, and add the results of each Semgrex operation included
* in the request.
*/
public static CoreNLPProtos.SemgrexResponse processRequest(CoreNLPProtos.SemgrexRequest request) {
ProtobufAnnotationSerializer serializer = new ProtobufAnnotationSerializer();
CoreNLPProtos.SemgrexResponse.Builder responseBuilder = CoreNLPProtos.SemgrexResponse.newBuilder();

List<SemgrexPattern> patterns = request.getSemgrexList().stream().map(SemgrexPattern::compile).collect(Collectors.toList());
int graphIdx = 0;
List<CoreMap> sentences = new ArrayList<>();
for (CoreNLPProtos.SemgrexRequest.Dependencies sentence : request.getQueryList()) {
CoreNLPProtos.SemgrexResponse.GraphResult.Builder graphResultBuilder = CoreNLPProtos.SemgrexResponse.GraphResult.newBuilder();

final List<CoreLabel> tokens;
if (sentence.getGraph().getTokenList().size() > 0) {
tokens = sentence.getGraph().getTokenList().stream().map(serializer::fromProto).collect(Collectors.toList());
} else {
tokens = sentence.getTokenList().stream().map(serializer::fromProto).collect(Collectors.toList());
}
SemanticGraph graph = ProtobufAnnotationSerializer.fromProto(sentence.getGraph(), tokens, "semgrex");
int patternIdx = 0;
for (SemgrexPattern pattern : patterns) {
graphResultBuilder.addResult(matchSentence(pattern, graph, patternIdx, graphIdx));
++patternIdx;
}

responseBuilder.addResult(graphResultBuilder.build());
++graphIdx;
CoreMap coremap = new ArrayCoreMap();
coremap.set(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class, graph);
coremap.set(CoreAnnotations.TokensAnnotation.class, tokens);
sentences.add(coremap);
}
return responseBuilder.build();

List<SemgrexPattern> patterns = request.getSemgrexList().stream().map(SemgrexPattern::compile).collect(Collectors.toList());
return processRequest(sentences, patterns);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/edu/stanford/nlp/semgraph/semgrex/SemgrexMatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ public Set<String> getNodeNames() {
return namesToNodes.keySet();
}

public Set<String> getRelationNames() {
return namesToRelations.keySet();
}

public String getRelnString(String name) {
return namesToRelations.get(name);
}

public Set<String> getEdgeNames() {
return namesToEdges.keySet();
}

public SemanticGraphEdge getEdge(String name) {
return namesToEdges.get(name);
}

public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(matchedPattern);
Expand Down
Loading