001    package org.maltparser.parser.algorithm.nivre.malt04;
002    
003    import org.maltparser.core.exception.MaltChainedException;
004    import org.maltparser.core.symbol.SymbolTable;
005    import org.maltparser.core.syntaxgraph.DependencyStructure;
006    import org.maltparser.core.syntaxgraph.node.DependencyNode;
007    import org.maltparser.parser.SingleMalt;
008    import org.maltparser.parser.algorithm.nivre.Nivre;
009    
010    /**
011     * 
012     * @author Joakim Nivre
013     * @author Johan Hall
014     * @since 1.0
015    */
016    public abstract class NivreMalt04 extends Nivre {
017            protected SymbolTable deprel;
018            protected boolean inPostProcessingMode = false;
019            
020    
021            public NivreMalt04(SingleMalt configuration) throws MaltChainedException {
022                    super(configuration);
023            }
024            
025            public DependencyStructure parse(DependencyStructure parseDependencyGraph) throws MaltChainedException {
026                    clear(parseDependencyGraph);
027                    inPostProcessingMode = false;
028                    while (!input.isEmpty()) {
029                            currentAction = history.getEmptyGuideUserAction();
030                            if (rootHandling != NORMAL && stack.peek().isRoot()) {
031                                    updateActionContainers(SHIFT, null);
032                            } else {
033                                    configuration.predict(currentAction); 
034                            }
035                            transition(parseDependencyGraph);
036                    }
037    
038                    if (postProcessing == true) {
039                            input.clear();
040                            inPostProcessingMode = true;
041                            
042                            for (int i = parseDependencyGraph.getHighestTokenIndex(); i > 0; i--) {
043                                    DependencyNode node = parseDependencyGraph.getDependencyNode(i);
044                                    if (node != null) { 
045                                            input.push(node);
046                                    }
047                            }
048                            int last = input.size();
049                            for (int i = 1; i < stack.size(); i++) {
050                                    if (!stack.get(i).hasHead() || stack.get(i).getHead().isRoot()) {
051                                            input.set(--last, stack.get(i));
052                                    } 
053                            }
054                            stack.clear();
055                            stack.push(parseDependencyGraph.getDependencyRoot());
056                            while (!input.isEmpty() && input.size() > last) {
057                                    currentAction = history.getEmptyGuideUserAction();
058                                    if (rootHandling != NORMAL && stack.peek().isRoot()) {
059                                            updateActionContainers(SHIFT, null);
060                                    } else {
061                                            configuration.predict(currentAction); 
062                                    }
063                                    transition(parseDependencyGraph);
064                            }
065                    }
066                    parseDependencyGraph.linkAllTreesToRoot();
067                    return parseDependencyGraph;
068            }
069            
070            public DependencyStructure oracleParse(DependencyStructure goldDependencyGraph, DependencyStructure parseDependencyGraph) throws MaltChainedException {
071    //              if (!(goldDependencyGraph instanceof SingleHeadedDependencyGraph)) {
072    //                      throw new ParsingException("The gold standard graph must be a single headed graph. ");
073    //              }
074                    inPostProcessingMode = false;
075                    clear(parseDependencyGraph);
076                    while (!input.isEmpty()) {
077                            currentAction = history.getEmptyGuideUserAction();
078                            if (rootHandling != NORMAL && stack.peek().isRoot()) {
079                                    updateActionContainers(SHIFT, null);
080                            } else {
081                                    oraclePredict(goldDependencyGraph, parseDependencyGraph);
082                                    configuration.setInstance(currentAction);
083                            }
084                            transition(parseDependencyGraph);
085                    }
086                    if (postProcessing == true) {
087                            input.clear();
088                            inPostProcessingMode = true;
089                            for (int i = parseDependencyGraph.getHighestTokenIndex(); i > 0; i--) {
090                                    DependencyNode node = parseDependencyGraph.getDependencyNode(i);
091                                    if (node != null) { 
092                                            input.push(node);
093                                    }
094                            }
095    
096                            int last = input.size();
097                            for (int i = 1; i < stack.size(); i++) {
098                                    if (!stack.get(i).hasHead() || stack.get(i).getHead().isRoot()) {
099                                            input.set(--last, stack.get(i));
100                                    } 
101                            }
102                            stack.clear();
103                            stack.push(parseDependencyGraph.getDependencyRoot());
104                            while (!input.isEmpty() && input.size() > last) {
105                                    currentAction = history.getEmptyGuideUserAction();
106                                    if (rootHandling != NORMAL && stack.peek().isRoot()) {
107                                            updateActionContainers(SHIFT, null);
108                                    } else {
109                                            oraclePredict(goldDependencyGraph, parseDependencyGraph);
110                                            configuration.setInstance(currentAction);
111                                    }
112                                    transition(parseDependencyGraph);
113                            }
114                    }
115                    parseDependencyGraph.linkAllTreesToRoot();
116                    return parseDependencyGraph;
117            }
118            
119            protected abstract void transition(DependencyStructure dg) throws MaltChainedException; 
120            protected abstract boolean checkParserAction(DependencyStructure dg) throws MaltChainedException;
121            protected abstract void oraclePredict(DependencyStructure gold, DependencyStructure parseDependencyGraph) throws MaltChainedException;
122            public abstract String getName();
123    
124    }
125