Skip to content

Commit

Permalink
Add a couple left/right relations similar to the existing one to Semg…
Browse files Browse the repository at this point in the history
…rex. Implementations of the Spacy extensions of the same operations

Add a short test of adjacent left/right, left/right
  • Loading branch information
AngledLuffa committed Jan 20, 2023
1 parent e2f0c1f commit 98be52a
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 41 deletions.
164 changes: 160 additions & 4 deletions src/edu/stanford/nlp/semgraph/semgrex/GraphRelation.java
Expand Up @@ -913,11 +913,11 @@ boolean satisfiesOrder(IndexedWord l1, IndexedWord l2) {
}
}

static private class ADJACENT_NODE extends GraphRelation {
static private class ADJACENT_RIGHT extends GraphRelation {

private static final long serialVersionUID = 1L;

ADJACENT_NODE(String reln, String name) {
ADJACENT_RIGHT(String reln, String name) {
super(".", reln, name);
}

Expand Down Expand Up @@ -948,6 +948,7 @@ public void advance() {

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


static private class ADJACENT_LEFT extends GraphRelation {

private static final long serialVersionUID = 1L;

ADJACENT_LEFT(String reln, String name) {
super("-", reln, name);
}


@Override
boolean satisfies(IndexedWord l1, IndexedWord l2, SemanticGraph sg) {
if (l1.index() == (l2.index() + 1)) {
return true;
}
return false;
}

@Override
Iterator<IndexedWord> searchNodeIterator(final IndexedWord node, final SemanticGraph sg) {
return new SearchNodeIterator() {
Iterator<IndexedWord> iterator;

@Override
public void advance() {
if (node.equals(IndexedWord.NO_WORD)) {
next = null;
return;
}

if (iterator == null) {
iterator = sg.vertexSet().iterator();
}

while (iterator.hasNext()) {
IndexedWord word = iterator.next();
// note that index might not be unique if there are copy nodes
if (node.index() != (word.index() + 1)) {
continue;
}
this.next = word;
return;
}
this.next = null;
}
};
}

}


static private class RIGHT extends GraphRelation {

private static final long serialVersionUID = 1L;

RIGHT(String reln, String name) {
super("..", reln, name);
}


@Override
boolean satisfies(IndexedWord l1, IndexedWord l2, SemanticGraph sg) {
if (l1.index() < l2.index()) {
return true;
}
return false;
}

@Override
Iterator<IndexedWord> searchNodeIterator(final IndexedWord node, final SemanticGraph sg) {
return new SearchNodeIterator() {
Iterator<IndexedWord> iterator;

@Override
public void advance() {
if (node.equals(IndexedWord.NO_WORD)) {
next = null;
return;
}

if (iterator == null) {
iterator = sg.vertexSet().iterator();
}

while (iterator.hasNext()) {
IndexedWord word = iterator.next();
if (node.index() >= word.index()) {
continue;
}
this.next = word;
return;
}
this.next = null;
}
};
}

}


static private class LEFT extends GraphRelation {

private static final long serialVersionUID = 1L;

LEFT(String reln, String name) {
super("--", reln, name);
}


@Override
boolean satisfies(IndexedWord l1, IndexedWord l2, SemanticGraph sg) {
if (l1.index() > l2.index()) {
return true;
}
return false;
}

@Override
Iterator<IndexedWord> searchNodeIterator(final IndexedWord node, final SemanticGraph sg) {
return new SearchNodeIterator() {
Iterator<IndexedWord> iterator;

@Override
public void advance() {
if (node.equals(IndexedWord.NO_WORD)) {
next = null;
return;
}

if (iterator == null) {
iterator = sg.vertexSet().iterator();
}

while (iterator.hasNext()) {
IndexedWord word = iterator.next();
if (node.index() <= word.index()) {
continue;
}
this.next = word;
return;
}
this.next = null;
}
};
}

}


// ============================================================================

public static boolean isKnownRelation(String reln) {
Expand All @@ -970,7 +1119,8 @@ public static boolean isKnownRelation(String reln) {
reln.equals("@") || reln.equals("==") ||
reln.equals("$+") || reln.equals("$++") ||
reln.equals("$-") || reln.equals("$--") ||
reln.equals("."));
reln.equals(".") || reln.equals("..") ||
reln.equals("-") || reln.equals("--"));
}

public static GraphRelation getRelation(String reln,
Expand Down Expand Up @@ -1001,7 +1151,13 @@ public static GraphRelation getRelation(String reln,
case "$--":
return new LEFT_SIBLING(type, name);
case ".":
return new ADJACENT_NODE(type, name);
return new ADJACENT_RIGHT(type, name);
case "..":
return new RIGHT(type, name);
case "-":
return new ADJACENT_LEFT(type, name);
case "--":
return new LEFT(type, name);
case "@":
return new ALIGNMENT();
default:
Expand Down
2 changes: 1 addition & 1 deletion src/edu/stanford/nlp/semgraph/semgrex/SemgrexParser.jj
Expand Up @@ -62,7 +62,7 @@ SPECIAL_TOKEN:

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

0 comments on commit 98be52a

Please sign in to comment.