@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
for (Sentence sentence : JCasUtil.select(jCas, Sentence.class)) {
List<BaseToken> tokens = JCasUtil.selectCovered(jCas, BaseToken.class, sentence);
DEPTree tree = new DEPTree();
// Build map between CAS dependency node and id for later creation of
// ClearNLP dependency node/tree
Map<ConllDependencyNode, Integer> depNodeToID = new HashMap<ConllDependencyNode, Integer>();
int nodeId = 1;
for (ConllDependencyNode depNode : JCasUtil.selectCovered(jCas, ConllDependencyNode.class, sentence)) {
//if (depNode instanceof TopDependencyNode) {
if (depNode.getHead() == null) {
// A node without the head is the head of the sentence
depNodeToID.put(depNode, 0);
} else {
depNodeToID.put(depNode, nodeId);
nodeId++;
}
}
int[] headIDs = new int[tokens.size()];
String[] deprels = new String[tokens.size()];
// Initialize Token / Sentence info for the ClearNLP Semantic Role Labeler
// we are filtering out newline tokens
// use idIter as the non-newline token index counter
int idIter = 0;
for (int i = 0; i < tokens.size(); i++) {
BaseToken token = tokens.get(i);
// ignore newline tokens within a sentence - newline = whitespace = non-token
if(!(token instanceof NewlineToken)) {
// Determine HeadId
List<ConllDependencyNode> casDepNodes = JCasUtil.selectCovered(jCas, ConllDependencyNode.class, token);
ConllDependencyNode casDepNode = casDepNodes.get(0);
if(casDepNode.getId() == 0) casDepNode = casDepNodes.get(1);
deprels[i] = casDepNode.getDeprel();
ConllDependencyNode head = casDepNode.getHead();
// If there is no head, this is the head node, set node to 0
headIDs[i] = (head == null) ? 0 : depNodeToID.get(head);
// Populate Dependency Node / Tree information
int id = idIter + 1;
String form = casDepNode.getForm();
String pos = casDepNode.getPostag();
String lemma = casDepNode.getLemma();
DEPNode node = new DEPNode(id, form, lemma, pos, new DEPFeat());
tree.add(node);
idIter++;
}
}
for (int i=1; i<tree.size(); i++)
{
DEPNode node = tree.get(i);
DEPNode head = tree.get(headIDs[i-1]);
String label = deprels[i-1];
node.setHead(head, label);
}
tree.initSHeads();
// Run the SRL
identifier.process(tree);
classifier.process(tree);
srlabeler.process(tree);