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 if (diagnostics == true) { 031 writeToDiaFile("ParseCount: " + ++parseCount + "\n"); 032 } 033 while (!parserState.isTerminalState()) { 034 GuideUserAction action = parserState.getTransitionSystem().getDeterministicAction(parserState.getHistory(), currentParserConfiguration); 035 if (action == null) { 036 action = predict(); 037 } else if (diagnostics == true) { 038 writeToDiaFile("*"); 039 } 040 if (diagnostics == true) { 041 writeToDiaFile(parserState.getTransitionSystem().getActionString(action)); 042 writeToDiaFile("\n"); 043 } 044 parserState.apply(action); 045 } 046 copyEdges(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph); 047 parseDependencyGraph.linkAllTreesToRoot(); 048 if (diagnostics == true) { 049 writeToDiaFile("\n"); 050 } 051 return parseDependencyGraph; 052 } 053 054 private GuideUserAction predict() throws MaltChainedException { 055 GuideUserAction currentAction = parserState.getHistory().getEmptyGuideUserAction(); 056 try { 057 classifierGuide.predict((GuideDecision)currentAction); 058 while (!parserState.permissible(currentAction)) { 059 if (classifierGuide.predictFromKBestList((GuideDecision)currentAction) == false) { 060 currentAction = getParserState().getTransitionSystem().defaultAction(parserState.getHistory(), currentParserConfiguration); 061 break; 062 } 063 } 064 } catch (NullPointerException e) { 065 throw new MaltChainedException("The guide cannot be found. ", e); 066 } 067 return currentAction; 068 } 069 070 public void terminate() throws MaltChainedException { 071 if (diagnostics == true) { 072 closeDiaWriter(); 073 } 074 } 075 }