我试图在解析的句子中获取子树的索引,以便我可以删除它。这是我的代码:
{
Queue<Tree> queue = new LinkedList<Tree>();
queue.add(tree);
while (!queue.isEmpty()) {
Tree current = queue.poll();
if (current.label().toString().equals("CC")) {
List<Tree> siblings = current.siblings(tree);
String leaf = current.getChildrenAsList().get(0).label().toString();
Tree rightNode = siblings.get(1);
Tree leftNode = siblings.get(0);
if (leaf.contains("and") &&
rightNode.label().toString().contains("S")) {
int index = rightNode.index();
System.out.println("index " + index);
System.out.println("returning index: " + tree.getChild(index).label().toString()); // testing to make sure it's the right node
}
}
List<Tree> children = current.getChildrenAsList();
for (Tree child : children) {
if (!child.isLeaf()) {
queue.add(child);
}
}
}
}
我已经尝试了我能想到的每一次objectIndexOf迭代:
int index = tree.objectIndexOf(rightNode);
int index = current.objectIndexOf(rightNode);
int index = rightNode.objectIndexOf(current);
int index = rightNode.objectIndexOf(tree);
但每次我得到-1。我的程序正确地命中条件,所以“s”节点在那棵树中作为“CC”的兄弟。但我无法让它返回一个索引,以便我可以删除它。
我不确定我在哪里出错了。