001 package org.maltparser.parser; 002 003 import org.maltparser.core.exception.MaltChainedException; 004 import org.maltparser.core.syntaxgraph.DependencyStructure; 005 006 import org.maltparser.parser.guide.ClassifierGuide; 007 import org.maltparser.parser.guide.SingleGuide; 008 import org.maltparser.parser.history.GuideHistory; 009 import org.maltparser.parser.history.action.GuideDecision; 010 import org.maltparser.parser.history.action.GuideUserAction; 011 /** 012 * @author Johan Hall 013 * 014 */ 015 public class DeterministicParser extends Parser { 016 private int parseCount; 017 018 public DeterministicParser(DependencyParserConfig manager) throws MaltChainedException { 019 super(manager); 020 setManager(manager); 021 initParserState(1); 022 ((SingleMalt)manager).addRegistry(org.maltparser.parser.Algorithm.class, this); 023 setGuide(new SingleGuide(manager, (GuideHistory)parserState.getHistory(), ClassifierGuide.GuideMode.CLASSIFY)); 024 } 025 026 public DependencyStructure parse(DependencyStructure parseDependencyGraph) throws MaltChainedException { 027 parserState.clear(); 028 parserState.initialize(parseDependencyGraph); 029 currentParserConfiguration = parserState.getConfiguration(); 030 parseCount++; 031 if (diagnostics == true) { 032 writeToDiaFile(parseCount + ""); 033 } 034 while (!parserState.isTerminalState()) { 035 GuideUserAction action = parserState.getTransitionSystem().getDeterministicAction(parserState.getHistory(), currentParserConfiguration); 036 if (action == null) { 037 action = predict(); 038 } else if (diagnostics == true) { 039 writeToDiaFile(" *"); 040 } 041 if (diagnostics == true) { 042 writeToDiaFile(" " + parserState.getTransitionSystem().getActionString(action)); 043 } 044 parserState.apply(action); 045 } 046 copyEdges(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph); 047 copyDynamicInput(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph); 048 parseDependencyGraph.linkAllTreesToRoot(); 049 if (diagnostics == true) { 050 writeToDiaFile("\n"); 051 } 052 return parseDependencyGraph; 053 } 054 055 private GuideUserAction predict() throws MaltChainedException { 056 GuideUserAction currentAction = parserState.getHistory().getEmptyGuideUserAction(); 057 try { 058 classifierGuide.predict((GuideDecision)currentAction); 059 while (!parserState.permissible(currentAction)) { 060 if (classifierGuide.predictFromKBestList((GuideDecision)currentAction) == false) { 061 currentAction = getParserState().getTransitionSystem().defaultAction(parserState.getHistory(), currentParserConfiguration); 062 break; 063 } 064 } 065 } catch (NullPointerException e) { 066 throw new MaltChainedException("The guide cannot be found. ", e); 067 } 068 return currentAction; 069 } 070 071 public void terminate() throws MaltChainedException { 072 if (diagnostics == true) { 073 closeDiaWriter(); 074 } 075 } 076 }