Skip to content

Commit

Permalink
Update the xml string output to encorporate EmptyIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
AngledLuffa committed Oct 14, 2023
1 parent e0a3807 commit 091fdc9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/edu/stanford/nlp/semgraph/SemanticGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -1556,8 +1556,22 @@ private String toXMLString() {
String dep = (edge.getTarget()).word();
int depIdx = (edge.getTarget()).index();
buf.append(" <dep type=\"").append(reln).append("\">\n");
buf.append(" <governor idx=\"").append(govIdx).append("\">").append(gov).append("</governor>\n");
buf.append(" <dependent idx=\"").append(depIdx).append("\">").append(dep).append("</dependent>\n");
buf.append(" <governor idx=\"").append(govIdx);
if (edge.getSource().hasEmptyIndex()) {
int emptyIdx = edge.getSource().getEmptyIndex();
if (emptyIdx != 0) {
buf.append("\" emptyIdx=\"").append(emptyIdx);
}
}
buf.append("\">").append(gov).append("</governor>\n");
buf.append(" <dependent idx=\"").append(depIdx);
if (edge.getTarget().hasEmptyIndex()) {
int emptyIdx = edge.getTarget().getEmptyIndex();
if (emptyIdx != 0) {
buf.append("\" emptyIdx=\"").append(emptyIdx);
}
}
buf.append("\">").append(dep).append("</dependent>\n");
buf.append(" </dep>\n");
}
buf.append("</dependencies>\n");
Expand Down
67 changes: 67 additions & 0 deletions test/src/edu/stanford/nlp/semgraph/SemanticGraphTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -495,4 +495,71 @@ public void testValueOfReuseIndices() {
Assert.assertEquals(E.index(), 4);
}

@Test
public void testXMLString() {
SemanticGraph sg = SemanticGraph.valueOf("[A/foo-0 obj> B/bar-1 obj> C/foo-2 obj> -2 dep> B/bar-1 nsubj> [D/bar-3 obj> E/baz-4]]");
String text = sg.toString(SemanticGraph.OutputFormat.XML);
String expected = ("<dependencies style=\"typed\">\n" +
" <dep type=\"dep\">\n" +
" <governor idx=\"0\">A</governor>\n" +
" <dependent idx=\"1\">B</dependent>\n" +
" </dep>\n" +
" <dep type=\"obj\">\n" +
" <governor idx=\"0\">A</governor>\n" +
" <dependent idx=\"1\">B</dependent>\n" +
" </dep>\n" +
" <dep type=\"obj\">\n" +
" <governor idx=\"0\">A</governor>\n" +
" <dependent idx=\"2\">C</dependent>\n" +
" </dep>\n" +
" <dep type=\"obj\">\n" +
" <governor idx=\"0\">A</governor>\n" +
" <dependent idx=\"2\">C</dependent>\n" +
" </dep>\n" +
" <dep type=\"nsubj\">\n" +
" <governor idx=\"0\">A</governor>\n" +
" <dependent idx=\"3\">D</dependent>\n" +
" </dep>\n" +
" <dep type=\"obj\">\n" +
" <governor idx=\"3\">D</governor>\n" +
" <dependent idx=\"4\">E</dependent>\n" +
" </dep>\n" +
"</dependencies>\n");

Assert.assertEquals(text, expected);
}

@Test
public void testXMLStringWithEmpties() {
SemanticGraph sg = SemanticGraph.valueOf("[A/foo-0 obj> B/bar-1.1 obj> C/foo-2 obj> -2 dep> B/bar-1 nsubj> [D/bar-3 obj> E/baz-4]]");
String text = sg.toString(SemanticGraph.OutputFormat.XML);
String expected = ("<dependencies style=\"typed\">\n" +
" <dep type=\"dep\">\n" +
" <governor idx=\"0\">A</governor>\n" +
" <dependent idx=\"1\">B</dependent>\n" +
" </dep>\n" +
" <dep type=\"obj\">\n" +
" <governor idx=\"0\">A</governor>\n" +
" <dependent idx=\"1\" emptyIdx=\"1\">B</dependent>\n" +
" </dep>\n" +
" <dep type=\"obj\">\n" +
" <governor idx=\"0\">A</governor>\n" +
" <dependent idx=\"2\">C</dependent>\n" +
" </dep>\n" +
" <dep type=\"obj\">\n" +
" <governor idx=\"0\">A</governor>\n" +
" <dependent idx=\"2\">C</dependent>\n" +
" </dep>\n" +
" <dep type=\"nsubj\">\n" +
" <governor idx=\"0\">A</governor>\n" +
" <dependent idx=\"3\">D</dependent>\n" +
" </dep>\n" +
" <dep type=\"obj\">\n" +
" <governor idx=\"3\">D</governor>\n" +
" <dependent idx=\"4\">E</dependent>\n" +
" </dep>\n" +
"</dependencies>\n");

Assert.assertEquals(text, expected);
}
}

0 comments on commit 091fdc9

Please sign in to comment.