Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a DeleteLeaf operation to Ssurgeon. Will delete a leaf (node with…
… no children) and rearrange all the indices appropriately.
- Loading branch information
1 parent
203eb06
commit 429f61a
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/DeleteLeaf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package edu.stanford.nlp.semgraph.semgrex.ssurgeon; | ||
|
||
import java.io.StringWriter; | ||
|
||
import edu.stanford.nlp.ling.IndexedWord; | ||
import edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher; | ||
import edu.stanford.nlp.semgraph.SemanticGraph; | ||
import edu.stanford.nlp.semgraph.SemanticGraphEdge; | ||
|
||
/** | ||
* This action removes all incoming edges for the given node. | ||
* @author lumberjack | ||
* | ||
*/ | ||
public class DeleteLeaf extends SsurgeonEdit { | ||
public static final String LABEL = "deleteLeaf"; | ||
protected String nodeName; // name of this node | ||
|
||
public DeleteLeaf(String nodeName) { | ||
this.nodeName = nodeName; | ||
} | ||
|
||
/** | ||
* If executed twice on the same node, the second time there | ||
* will be no further updates | ||
*/ | ||
@Override | ||
public boolean evaluate(SemanticGraph sg, SemgrexMatcher sm) { | ||
IndexedWord tgtNode = getNamedNode(nodeName, sm); | ||
if (tgtNode == null) { | ||
return false; | ||
} | ||
for (SemanticGraphEdge edge : sg.outgoingEdgeList(tgtNode)) { | ||
// if there are any outgoing edges, we aren't a leaf | ||
return false; | ||
} | ||
boolean deletedEdge = false; | ||
// use incomingEdgeList so that deleting an edge | ||
// doesn't affect the iteration | ||
for (SemanticGraphEdge edge : sg.incomingEdgeList(tgtNode)) { | ||
deletedEdge = deletedEdge || sg.removeEdge(edge); | ||
} | ||
int deletedIndex = tgtNode.index(); | ||
boolean deletedNode = sg.removeVertex(tgtNode); | ||
// TODO: renumber | ||
if (deletedNode) { | ||
AddDep.moveNodes(sg, sm, x -> (x >= deletedIndex), x -> x-1, false); | ||
} | ||
return deletedEdge || deletedNode; | ||
} | ||
|
||
@Override | ||
public String toEditString() { | ||
StringWriter buf = new StringWriter(); | ||
buf.write(LABEL); buf.write("\t"); | ||
buf.write(Ssurgeon.NODENAME_ARG); buf.write("\t"); buf.write(nodeName); | ||
return buf.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters