Skip to content

Commit 98be52a

Browse files
committed
Add a couple left/right relations similar to the existing one to Semgrex. Implementations of the Spacy extensions of the same operations
Add a short test of adjacent left/right, left/right
1 parent e2f0c1f commit 98be52a

File tree

4 files changed

+231
-41
lines changed

4 files changed

+231
-41
lines changed

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

Lines changed: 160 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -913,11 +913,11 @@ boolean satisfiesOrder(IndexedWord l1, IndexedWord l2) {
913913
}
914914
}
915915

916-
static private class ADJACENT_NODE extends GraphRelation {
916+
static private class ADJACENT_RIGHT extends GraphRelation {
917917

918918
private static final long serialVersionUID = 1L;
919919

920-
ADJACENT_NODE(String reln, String name) {
920+
ADJACENT_RIGHT(String reln, String name) {
921921
super(".", reln, name);
922922
}
923923

@@ -948,6 +948,7 @@ public void advance() {
948948

949949
while (iterator.hasNext()) {
950950
IndexedWord word = iterator.next();
951+
// note that index might not be unique if there are copy nodes
951952
if (node.index() != (word.index() - 1)) {
952953
continue;
953954
}
@@ -962,6 +963,154 @@ public void advance() {
962963
}
963964

964965

966+
static private class ADJACENT_LEFT extends GraphRelation {
967+
968+
private static final long serialVersionUID = 1L;
969+
970+
ADJACENT_LEFT(String reln, String name) {
971+
super("-", reln, name);
972+
}
973+
974+
975+
@Override
976+
boolean satisfies(IndexedWord l1, IndexedWord l2, SemanticGraph sg) {
977+
if (l1.index() == (l2.index() + 1)) {
978+
return true;
979+
}
980+
return false;
981+
}
982+
983+
@Override
984+
Iterator<IndexedWord> searchNodeIterator(final IndexedWord node, final SemanticGraph sg) {
985+
return new SearchNodeIterator() {
986+
Iterator<IndexedWord> iterator;
987+
988+
@Override
989+
public void advance() {
990+
if (node.equals(IndexedWord.NO_WORD)) {
991+
next = null;
992+
return;
993+
}
994+
995+
if (iterator == null) {
996+
iterator = sg.vertexSet().iterator();
997+
}
998+
999+
while (iterator.hasNext()) {
1000+
IndexedWord word = iterator.next();
1001+
// note that index might not be unique if there are copy nodes
1002+
if (node.index() != (word.index() + 1)) {
1003+
continue;
1004+
}
1005+
this.next = word;
1006+
return;
1007+
}
1008+
this.next = null;
1009+
}
1010+
};
1011+
}
1012+
1013+
}
1014+
1015+
1016+
static private class RIGHT extends GraphRelation {
1017+
1018+
private static final long serialVersionUID = 1L;
1019+
1020+
RIGHT(String reln, String name) {
1021+
super("..", reln, name);
1022+
}
1023+
1024+
1025+
@Override
1026+
boolean satisfies(IndexedWord l1, IndexedWord l2, SemanticGraph sg) {
1027+
if (l1.index() < l2.index()) {
1028+
return true;
1029+
}
1030+
return false;
1031+
}
1032+
1033+
@Override
1034+
Iterator<IndexedWord> searchNodeIterator(final IndexedWord node, final SemanticGraph sg) {
1035+
return new SearchNodeIterator() {
1036+
Iterator<IndexedWord> iterator;
1037+
1038+
@Override
1039+
public void advance() {
1040+
if (node.equals(IndexedWord.NO_WORD)) {
1041+
next = null;
1042+
return;
1043+
}
1044+
1045+
if (iterator == null) {
1046+
iterator = sg.vertexSet().iterator();
1047+
}
1048+
1049+
while (iterator.hasNext()) {
1050+
IndexedWord word = iterator.next();
1051+
if (node.index() >= word.index()) {
1052+
continue;
1053+
}
1054+
this.next = word;
1055+
return;
1056+
}
1057+
this.next = null;
1058+
}
1059+
};
1060+
}
1061+
1062+
}
1063+
1064+
1065+
static private class LEFT extends GraphRelation {
1066+
1067+
private static final long serialVersionUID = 1L;
1068+
1069+
LEFT(String reln, String name) {
1070+
super("--", reln, name);
1071+
}
1072+
1073+
1074+
@Override
1075+
boolean satisfies(IndexedWord l1, IndexedWord l2, SemanticGraph sg) {
1076+
if (l1.index() > l2.index()) {
1077+
return true;
1078+
}
1079+
return false;
1080+
}
1081+
1082+
@Override
1083+
Iterator<IndexedWord> searchNodeIterator(final IndexedWord node, final SemanticGraph sg) {
1084+
return new SearchNodeIterator() {
1085+
Iterator<IndexedWord> iterator;
1086+
1087+
@Override
1088+
public void advance() {
1089+
if (node.equals(IndexedWord.NO_WORD)) {
1090+
next = null;
1091+
return;
1092+
}
1093+
1094+
if (iterator == null) {
1095+
iterator = sg.vertexSet().iterator();
1096+
}
1097+
1098+
while (iterator.hasNext()) {
1099+
IndexedWord word = iterator.next();
1100+
if (node.index() <= word.index()) {
1101+
continue;
1102+
}
1103+
this.next = word;
1104+
return;
1105+
}
1106+
this.next = null;
1107+
}
1108+
};
1109+
}
1110+
1111+
}
1112+
1113+
9651114
// ============================================================================
9661115

9671116
public static boolean isKnownRelation(String reln) {
@@ -970,7 +1119,8 @@ public static boolean isKnownRelation(String reln) {
9701119
reln.equals("@") || reln.equals("==") ||
9711120
reln.equals("$+") || reln.equals("$++") ||
9721121
reln.equals("$-") || reln.equals("$--") ||
973-
reln.equals("."));
1122+
reln.equals(".") || reln.equals("..") ||
1123+
reln.equals("-") || reln.equals("--"));
9741124
}
9751125

9761126
public static GraphRelation getRelation(String reln,
@@ -1001,7 +1151,13 @@ public static GraphRelation getRelation(String reln,
10011151
case "$--":
10021152
return new LEFT_SIBLING(type, name);
10031153
case ".":
1004-
return new ADJACENT_NODE(type, name);
1154+
return new ADJACENT_RIGHT(type, name);
1155+
case "..":
1156+
return new RIGHT(type, name);
1157+
case "-":
1158+
return new ADJACENT_LEFT(type, name);
1159+
case "--":
1160+
return new LEFT(type, name);
10051161
case "@":
10061162
return new ALIGNMENT();
10071163
default:

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)