Skip to content

Instantly share code, notes, and snippets.

@allanj
Created October 15, 2017 06:23
Show Gist options
  • Save allanj/b5d4e61eba0abbbf637c35f21dd59d17 to your computer and use it in GitHub Desktop.
Save allanj/b5d4e61eba0abbbf637c35f21dd59d17 to your computer and use it in GitHub Desktop.
Dependency Tree class for StatNLP framework
package org.statnlp.example.nerelation.struct;
import org.statnlp.commons.types.Sentence;
import gnu.trove.list.TIntList;
import gnu.trove.list.array.TIntArrayList;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import gnu.trove.stack.TIntStack;
public class DependencyTree {
private TIntObjectMap<TIntList> tree;
/**
* The sentence must be with head index and it's 0-indexed.
* In this case, the root should be indexed by -1.
* @param sent
*/
public DependencyTree (Sentence sent) {
this.build(sent);
}
public void build(Sentence sent) {
tree = new TIntObjectHashMap<>();
for (int i = 0; i < sent.length(); i++) {
this.add(sent.get(i).getHeadIndex(), i);
}
}
private void add (int parent, int child) {
if (tree.containsKey(parent)) {
TIntList list = tree.get(parent);
list.add(child);
} else {
TIntList list = new TIntArrayList();
list.add(child);
tree.put(parent, list);
}
}
public boolean dfsFind (int val, TIntStack stack) {
return dfsFind(-1, val, stack);
}
private boolean dfsFind(int curr, int val, TIntStack stack) {
stack.push(curr);
if (curr == val) {
return true;
} else {
TIntList children = tree.get(curr);
if (children != null) {
for (int i = 0; i < children.size(); i++) {
boolean found = dfsFind(children.get(i), val, stack);
if (found) {
return true;
}
}
}
stack.pop();
return false;
}
}
/**** ///debuggging code.
//Testing the dependency tree.
public static void main(String[] args) {
List<WordToken> wts = new ArrayList<WordToken>();
wts.add(new WordToken("Mike", "NNP",1, "", "compound")); wts.add(new WordToken("O", "NNP",3, "", "nmod:poss"));
wts.add(new WordToken("'", "NNP",1, "", "case")); wts.add(new WordToken("Sullivan", "NNP",-1, "", "root"));
wts.add(new WordToken(",", "NNP",3, "", "punct")); wts.add(new WordToken("VOA", "NNP",6, "", "compound"));
wts.add(new WordToken("News", "NNP",3, "", "conj")); wts.add(new WordToken(",", "NNP",3, "", "punct"));
wts.add(new WordToken("Los", "NNP",9, "", "compound")); wts.add(new WordToken("Angeles", "NNP",3, "", "appos"));
wts.add(new WordToken(".", "NNP",3, "", "punct"));
LGSentence sent = new LGSentence(wts.toArray(new WordToken[wts.size()]));
DependencyTree tree = new DependencyTree(sent);
sent.depTree = tree;
TIntStack stack = new TIntArrayStack();
for (int val = 0; val < sent.length(); val++) {
stack = new TIntArrayStack();
boolean found = tree.dfsFind(val, stack);
System.out.println("val "+val+" is found? " + found+" trace:"+stack);
}
for (int i = 0; i< sent.length(); i++)
for (int j = 0; j < sent.length(); j++) {
System.out.println(i + "," + j + ":" + tree.findPath(sent, i, j));
}
}
***/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment