001package org.maltparser.parser.algorithm.planar;
002
003import org.maltparser.core.exception.MaltChainedException;
004import org.maltparser.core.syntaxgraph.DependencyStructure;
005import org.maltparser.core.syntaxgraph.node.DependencyNode;
006import org.maltparser.parser.DependencyParserConfig;
007import org.maltparser.parser.Oracle;
008import org.maltparser.parser.ParserConfiguration;
009import org.maltparser.parser.history.GuideUserHistory;
010import org.maltparser.parser.history.action.GuideUserAction;
011/**
012 * @author Carlos Gomez Rodriguez
013 *
014 */
015public class PlanarArcEagerOracle extends Oracle {
016
017        public PlanarArcEagerOracle(DependencyParserConfig manager, GuideUserHistory history) throws MaltChainedException {
018                super(manager, history);
019                setGuideName("Planar");
020        }
021        
022        public GuideUserAction predict(DependencyStructure gold, ParserConfiguration config) throws MaltChainedException {
023                PlanarConfig planarConfig = (PlanarConfig)config;
024                DependencyStructure dg = planarConfig.getDependencyGraph();
025                DependencyNode stackPeek = planarConfig.getStack().peek();
026                int stackPeekIndex = stackPeek.getIndex();
027                int inputPeekIndex = planarConfig.getInput().peek().getIndex();
028                
029                if (!stackPeek.isRoot() && gold.getTokenNode(stackPeekIndex).getHead().getIndex() == inputPeekIndex
030                                && !checkIfArcExists ( dg , inputPeekIndex , stackPeekIndex ) )  {
031                        return updateActionContainers(Planar.LEFTARC, gold.getTokenNode(stackPeekIndex).getHeadEdge().getLabelSet());   
032                } else if (gold.getTokenNode(inputPeekIndex).getHead().getIndex() == stackPeekIndex
033                                && !checkIfArcExists ( dg , stackPeekIndex , inputPeekIndex ) ) {
034                        return updateActionContainers(Planar.RIGHTARC, gold.getTokenNode(inputPeekIndex).getHeadEdge().getLabelSet());
035                } else if (gold.getTokenNode(inputPeekIndex).hasLeftDependent() &&
036                                gold.getTokenNode(inputPeekIndex).getLeftmostDependent().getIndex() < stackPeekIndex) {
037                        return updateActionContainers(Planar.REDUCE, null);
038                } else if (gold.getTokenNode(inputPeekIndex).getHead().getIndex() < stackPeekIndex
039                                && 
040                                ( !gold.getTokenNode(inputPeekIndex).getHead().isRoot() || planarConfig.getRootHandling()==PlanarConfig.NORMAL )        
041                        ) {
042                        return updateActionContainers(Planar.REDUCE, null);
043                } else {
044                        return updateActionContainers(Planar.SHIFT, null);
045                }
046                        
047        }
048        
049        private boolean checkIfArcExists ( DependencyStructure dg , int index1 , int index2 ) throws MaltChainedException
050        {
051                return dg.getTokenNode(index2).hasHead() && dg.getTokenNode(index2).getHead().getIndex() == index1;
052        }
053        
054        public void finalizeSentence(DependencyStructure dependencyGraph) throws MaltChainedException {}
055        
056        public void terminate() throws MaltChainedException {}
057}