Skip to content

Commit 15e6186

Browse files
committed
Add utilities to check the min & max index of a SemanticGraph
1 parent 1c19313 commit 15e6186

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/edu/stanford/nlp/semgraph/SemanticGraphUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,4 +1287,24 @@ public static boolean isTree(SemanticGraph sg) {
12871287

12881288
return visitedNodes.size() == sg.size();
12891289
}
1290+
1291+
public static int maxIndex(SemanticGraph sg) {
1292+
int index = Integer.MIN_VALUE;
1293+
for (IndexedWord node : sg.vertexSet()) {
1294+
if (node.index() > index) {
1295+
index = node.index();
1296+
}
1297+
}
1298+
return index;
1299+
}
1300+
1301+
public static int minIndex(SemanticGraph sg) {
1302+
int index = Integer.MAX_VALUE;
1303+
for (IndexedWord node : sg.vertexSet()) {
1304+
if (node.index() < index) {
1305+
index = node.index();
1306+
}
1307+
}
1308+
return index;
1309+
}
12901310
}

test/src/edu/stanford/nlp/semgraph/SemanticGraphUtilsTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,34 @@ public void testCreateSemgrexPattern(){
3535
assertEquals("{word: ate; tag: null; ner: null}=ate >subj=E1 {word: bill; tag: null; ner: null}=Bill", pat.trim());
3636
}
3737

38+
@Test
39+
public void testMaxIndex() {
40+
SemanticGraph sg = SemanticGraph.valueOf("[has-2 nsubj> Jennifer-1 obj> [antennae-4 dep> blue-3]]");
41+
assertEquals(4, SemanticGraphUtils.maxIndex(sg));
42+
43+
// with a weird index
44+
sg = SemanticGraph.valueOf("[has-2 nsubj> Jennifer-1 obj> [antennae-5 dep> blue-3]]");
45+
assertEquals(5, SemanticGraphUtils.maxIndex(sg));
46+
47+
sg = SemanticGraph.valueOf("[has-1 nsubj> Jennifer-0 obj> [antennae-3 dep> blue-2]]");
48+
assertEquals(3, SemanticGraphUtils.maxIndex(sg));
49+
50+
sg = SemanticGraph.valueOf("[foo-1]");
51+
assertEquals(1, SemanticGraphUtils.maxIndex(sg));
52+
}
53+
54+
@Test
55+
public void testMinIndex() {
56+
SemanticGraph sg = SemanticGraph.valueOf("[has-2 nsubj> Jennifer-1 obj> [antennae-4 dep> blue-3]]");
57+
assertEquals(1, SemanticGraphUtils.minIndex(sg));
58+
59+
sg = SemanticGraph.valueOf("[has-2 nsubj> Jennifer-1 obj> [antennae-5 dep> blue-3]]");
60+
assertEquals(1, SemanticGraphUtils.minIndex(sg));
61+
62+
sg = SemanticGraph.valueOf("[has-1 nsubj> Jennifer-0 obj> [antennae-3 dep> blue-2]]");
63+
assertEquals(0, SemanticGraphUtils.minIndex(sg));
64+
65+
sg = SemanticGraph.valueOf("[foo-1]");
66+
assertEquals(1, SemanticGraphUtils.minIndex(sg));
67+
}
3868
}

0 commit comments

Comments
 (0)