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 if (diagnostics == true) { 028 return parseDiagnostic(parseDependencyGraph); 029 } 030 parserState.clear(); 031 parserState.initialize(parseDependencyGraph); 032 currentParserConfiguration = parserState.getConfiguration(); 033 parseCount++; 034 035 while (!parserState.isTerminalState()) { 036 GuideUserAction action = parserState.getTransitionSystem().getDeterministicAction(parserState.getHistory(), currentParserConfiguration); 037 if (action == null) { 038 action = predict(); 039 } 040 parserState.apply(action); 041 } 042 copyEdges(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph); 043 copyDynamicInput(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph); 044 parseDependencyGraph.linkAllTreesToRoot(); 045 return parseDependencyGraph; 046 } 047 048 private DependencyStructure parseDiagnostic(DependencyStructure parseDependencyGraph) throws MaltChainedException { 049 parserState.clear(); 050 parserState.initialize(parseDependencyGraph); 051 currentParserConfiguration = parserState.getConfiguration(); 052 parseCount++; 053 if (diagnostics == true) { 054 writeToDiaFile(parseCount + ""); 055 } 056 while (!parserState.isTerminalState()) { 057 GuideUserAction action = parserState.getTransitionSystem().getDeterministicAction(parserState.getHistory(), currentParserConfiguration); 058 if (action == null) { 059 action = predict(); 060 } else if (diagnostics == true) { 061 writeToDiaFile(" *"); 062 } 063 if (diagnostics == true) { 064 writeToDiaFile(" " + parserState.getTransitionSystem().getActionString(action)); 065 } 066 parserState.apply(action); 067 } 068 copyEdges(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph); 069 copyDynamicInput(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph); 070 parseDependencyGraph.linkAllTreesToRoot(); 071 if (diagnostics == true) { 072 writeToDiaFile("\n"); 073 } 074 return parseDependencyGraph; 075 } 076 077 078 private GuideUserAction predict() throws MaltChainedException { 079 GuideUserAction currentAction = parserState.getHistory().getEmptyGuideUserAction(); 080 try { 081 classifierGuide.predict((GuideDecision)currentAction); 082 while (!parserState.permissible(currentAction)) { 083 if (classifierGuide.predictFromKBestList((GuideDecision)currentAction) == false) { 084 currentAction = getParserState().getTransitionSystem().defaultAction(parserState.getHistory(), currentParserConfiguration); 085 break; 086 } 087 } 088 } catch (NullPointerException e) { 089 throw new MaltChainedException("The guide cannot be found. ", e); 090 } 091 return currentAction; 092 } 093 094 public void terminate() throws MaltChainedException { 095 if (diagnostics == true) { 096 closeDiaWriter(); 097 } 098 } 099 }