001    package org.maltparser.parser.algorithm.planar;
002    
003    import org.maltparser.core.exception.MaltChainedException;
004    import org.maltparser.core.syntaxgraph.DependencyStructure;
005    import org.maltparser.core.syntaxgraph.node.DependencyNode;
006    import org.maltparser.parser.DependencyParserConfig;
007    import org.maltparser.parser.Oracle;
008    import org.maltparser.parser.ParserConfiguration;
009    import org.maltparser.parser.history.GuideUserHistory;
010    import org.maltparser.parser.history.action.GuideUserAction;
011    /**
012     * @author Carlos Gomez Rodriguez
013     *
014     */
015    public 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                            return updateActionContainers(Planar.REDUCE, null);
040                    } else {
041                            return updateActionContainers(Planar.SHIFT, null);
042                    }
043                            
044            }
045            
046            private boolean checkIfArcExists ( DependencyStructure dg , int index1 , int index2 ) throws MaltChainedException
047            {
048                    return dg.getTokenNode(index2).hasHead() && dg.getTokenNode(index2).getHead().getIndex() == index1;
049            }
050            
051            public void finalizeSentence(DependencyStructure dependencyGraph) throws MaltChainedException {}
052            
053            public void terminate() throws MaltChainedException {}
054    }