Skip to content

Commit 195e259

Browse files
committed
Zero argument commmands in Ssurgeon need slightly different arg parsing. This is the only bugfix needed to make killNonRooted work as expected
1 parent 2823808 commit 195e259

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/Ssurgeon.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,12 @@ private static String[] parseArgs(String argsString) {
299299
*/
300300
public static SsurgeonEdit parseEditLine(String editLine) {
301301
// Extract the operation name first
302-
String[] tuples1 = editLine.split("\\s+", 2);
303-
if (tuples1.length < 2) {
302+
final String[] tuples1 = editLine.split("\\s+", 2);
303+
if (tuples1.length < 1) {
304304
throw new SsurgeonParseException("Error in SsurgeonEdit.parseEditLine: invalid number of arguments");
305305
}
306-
String command = tuples1[0];
307-
String[] argsArray = parseArgs(tuples1[1]);
306+
final String command = tuples1[0];
307+
final String[] argsArray = tuples1.length == 1 ? new String[0] : parseArgs(tuples1[1]);
308308
SsurgeonArgs argsBox = new SsurgeonArgs();
309309

310310
for (int argIndex = 0; argIndex < argsArray.length; ++argIndex) {

test/src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/SsurgeonTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,71 @@ public void readXMLPruneNodesResetRoots() {
267267
assertEquals(SemanticGraph.valueOf("[C-2]"), pruneSG);
268268
}
269269

270+
/**
271+
* Check that cutting a graph with two nodes into two pieces, then
272+
* pruning any disjoint pieces, results in a graph with just the root
273+
*/
274+
@Test
275+
public void readXMLKillNonRootedIterate() {
276+
Ssurgeon inst = Ssurgeon.inst();
277+
278+
String cut = String.join(newline,
279+
"<ssurgeon-pattern-list>",
280+
" <ssurgeon-pattern>",
281+
" <uid>38</uid>",
282+
" <notes>Remove dep edges</notes>",
283+
" <semgrex>" + XMLUtils.escapeXML("{}=a1 > {}=a2") + "</semgrex>",
284+
" <edit-list>removeEdge -gov a1 -dep a2 -reln dep</edit-list>",
285+
" </ssurgeon-pattern>",
286+
"</ssurgeon-pattern-list>");
287+
List<SsurgeonPattern> patterns = inst.readFromString(cut);
288+
assertEquals(patterns.size(), 1);
289+
SsurgeonPattern ssurgeonCut = patterns.get(0);
290+
291+
String prune = String.join(newline,
292+
"<ssurgeon-pattern-list>",
293+
" <ssurgeon-pattern>",
294+
" <uid>38</uid>",
295+
" <notes>Match every graph, kill unrooted nodes</notes>",
296+
" <semgrex>" + XMLUtils.escapeXML("{$}") + "</semgrex>",
297+
" <edit-list>killNonRooted</edit-list>",
298+
" </ssurgeon-pattern>",
299+
"</ssurgeon-pattern-list>");
300+
patterns = inst.readFromString(prune);
301+
assertEquals(patterns.size(), 1);
302+
SsurgeonPattern ssurgeonPrune = patterns.get(0);
303+
304+
// Test a two node only version
305+
SemanticGraph sg = SemanticGraph.valueOf("[A dep> B]");
306+
SemanticGraph cutSG = ssurgeonCut.iterate(sg);
307+
assertEquals(cutSG.vertexSet().size(), 2);
308+
SemanticGraph pruneSG = ssurgeonPrune.iterate(cutSG);
309+
SemanticGraph expected = SemanticGraph.valueOf("[A]");
310+
assertEquals(pruneSG, expected);
311+
312+
// Test a chain cut at the start
313+
sg = SemanticGraph.valueOf("[A dep> [B obj> C]]");
314+
cutSG = ssurgeonCut.iterate(sg);
315+
assertEquals(cutSG.vertexSet().size(), 3);
316+
pruneSG = ssurgeonPrune.iterate(cutSG);
317+
assertEquals(pruneSG, expected);
318+
319+
// Test the chain cut at the bottom
320+
sg = SemanticGraph.valueOf("[A obj> [B dep> C]]");
321+
cutSG = ssurgeonCut.iterate(sg);
322+
assertEquals(cutSG.vertexSet().size(), 3);
323+
pruneSG = ssurgeonPrune.iterate(cutSG);
324+
assertEquals(pruneSG, SemanticGraph.valueOf("[A obj> B]"));
325+
326+
// Test a chain cut at the start
327+
// Only the root will be left at the end
328+
sg = SemanticGraph.valueOf("[A dep> B dep> C]");
329+
cutSG = ssurgeonCut.iterate(sg);
330+
assertEquals(cutSG.vertexSet().size(), 3);
331+
pruneSG = ssurgeonPrune.iterate(cutSG);
332+
assertEquals(pruneSG, expected);
333+
}
334+
270335
/**
271336
* Simple test of an Ssurgeon edit script. This instances a simple semantic graph,
272337
* a semgrex pattern, and then the resulting actions over the named nodes in the

0 commit comments

Comments
 (0)