001package org.maltparser.parser;
002
003import org.maltparser.core.exception.MaltChainedException;
004import org.maltparser.core.feature.FeatureModel;
005import org.maltparser.core.symbol.SymbolTableHandler;
006import org.maltparser.core.syntaxgraph.DependencyStructure;
007
008import org.maltparser.parser.guide.ClassifierGuide;
009import org.maltparser.parser.guide.SingleGuide;
010import org.maltparser.parser.history.action.GuideDecision;
011import org.maltparser.parser.history.action.GuideUserAction;
012/**
013 * @author Johan Hall
014 *
015 */
016public class DeterministicParser extends Parser {
017        private final FeatureModel featureModel;
018        public DeterministicParser(DependencyParserConfig manager, SymbolTableHandler symbolTableHandler) throws MaltChainedException {
019                super(manager,symbolTableHandler);
020                registry.setAlgorithm(this);
021                setGuide(new SingleGuide(this, ClassifierGuide.GuideMode.CLASSIFY));
022                String featureModelFileName = manager.getOptionValue("guide", "features").toString().trim();
023                if (manager.isLoggerInfoEnabled()) {
024                        manager.logDebugMessage("  Feature model        : " + featureModelFileName+"\n");
025                        manager.logDebugMessage("  Classifier           : " + manager.getOptionValueString("guide", "learner")+"\n");   
026                }
027                String dataSplitColumn = manager.getOptionValue("guide", "data_split_column").toString().trim();
028                String dataSplitStructure = manager.getOptionValue("guide", "data_split_structure").toString().trim();
029                featureModel = manager.getFeatureModelManager().getFeatureModel(SingleGuide.findURL(featureModelFileName, manager), 0, getParserRegistry(), dataSplitColumn, dataSplitStructure);
030        }
031        
032        public DependencyStructure parse(DependencyStructure parseDependencyGraph) throws MaltChainedException {
033                parserState.clear();
034                parserState.initialize(parseDependencyGraph);
035                currentParserConfiguration = parserState.getConfiguration();
036                TransitionSystem ts = parserState.getTransitionSystem();
037                while (!parserState.isTerminalState()) {
038                        GuideUserAction action = ts.getDeterministicAction(parserState.getHistory(), currentParserConfiguration);
039                        if (action == null) {
040                                action = predict();
041                        }
042                        parserState.apply(action);
043                } 
044//              copyEdges(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph);
045//              copyDynamicInput(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph);
046                parseDependencyGraph.linkAllTreesToRoot();
047                return parseDependencyGraph;
048        }
049        
050        private GuideUserAction predict() throws MaltChainedException {
051                GuideUserAction currentAction = parserState.getHistory().getEmptyGuideUserAction();
052                try {
053                        classifierGuide.predict(featureModel,(GuideDecision)currentAction);
054                        while (!parserState.permissible(currentAction)) {
055                                if (classifierGuide.predictFromKBestList(featureModel,(GuideDecision)currentAction) == false) {
056                                        currentAction = getParserState().getTransitionSystem().defaultAction(parserState.getHistory(), currentParserConfiguration);
057                                        break;
058                                }
059                        }
060                } catch (NullPointerException e) {
061                        throw new MaltChainedException("The guide cannot be found. ", e);
062                }
063                return currentAction;
064        }
065        
066        public void terminate() throws MaltChainedException { }
067}