From 203eb065cbd86e34ae9388fe6515ef278d580374 Mon Sep 17 00:00:00 2001 From: John Bauer Date: Wed, 5 Apr 2023 09:32:42 -0700 Subject: [PATCH] Allow dashes as the word in an expression with indices in SemanticGraph.valueOf --- .../stanford/nlp/semgraph/SemanticGraph.java | 2 +- .../nlp/semgraph/SemanticGraphTest.java | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/edu/stanford/nlp/semgraph/SemanticGraph.java b/src/edu/stanford/nlp/semgraph/SemanticGraph.java index 02dd67b84c..502eff6257 100644 --- a/src/edu/stanford/nlp/semgraph/SemanticGraph.java +++ b/src/edu/stanford/nlp/semgraph/SemanticGraph.java @@ -1861,7 +1861,7 @@ public SemanticGraph makeSoftCopy() { // ============================================================================ - private static final Pattern WORD_AND_INDEX_PATTERN = Pattern.compile("([^-]*)-([0-9]+)"); + private static final Pattern WORD_AND_INDEX_PATTERN = Pattern.compile("(.*)-([0-9]+)"); /** * This nested class is a helper for valueOf(). It represents the task of diff --git a/test/src/edu/stanford/nlp/semgraph/SemanticGraphTest.java b/test/src/edu/stanford/nlp/semgraph/SemanticGraphTest.java index 44484b8515..e76bc07f07 100644 --- a/test/src/edu/stanford/nlp/semgraph/SemanticGraphTest.java +++ b/test/src/edu/stanford/nlp/semgraph/SemanticGraphTest.java @@ -304,6 +304,32 @@ public void testValueOfSimple() { assertEquals(sg.getParentsWithReln(E, "dep").size(), 0); } + /** + * Test that dashes as the word work as expected with indices + */ + public void testValueOfDashes() { + SemanticGraph sg = SemanticGraph.valueOf("[--3 obj> -/bar-1 obj> C-4 nsubj> [D-2 obj> E-0]]"); + + List words = sg.vertexListSorted(); + assertEquals(words.size(), 5); + + for (int i = 0; i < 5; ++i) { + assertEquals(words.get(i).index(), i); + } + IndexedWord A = words.get(3); + IndexedWord B = words.get(1); + IndexedWord C = words.get(4); + IndexedWord D = words.get(2); + IndexedWord E = words.get(0); + + assertEquals(A.word(), "-"); + assertEquals(B.word(), "-"); + assertEquals(B.tag(), "bar"); + assertEquals(C.word(), "C"); + assertEquals(D.word(), "D"); + assertEquals(E.word(), "E"); + } + /** * Test the vertices and edges of a very simple valueOf graph with indices added */