Skip to content

Commit 73f0163

Browse files
committed
Save the edge being matched in the GraphRelation.
- Pass around a map for matching edges - Setting ~name on a relation in the SemgrexPattern keeps the matched edge in the map - this only applies to relations which are immediate parent or child relations: > >-- >++ < <-- <++ if set on any other relation, an exception is thrown (test included) - includes a test of the edge naming and backreferences for the various relations - document the feature in the SemgrexPattern javadoc
1 parent 2d1120e commit 73f0163

File tree

10 files changed

+348
-96
lines changed

10 files changed

+348
-96
lines changed

src/edu/stanford/nlp/semgraph/semgrex/CoordinationPattern.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import edu.stanford.nlp.ling.IndexedWord;
77
import edu.stanford.nlp.semgraph.SemanticGraph;
8+
import edu.stanford.nlp.semgraph.SemanticGraphEdge;
89

910
/** @author Chloe Kiddon */
1011
public class CoordinationPattern extends SemgrexPattern {
@@ -102,10 +103,11 @@ public String toString(boolean hasPrecedence) {
102103
public SemgrexMatcher matcher(SemanticGraph sg, IndexedWord node,
103104
Map<String, IndexedWord> namesToNodes,
104105
Map<String, String> namesToRelations,
106+
Map<String, SemanticGraphEdge> namesToEdges,
105107
VariableStrings variableStrings,
106108
boolean ignoreCase) {
107109
return new CoordinationMatcher(this, sg, null, null, true, node,
108-
namesToNodes, namesToRelations,
110+
namesToNodes, namesToRelations, namesToEdges,
109111
variableStrings, ignoreCase);
110112
}
111113

@@ -115,11 +117,12 @@ public SemgrexMatcher matcher(SemanticGraph sg,
115117
boolean hypToText, IndexedWord node,
116118
Map<String, IndexedWord> namesToNodes,
117119
Map<String, String> namesToRelations,
120+
Map<String, SemanticGraphEdge> namesToEdges,
118121
VariableStrings variableStrings,
119122
boolean ignoreCase) {
120123
return new CoordinationMatcher(this, sg, alignment, sg_align,
121124
hypToText, node,
122-
namesToNodes, namesToRelations,
125+
namesToNodes, namesToRelations, namesToEdges,
123126
variableStrings, ignoreCase);
124127
}
125128

@@ -136,16 +139,18 @@ private static class CoordinationMatcher extends SemgrexMatcher {
136139
public CoordinationMatcher(CoordinationPattern c, SemanticGraph sg, Alignment alignment,
137140
SemanticGraph sg_align, boolean hypToText, IndexedWord n,
138141
Map<String, IndexedWord> namesToNodes,
139-
Map<String, String> namesToRelations, VariableStrings variableStrings,
142+
Map<String, String> namesToRelations,
143+
Map<String, SemanticGraphEdge> namesToEdges,
144+
VariableStrings variableStrings,
140145
boolean ignoreCase) {
141-
super(sg, alignment, sg_align, hypToText, n, namesToNodes, namesToRelations, variableStrings);
146+
super(sg, alignment, sg_align, hypToText, n, namesToNodes, namesToRelations, namesToEdges, variableStrings);
142147
myNode = c;
143148
children = new SemgrexMatcher[myNode.children.size()];
144149
for (int i = 0; i < children.length; i++) {
145150
SemgrexPattern node = myNode.children.get(i);
146151
children[i] = node.matcher(sg, alignment, sg_align, hypToText,
147152
n, namesToNodes,
148-
namesToRelations, variableStrings, ignoreCase);
153+
namesToRelations, namesToEdges, variableStrings, ignoreCase);
149154
}
150155
currChild = 0;
151156
considerAll = myNode.isConj ^ myNode.isNegated();

src/edu/stanford/nlp/semgraph/semgrex/GraphRelation.java

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ abstract class GraphRelation implements Serializable {
3737
final String rawType;
3838

3939
final String name;
40+
final String edgeName;
4041

4142
//"<" | ">" | ">>" | "<<" | "<#" | ">#" | ":" | "@">
4243

@@ -53,11 +54,16 @@ abstract class GraphRelation implements Serializable {
5354
*/
5455
abstract Iterator<IndexedWord> searchNodeIterator(IndexedWord node, SemanticGraph sg);
5556

56-
private GraphRelation(String symbol, String type, String name) {
57+
private GraphRelation(String symbol, String type, String name, String edgeName) {
5758
this.symbol = symbol;
5859
this.type = getPattern(type);
5960
this.rawType = type;
6061
this.name = name;
62+
this.edgeName = edgeName;
63+
}
64+
65+
private GraphRelation(String symbol, String type, String name) {
66+
this(symbol, type, name, null);
6167
}
6268

6369
private GraphRelation(String symbol, String type) {
@@ -70,7 +76,7 @@ private GraphRelation(String symbol) {
7076

7177
@Override
7278
public String toString() {
73-
return symbol + ((rawType != null) ? rawType : "") + ((name != null) ? "=" + name : "");
79+
return symbol + ((rawType != null) ? rawType : "") + ((name != null) ? "=" + name : "") + ((edgeName != null) ? "~" + edgeName : "");
7480
}
7581

7682
public Predicate<String> getPattern(String relnType)
@@ -89,6 +95,11 @@ public String getName() {
8995
return name;
9096
}
9197

98+
public String getEdgeName() {
99+
if (edgeName == null || edgeName == "") return null;
100+
return edgeName;
101+
}
102+
92103

93104
// ALIGNMENT graph relation: "@" ==============================================
94105

@@ -237,8 +248,8 @@ void initialize() {
237248
// GOVERNOR graph relation: ">" ===============================================
238249

239250
static private class GOVERNOR extends GraphRelation {
240-
GOVERNOR(String reln, String name) {
241-
super(">", reln, name);
251+
GOVERNOR(String reln, String name, String edgeName) {
252+
super(">", reln, name, edgeName);
242253
}
243254

244255
@Override
@@ -274,9 +285,11 @@ public void advance() {
274285
continue;
275286
}
276287
this.next = edge.getTarget();
288+
this.edge = edge;
277289
return;
278290
}
279291
this.next = null;
292+
this.edge = null;
280293
}
281294
};
282295
}
@@ -286,8 +299,8 @@ public void advance() {
286299
};
287300

288301
static private class GOVERNOR_RIGHT extends GraphRelation {
289-
GOVERNOR_RIGHT(String reln, String name) {
290-
super(">++", reln, name);
302+
GOVERNOR_RIGHT(String reln, String name, String edgeName) {
303+
super(">++", reln, name, edgeName);
291304
}
292305

293306

@@ -326,10 +339,12 @@ public void advance() {
326339
}
327340
if (node.index() < edge.getTarget().index()) {
328341
this.next = edge.getTarget();
342+
this.edge = edge;
329343
return;
330344
}
331345
}
332346
this.next = null;
347+
this.edge = null;
333348
}
334349
};
335350
}
@@ -339,8 +354,8 @@ public void advance() {
339354
}
340355

341356
static private class GOVERNOR_LEFT extends GraphRelation {
342-
GOVERNOR_LEFT(String reln, String name) {
343-
super(">--", reln, name);
357+
GOVERNOR_LEFT(String reln, String name, String edgeName) {
358+
super(">--", reln, name, edgeName);
344359
}
345360

346361

@@ -379,10 +394,12 @@ public void advance() {
379394
}
380395
if (node.index() > edge.getTarget().index()) {
381396
this.next = edge.getTarget();
397+
this.edge = edge;
382398
return;
383399
}
384400
}
385401
this.next = null;
402+
this.edge = null;
386403
}
387404
};
388405
}
@@ -394,8 +411,8 @@ public void advance() {
394411
// DEPENDENT graph relation: "<" ===============================================
395412

396413
static private class DEPENDENT extends GraphRelation {
397-
DEPENDENT(String reln, String name) {
398-
super("<", reln, name);
414+
DEPENDENT(String reln, String name, String edgeName) {
415+
super("<", reln, name, edgeName);
399416
}
400417

401418
@Override
@@ -431,11 +448,13 @@ public void advance() {
431448
continue;
432449
}
433450
this.next = edge.getSource();
451+
this.edge = edge;
434452
return;
435453
}
436454
this.next = null;
455+
this.edge = null;
437456
}
438-
};
457+
};
439458
}
440459

441460
// automatically generated by Eclipse
@@ -444,8 +463,8 @@ public void advance() {
444463

445464

446465
static private class DEPENDENT_RIGHT extends GraphRelation {
447-
DEPENDENT_RIGHT(String reln, String name) {
448-
super("<++", reln, name);
466+
DEPENDENT_RIGHT(String reln, String name, String edgeName) {
467+
super("<++", reln, name, edgeName);
449468
}
450469

451470
@Override
@@ -485,10 +504,12 @@ public void advance() {
485504
}
486505
if (node.index() < edge.getSource().index()) {
487506
this.next = edge.getSource();
507+
this.edge = edge;
488508
return;
489509
}
490510
}
491511
this.next = null;
512+
this.edge = null;
492513
}
493514
};
494515
}
@@ -499,8 +520,8 @@ public void advance() {
499520

500521

501522
static private class DEPENDENT_LEFT extends GraphRelation {
502-
DEPENDENT_LEFT(String reln, String name) {
503-
super("<--", reln, name);
523+
DEPENDENT_LEFT(String reln, String name, String edgeName) {
524+
super("<--", reln, name, edgeName);
504525
}
505526

506527
@Override
@@ -518,6 +539,7 @@ boolean satisfies(IndexedWord l1, IndexedWord l2, SemanticGraph sg) {
518539
return false;
519540
}
520541

542+
521543
@Override
522544
Iterator<IndexedWord> searchNodeIterator(final IndexedWord node, final SemanticGraph sg) {
523545
return new SearchNodeIterator() {
@@ -540,10 +562,12 @@ public void advance() {
540562
}
541563
if (node.index() > edge.getSource().index()) {
542564
this.next = edge.getSource();
565+
this.edge = edge;
543566
return;
544567
}
545568
}
546569
this.next = null;
570+
this.edge = null;
547571
}
548572
};
549573
}
@@ -1342,25 +1366,31 @@ public static boolean isKnownRelation(String reln) {
13421366

13431367
public static GraphRelation getRelation(String reln,
13441368
String type,
1345-
String name) throws ParseException {
1369+
String name,
1370+
String edgeName) throws ParseException {
13461371
if (reln == null && type == null)
13471372
return null;
13481373
if (!isKnownRelation(reln)) {
13491374
throw new ParseException("Unknown relation " + reln);
13501375
}
13511376
switch (reln) {
13521377
case ">":
1353-
return new GOVERNOR(type, name);
1378+
return new GOVERNOR(type, name, edgeName);
13541379
case ">++":
1355-
return new GOVERNOR_RIGHT(type, name);
1380+
return new GOVERNOR_RIGHT(type, name, edgeName);
13561381
case ">--":
1357-
return new GOVERNOR_LEFT(type, name);
1382+
return new GOVERNOR_LEFT(type, name, edgeName);
13581383
case "<":
1359-
return new DEPENDENT(type, name);
1384+
return new DEPENDENT(type, name, edgeName);
13601385
case "<++":
1361-
return new DEPENDENT_RIGHT(type, name);
1386+
return new DEPENDENT_RIGHT(type, name, edgeName);
13621387
case "<--":
1363-
return new DEPENDENT_LEFT(type, name);
1388+
return new DEPENDENT_LEFT(type, name, edgeName);
1389+
}
1390+
if (edgeName != null) {
1391+
throw new ParseException("Relation " + reln + " does not allow for named edges");
1392+
}
1393+
switch (reln) {
13641394
case ">>":
13651395
return new GRANDPARENT(type, name);
13661396
case "<<":
@@ -1386,7 +1416,7 @@ public static GraphRelation getRelation(String reln,
13861416
case "@":
13871417
return new ALIGNMENT();
13881418
default:
1389-
//error
1419+
//error
13901420
throw new ParseException("Relation " + reln +
13911421
" not handled by getRelation");
13921422
}
@@ -1395,7 +1425,11 @@ public static GraphRelation getRelation(String reln,
13951425
public static GraphRelation getRelation(String reln,
13961426
String type,
13971427
int num,
1398-
String name) throws ParseException {
1428+
String name,
1429+
String edgeName) throws ParseException {
1430+
if (edgeName != null) {
1431+
throw new ParseException("Relation " + reln + " does not allow for named edges");
1432+
}
13991433
if (reln == null && type == null)
14001434
return null;
14011435
if (reln.equals(">>"))
@@ -1413,7 +1447,11 @@ else if (isKnownRelation(reln))
14131447
public static GraphRelation getRelation(String reln,
14141448
String type,
14151449
int num, int num2,
1416-
String name) throws ParseException {
1450+
String name,
1451+
String edgeName) throws ParseException {
1452+
if (edgeName != null) {
1453+
throw new ParseException("Relation " + reln + " does not allow for named edges");
1454+
}
14171455
if (reln == null && type == null)
14181456
return null;
14191457
if (reln.equals(">>"))
@@ -1472,6 +1510,12 @@ public SearchNodeIterator() {
14721510
*/
14731511
String relation = null;
14741512

1513+
/**
1514+
* If the relation is a type which a single edge, such as dependent or governor,
1515+
* this variable can store that edge. Other relations such as grandparent
1516+
* do not store an edge.
1517+
*/
1518+
SemanticGraphEdge edge = null;
14751519
/**
14761520
* This method must insure that next points to first item, or null if there
14771521
* are no items.
@@ -1503,6 +1547,12 @@ public IndexedWord next() {
15031547

15041548
String getReln() {return relation;}
15051549

1550+
/**
1551+
* Return the edge cached by the match - might be null if it was not an appropriate relation type,
1552+
* even if there was a labeled relation
1553+
*/
1554+
SemanticGraphEdge getEdge() {return edge;}
1555+
15061556
public void remove() {
15071557
throw new UnsupportedOperationException("SearchNodeIterator does not support remove().");
15081558
}

0 commit comments

Comments
 (0)