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;
007import org.maltparser.parser.guide.ClassifierGuide;
008import org.maltparser.parser.guide.SingleGuide;
009import org.maltparser.parser.history.action.GuideDecision;
010import org.maltparser.parser.history.action.GuideUserAction;
011
012public class DeterministicParserWithDiagnostics extends Parser {
013        private final Diagnostics diagnostics;
014        private int parseCount;
015        private final FeatureModel featureModel;
016        
017        public DeterministicParserWithDiagnostics(DependencyParserConfig manager, SymbolTableHandler symbolTableHandler) throws MaltChainedException {
018                super(manager,symbolTableHandler);
019                this.diagnostics = new Diagnostics(manager.getOptionValue("singlemalt", "diafile").toString());
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        
033        public DependencyStructure parse(DependencyStructure parseDependencyGraph) throws MaltChainedException {
034                parserState.clear();
035                parserState.initialize(parseDependencyGraph);
036                currentParserConfiguration = parserState.getConfiguration();
037                parseCount++;
038
039                diagnostics.writeToDiaFile(parseCount + "");
040                while (!parserState.isTerminalState()) {
041                        GuideUserAction action = parserState.getTransitionSystem().getDeterministicAction(parserState.getHistory(), currentParserConfiguration);
042                        if (action == null) {
043                                action = predict();
044                        } else {
045                                diagnostics.writeToDiaFile(" *");
046                        }
047
048                        diagnostics.writeToDiaFile(" " + parserState.getTransitionSystem().getActionString(action));
049
050                        parserState.apply(action);
051                } 
052                copyEdges(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph);
053                copyDynamicInput(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph);
054                parseDependencyGraph.linkAllTreesToRoot();
055
056                diagnostics.writeToDiaFile("\n");
057
058                return parseDependencyGraph;
059        }
060        
061        
062        private GuideUserAction predict() throws MaltChainedException {
063                GuideUserAction currentAction = parserState.getHistory().getEmptyGuideUserAction();
064                try {
065                        classifierGuide.predict(featureModel,(GuideDecision)currentAction);
066                        while (!parserState.permissible(currentAction)) {
067                                if (classifierGuide.predictFromKBestList(featureModel,(GuideDecision)currentAction) == false) {
068                                        currentAction = getParserState().getTransitionSystem().defaultAction(parserState.getHistory(), currentParserConfiguration);
069                                        break;
070                                }
071                        }
072                } catch (NullPointerException e) {
073                        throw new MaltChainedException("The guide cannot be found. ", e);
074                }
075                return currentAction;
076        }
077        
078        public void terminate() throws MaltChainedException {
079                diagnostics.closeDiaWriter();
080        }
081}