Skip to content

Commit efe5a14

Browse files
committed
Add to Semgrex some of the other relations defined by spacy's implementation of semgrex: <++ <-- >++ <--
1 parent 98be52a commit efe5a14

File tree

4 files changed

+325
-68
lines changed

4 files changed

+325
-68
lines changed

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

Lines changed: 230 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ void initialize() {
236236

237237
// GOVERNOR graph relation: ">" ===============================================
238238

239-
static private class GOVERNER extends GraphRelation {
240-
GOVERNER(String reln, String name) {
239+
static private class GOVERNOR extends GraphRelation {
240+
GOVERNOR(String reln, String name) {
241241
super(">", reln, name);
242242
}
243243

@@ -278,13 +278,118 @@ public void advance() {
278278
}
279279
this.next = null;
280280
}
281-
};
281+
};
282282
}
283283

284284
// automatically generated by Eclipse
285285
private static final long serialVersionUID = -7003148918274183951L;
286286
};
287287

288+
static private class GOVERNOR_RIGHT extends GraphRelation {
289+
GOVERNOR_RIGHT(String reln, String name) {
290+
super(">++", reln, name);
291+
}
292+
293+
294+
@Override
295+
boolean satisfies(IndexedWord l1, IndexedWord l2, SemanticGraph sg) {
296+
List<Pair<GrammaticalRelation, IndexedWord>> deps = sg.childPairs(l1);
297+
for (Pair<GrammaticalRelation, IndexedWord> dep : deps) {
298+
if (this.type.test(dep.first().toString()) &&
299+
dep.second().equals(l2) &&
300+
l1.index() < l2.index()) {
301+
return true;
302+
}
303+
}
304+
return false;
305+
}
306+
307+
@Override
308+
Iterator<IndexedWord> searchNodeIterator(final IndexedWord node, final SemanticGraph sg) {
309+
return new SearchNodeIterator() {
310+
Iterator<SemanticGraphEdge> iterator;
311+
312+
@Override
313+
public void advance() {
314+
if (node.equals(IndexedWord.NO_WORD)) {
315+
next = null;
316+
return;
317+
}
318+
if (iterator == null) {
319+
iterator = sg.outgoingEdgeIterator(node);
320+
}
321+
while (iterator.hasNext()) {
322+
SemanticGraphEdge edge = iterator.next();
323+
relation = edge.getRelation().toString();
324+
if (!type.test(relation)) {
325+
continue;
326+
}
327+
if (node.index() < edge.getTarget().index()) {
328+
this.next = edge.getTarget();
329+
return;
330+
}
331+
}
332+
this.next = null;
333+
}
334+
};
335+
}
336+
337+
// automatically generated by button mashing
338+
private static final long serialVersionUID = -13455768987623L;
339+
}
340+
341+
static private class GOVERNOR_LEFT extends GraphRelation {
342+
GOVERNOR_LEFT(String reln, String name) {
343+
super(">--", reln, name);
344+
}
345+
346+
347+
@Override
348+
boolean satisfies(IndexedWord l1, IndexedWord l2, SemanticGraph sg) {
349+
List<Pair<GrammaticalRelation, IndexedWord>> deps = sg.childPairs(l1);
350+
for (Pair<GrammaticalRelation, IndexedWord> dep : deps) {
351+
if (this.type.test(dep.first().toString()) &&
352+
dep.second().equals(l2) &&
353+
l1.index() > l2.index()) {
354+
return true;
355+
}
356+
}
357+
return false;
358+
}
359+
360+
@Override
361+
Iterator<IndexedWord> searchNodeIterator(final IndexedWord node, final SemanticGraph sg) {
362+
return new SearchNodeIterator() {
363+
Iterator<SemanticGraphEdge> iterator;
364+
365+
@Override
366+
public void advance() {
367+
if (node.equals(IndexedWord.NO_WORD)) {
368+
next = null;
369+
return;
370+
}
371+
if (iterator == null) {
372+
iterator = sg.outgoingEdgeIterator(node);
373+
}
374+
while (iterator.hasNext()) {
375+
SemanticGraphEdge edge = iterator.next();
376+
relation = edge.getRelation().toString();
377+
if (!type.test(relation)) {
378+
continue;
379+
}
380+
if (node.index() > edge.getTarget().index()) {
381+
this.next = edge.getTarget();
382+
return;
383+
}
384+
}
385+
this.next = null;
386+
}
387+
};
388+
}
389+
390+
// automatically generated by button mashing
391+
private static final long serialVersionUID = -84679864256L;
392+
}
288393

289394
// DEPENDENT graph relation: "<" ===============================================
290395

@@ -338,6 +443,116 @@ public void advance() {
338443
};
339444

340445

446+
static private class DEPENDENT_RIGHT extends GraphRelation {
447+
DEPENDENT_RIGHT(String reln, String name) {
448+
super("<++", reln, name);
449+
}
450+
451+
@Override
452+
boolean satisfies(IndexedWord l1, IndexedWord l2, SemanticGraph sg) {
453+
if (l1.equals(IndexedWord.NO_WORD) || l2.equals(IndexedWord.NO_WORD) )
454+
return false;
455+
List<Pair<GrammaticalRelation, IndexedWord>> govs = sg.parentPairs(l1);
456+
for (Pair<GrammaticalRelation, IndexedWord> gov : govs) {
457+
if (this.type.test(gov.first().toString()) &&
458+
gov.second().equals(l2) &&
459+
l1.index() < l2.index()) {
460+
return true;
461+
}
462+
}
463+
return false;
464+
}
465+
466+
@Override
467+
Iterator<IndexedWord> searchNodeIterator(final IndexedWord node, final SemanticGraph sg) {
468+
return new SearchNodeIterator() {
469+
Iterator<SemanticGraphEdge> iterator;
470+
471+
@Override
472+
public void advance() {
473+
if (node.equals(IndexedWord.NO_WORD)) {
474+
next = null;
475+
return;
476+
}
477+
if (iterator == null) {
478+
iterator = sg.incomingEdgeIterator(node);
479+
}
480+
while (iterator.hasNext()) {
481+
SemanticGraphEdge edge = iterator.next();
482+
relation = edge.getRelation().toString();
483+
if (!type.test(relation)) {
484+
continue;
485+
}
486+
if (node.index() < edge.getSource().index()) {
487+
this.next = edge.getSource();
488+
return;
489+
}
490+
}
491+
this.next = null;
492+
}
493+
};
494+
}
495+
496+
// automatically generated by button mashing
497+
private static final long serialVersionUID = -98652734561235L;
498+
};
499+
500+
501+
static private class DEPENDENT_LEFT extends GraphRelation {
502+
DEPENDENT_LEFT(String reln, String name) {
503+
super("<--", reln, name);
504+
}
505+
506+
@Override
507+
boolean satisfies(IndexedWord l1, IndexedWord l2, SemanticGraph sg) {
508+
if (l1.equals(IndexedWord.NO_WORD) || l2.equals(IndexedWord.NO_WORD) )
509+
return false;
510+
List<Pair<GrammaticalRelation, IndexedWord>> govs = sg.parentPairs(l1);
511+
for (Pair<GrammaticalRelation, IndexedWord> gov : govs) {
512+
if (this.type.test(gov.first().toString()) &&
513+
gov.second().equals(l2) &&
514+
l1.index() > l2.index()) {
515+
return true;
516+
}
517+
}
518+
return false;
519+
}
520+
521+
@Override
522+
Iterator<IndexedWord> searchNodeIterator(final IndexedWord node, final SemanticGraph sg) {
523+
return new SearchNodeIterator() {
524+
Iterator<SemanticGraphEdge> iterator;
525+
526+
@Override
527+
public void advance() {
528+
if (node.equals(IndexedWord.NO_WORD)) {
529+
next = null;
530+
return;
531+
}
532+
if (iterator == null) {
533+
iterator = sg.incomingEdgeIterator(node);
534+
}
535+
while (iterator.hasNext()) {
536+
SemanticGraphEdge edge = iterator.next();
537+
relation = edge.getRelation().toString();
538+
if (!type.test(relation)) {
539+
continue;
540+
}
541+
if (node.index() > edge.getSource().index()) {
542+
this.next = edge.getSource();
543+
return;
544+
}
545+
}
546+
this.next = null;
547+
}
548+
};
549+
}
550+
551+
// automatically generated by button mashing
552+
private static final long serialVersionUID = -1356537761324587L;
553+
};
554+
555+
341556
static private class LIMITED_GRANDPARENT extends GraphRelation {
342557
final int startDepth, endDepth;
343558

@@ -1120,7 +1335,9 @@ public static boolean isKnownRelation(String reln) {
11201335
reln.equals("$+") || reln.equals("$++") ||
11211336
reln.equals("$-") || reln.equals("$--") ||
11221337
reln.equals(".") || reln.equals("..") ||
1123-
reln.equals("-") || reln.equals("--"));
1338+
reln.equals("-") || reln.equals("--") ||
1339+
reln.equals(">++") || reln.equals(">--") ||
1340+
reln.equals("<++") || reln.equals("<--"));
11241341
}
11251342

11261343
public static GraphRelation getRelation(String reln,
@@ -1133,9 +1350,17 @@ public static GraphRelation getRelation(String reln,
11331350
}
11341351
switch (reln) {
11351352
case ">":
1136-
return new GOVERNER(type, name);
1353+
return new GOVERNOR(type, name);
1354+
case ">++":
1355+
return new GOVERNOR_RIGHT(type, name);
1356+
case ">--":
1357+
return new GOVERNOR_LEFT(type, name);
11371358
case "<":
11381359
return new DEPENDENT(type, name);
1360+
case "<++":
1361+
return new DEPENDENT_RIGHT(type, name);
1362+
case "<--":
1363+
return new DEPENDENT_LEFT(type, name);
11391364
case ">>":
11401365
return new GRANDPARENT(type, name);
11411366
case "<<":

src/edu/stanford/nlp/semgraph/semgrex/SemgrexParser.jj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ SPECIAL_TOKEN:
6262

6363
TOKEN:
6464
{
65-
< RELATION: "<" | ">" | ">>" | "<<" | "==" | "$+" | "$-" | "$++" | "$--" | "." | ".." | "-" | "--" >
65+
< RELATION: "<" | ">" | ">>" | "<<" | "==" | "$+" | "$-" | "$++" | "$--" | "." | ".." | "-" | "--" | ">++" | ">--" | "<++" | "<--" >
6666
| < ALIGNRELN: "@" >
6767
| < IDENTIFIER: (~[" ", "\n", "\r", "(", "/", "|", "@", "!", "#", "%", "&", ")", "=", "?", "[", "]", ">", "<", "~", ".", ",", "$", ":", ";", "{", "}", "+", "-"])+ >
6868
| < NUMBER: ( ["0"-"9"] )+ >

0 commit comments

Comments
 (0)