public void testShortestPath() { // graph.prettyPrint(); IndexedWord word1 = graph.getNodeByIndex(10); IndexedWord word2 = graph.getNodeByIndex(14); // System.out.println("word1: " + word1); // System.out.println("word1: " + word1.hashCode()); // System.out.println("word2: " + word2); // System.out.println("word2: " + word2.hashCode()); // System.out.println("word eq: " + word1.equals(word2)); // System.out.println("word eq: " + (word1.hashCode() == word2.hashCode())); // System.out.println("word eq: " + (word1.toString().equals(word2.toString()))); List<SemanticGraphEdge> edges = graph.getShortestUndirectedPathEdges(word1, word2); // System.out.println("path: " + edges); assertNotNull(edges); List<IndexedWord> nodes = graph.getShortestUndirectedPathNodes(word1, word2); // System.out.println("path: " + nodes); assertNotNull(nodes); assertEquals(word1, nodes.get(0)); assertEquals(word2, nodes.get(nodes.size() - 1)); edges = graph.getShortestUndirectedPathEdges(word1, word1); // System.out.println("path: " + edges); assertNotNull(edges); assertEquals(0, edges.size()); nodes = graph.getShortestUndirectedPathNodes(word1, word1); // System.out.println("path: " + nodes); assertNotNull(nodes); assertEquals(1, nodes.size()); assertEquals(word1, nodes.get(0)); }
public void testIsAncestor() { // System.err.println(graph.toString(CoreLabel.VALUE_TAG_INDEX_FORMAT)); assertEquals(1, graph.isAncestor(graph.getNodeByIndex(42), graph.getNodeByIndex(45))); assertEquals(2, graph.isAncestor(graph.getNodeByIndex(40), graph.getNodeByIndex(37))); assertEquals(-1, graph.isAncestor(graph.getNodeByIndex(40), graph.getNodeByIndex(38))); assertEquals(-1, graph.isAncestor(graph.getNodeByIndex(40), graph.getNodeByIndex(10))); assertEquals(-1, graph.isAncestor(graph.getNodeByIndex(45), graph.getNodeByIndex(42))); }
public void testHasChildren() { SemanticGraph gr = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]"); List<IndexedWord> vertices = gr.vertexListSorted(); for (IndexedWord word : vertices) { if (word.word().equals("ate") || word.word().equals("muffins")) { assertTrue(gr.hasChildren(word)); } else { assertFalse(gr.hasChildren(word)); } } }
/** * Tests that a particular topological sort is correct by verifying for each node that it appears * in the sort and all of its children occur later in the sort */ private static void verifyTopologicalSort(SemanticGraph graph) { List<IndexedWord> sorted = graph.topologicalSort(); Map<IndexedWord, Integer> indices = Generics.newHashMap(); for (int index = 0; index < sorted.size(); ++index) { indices.put(sorted.get(index), index); } for (IndexedWord parent : graph.vertexSet()) { assertTrue(indices.containsKey(parent)); int parentIndex = indices.get(parent); for (IndexedWord child : graph.getChildren(parent)) { assertTrue(indices.containsKey(child)); int childIndex = indices.get(child); assertTrue(parentIndex < childIndex); } } }
public void testGetCommonAncestor() { IndexedWord common = graph.getCommonAncestor(graph.getNodeByIndex(43), graph.getNodeByIndex(44)); assertEquals(45, common.index()); common = graph.getCommonAncestor(graph.getNodeByIndex(41), graph.getNodeByIndex(39)); assertEquals(41, common.index()); common = graph.getCommonAncestor(graph.getNodeByIndex(39), graph.getNodeByIndex(41)); assertEquals(41, common.index()); common = graph.getCommonAncestor(graph.getNodeByIndex(40), graph.getNodeByIndex(42)); assertEquals(41, common.index()); // too far for this method common = graph.getCommonAncestor(graph.getNodeByIndex(10), graph.getNodeByIndex(42)); assertEquals(null, common); common = graph.getCommonAncestor(graph.getNodeByIndex(10), graph.getNodeByIndex(10)); assertEquals(10, common.index()); common = graph.getCommonAncestor(graph.getNodeByIndex(40), graph.getNodeByIndex(40)); assertEquals(40, common.index()); // a couple tests at the top of the graph common = graph.getCommonAncestor(graph.getNodeByIndex(10), graph.getNodeByIndex(1)); assertEquals(10, common.index()); common = graph.getCommonAncestor(graph.getNodeByIndex(1), graph.getNodeByIndex(10)); assertEquals(10, common.index()); }
public void testGetSiblings() { verifySet(graph.getSiblings(graph.getNodeByIndex(43)), 42, 44, 48); verifySet(graph.getSiblings(graph.getNodeByIndex(10))); // empty set verifySet(graph.getSiblings(graph.getNodeByIndex(42)), 43, 44, 48); }
public void testGetPathToRoot() { verifyPath(graph.getPathToRoot(graph.getNodeByIndex(1)), 4, 10); verifyPath(graph.getPathToRoot(graph.getNodeByIndex(10))); // empty path verifyPath(graph.getPathToRoot(graph.getNodeByIndex(34)), 35, 28, 10); }
public void testTopologicalSort() { SemanticGraph gr = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]"); verifyTopologicalSort(gr); List<IndexedWord> vertices = gr.vertexListSorted(); gr.addEdge( vertices.get(1), vertices.get(2), UniversalEnglishGrammaticalRelations.DIRECT_OBJECT, 1.0, false); verifyTopologicalSort(gr); gr = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]"); vertices = gr.vertexListSorted(); gr.addEdge( vertices.get(2), vertices.get(1), UniversalEnglishGrammaticalRelations.DIRECT_OBJECT, 1.0, false); verifyTopologicalSort(gr); gr = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]"); vertices = gr.vertexListSorted(); gr.addEdge( vertices.get(1), vertices.get(3), UniversalEnglishGrammaticalRelations.DIRECT_OBJECT, 1.0, false); verifyTopologicalSort(gr); // now create a graph with a directed loop, which we should not // be able to topologically sort gr = SemanticGraph.valueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]"); vertices = gr.vertexListSorted(); gr.addEdge( vertices.get(3), vertices.get(0), UniversalEnglishGrammaticalRelations.DIRECT_OBJECT, 1.0, false); try { verifyTopologicalSort(gr); throw new RuntimeException("Expected to fail"); } catch (IllegalStateException e) { // yay, correctly caught error } }
public void testCommonAncestor() { assertEquals(1, graph.commonAncestor(graph.getNodeByIndex(43), graph.getNodeByIndex(44))); assertEquals(1, graph.commonAncestor(graph.getNodeByIndex(41), graph.getNodeByIndex(39))); assertEquals(1, graph.commonAncestor(graph.getNodeByIndex(39), graph.getNodeByIndex(41))); assertEquals(2, graph.commonAncestor(graph.getNodeByIndex(40), graph.getNodeByIndex(42))); assertEquals(2, graph.commonAncestor(graph.getNodeByIndex(42), graph.getNodeByIndex(40))); // too far for this method assertEquals(-1, graph.commonAncestor(graph.getNodeByIndex(10), graph.getNodeByIndex(42))); // assertEquals(null, common); assertEquals(0, graph.commonAncestor(graph.getNodeByIndex(10), graph.getNodeByIndex(10))); assertEquals(0, graph.commonAncestor(graph.getNodeByIndex(40), graph.getNodeByIndex(40))); // assertEquals(40, common.index()); // a couple tests at the top of the graph assertEquals(2, graph.commonAncestor(graph.getNodeByIndex(10), graph.getNodeByIndex(1))); assertEquals(2, graph.commonAncestor(graph.getNodeByIndex(1), graph.getNodeByIndex(10))); }