connectUnattachedSpines(graph, depNode.getRightDependent(i));
}
}
public void updatePhraseStructureGraph(MappablePhraseStructureGraph graph, Edge depEdge, boolean attachHeadSpineToRoot) throws MaltChainedException {
PhraseStructureNode dependentSpine = (PhraseStructureNode)depEdge.getTarget();
if (((PhraseStructureNode)depEdge.getTarget()).getParent() == null) {
// Restore dependent spine
String phraseSpineLabel = null;
String edgeSpineLabel = null;
int empty_label = 0;
if (depEdge.hasLabel(graph.getSymbolTables().getSymbolTable(PHRASE))) {
phraseSpineLabel = depEdge.getLabelSymbol(graph.getSymbolTables().getSymbolTable(PHRASE));
}
if (depEdge.hasLabel(graph.getSymbolTables().getSymbolTable(HEADREL))) {
edgeSpineLabel = depEdge.getLabelSymbol(graph.getSymbolTables().getSymbolTable(HEADREL));
}
if (phraseSpineLabel != null && phraseSpineLabel.length() > 0 && phraseSpineLabel.charAt(0) != EMPTY_SPINE) {
int ps = 0, es = 0, i = 0, j = 0, n = phraseSpineLabel.length()-1, m = edgeSpineLabel.length()-1;
PhraseStructureNode child = (PhraseStructureNode)depEdge.getTarget();
while (true) {
while (i <= n && phraseSpineLabel.charAt(i) != SPINE_ELEMENT_SEPARATOR) {
if (phraseSpineLabel.charAt(i) == QUESTIONMARK) {
empty_label++;
} else {
empty_label = 0;
}
i++;
}
if (depEdge.getSource().isRoot() && i >= n) {
dependentSpine = graph.getPhraseStructureRoot();
} else {
dependentSpine = graph.addNonTerminalNode(++nonTerminalCounter);
}
if (empty_label != 2 && ps != i) {
dependentSpine.addLabel(graph.getSymbolTables().addSymbolTable(CAT), phraseSpineLabel.substring(ps,i));
}
empty_label = 0;
if (edgeSpineLabel != null) {
while (j <= m && edgeSpineLabel.charAt(j) != SPINE_ELEMENT_SEPARATOR) {
if (edgeSpineLabel.charAt(j) == QUESTIONMARK) {
empty_label++;
} else {
empty_label = 0;
}
j++;
}
}
lockUpdate = true;
Edge e = graph.addPhraseStructureEdge(dependentSpine, child);
if (empty_label != 2 && es != j && edgeSpineLabel != null && e != null) {
e.addLabel(graph.getSymbolTables().addSymbolTable(EDGELABEL), edgeSpineLabel.substring(es,j));
} else if (es == j) {
e.addLabel(graph.getSymbolTables().addSymbolTable(EDGELABEL), EMPTY_LABEL);
}
lockUpdate = false;
child = dependentSpine;
if (i >= n) { break; }
empty_label = 0;
ps = i = i + 1;
es = j = j + 1;
}
}
// Recursively attach the dependent spines to target node.
DependencyNode target = (DependencyNode)depEdge.getTarget();
for (int i = 0; i < target.getLeftDependentCount(); i++) {
updatePhraseStructureGraph(graph, target.getLeftDependent(i).getHeadEdge(), attachHeadSpineToRoot);
}
for (int i = target.getRightDependentCount()-1; i >= 0 ; i--) {
updatePhraseStructureGraph(graph, target.getRightDependent(i).getHeadEdge(), attachHeadSpineToRoot);
}
} else {
// If dependent spine already exist, then set dependentSpine to the highest nonterminal
// of the dependent spine.
while (dependentSpine.getParent() != null && !dependentSpine.getParent().isRoot()) {
dependentSpine = dependentSpine.getParent();
}
}
PhraseStructureNode headSpine = null;
if (((PhraseStructureNode)depEdge.getSource()).getParent() != null) {
// If head spine exist, then attach dependent spine to the head spine at the attachment level a.
int a = 0;
headSpine = ((PhraseStructureNode)depEdge.getSource()).getParent();
if (depEdge.hasLabel(graph.getSymbolTables().getSymbolTable(ATTACH))) {
try {
a = Integer.parseInt((depEdge.getLabelSymbol(graph.getSymbolTables().getSymbolTable(ATTACH))));
} catch (NumberFormatException e) {
throw new MaltChainedException(e.getMessage());
}
}
for (int i = 0; i < a && headSpine != null; i++) {
headSpine = headSpine.getParent();
}
if ((headSpine == null || headSpine == dependentSpine) && attachHeadSpineToRoot) {
headSpine = graph.getPhraseStructureRoot();
}