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.OracleGuide; 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 */ 015public class BatchTrainer extends Trainer { 016 private final OracleGuide oracleGuide; 017 private final FeatureModel featureModel; 018 019 public BatchTrainer(DependencyParserConfig manager, SymbolTableHandler symbolTableHandler) throws MaltChainedException { 020 super(manager,symbolTableHandler); 021 registry.setAlgorithm(this); 022 setGuide(new SingleGuide(this, ClassifierGuide.GuideMode.BATCH)); 023 String featureModelFileName = manager.getOptionValue("guide", "features").toString().trim(); 024 if (manager.isLoggerInfoEnabled()) { 025 manager.logDebugMessage(" Feature model : " + featureModelFileName+"\n"); 026 manager.logDebugMessage(" Learner : " + manager.getOptionValueString("guide", "learner").toString()+"\n"); 027 } 028 String dataSplitColumn = manager.getOptionValue("guide", "data_split_column").toString().trim(); 029 String dataSplitStructure = manager.getOptionValue("guide", "data_split_structure").toString().trim(); 030 this.featureModel = manager.getFeatureModelManager().getFeatureModel(SingleGuide.findURL(featureModelFileName, manager), 0, getParserRegistry(), dataSplitColumn, dataSplitStructure); 031 032 manager.writeInfoToConfigFile("\nFEATURE MODEL\n"); 033 manager.writeInfoToConfigFile(featureModel.toString()); 034 035 oracleGuide = parserState.getFactory().makeOracleGuide(parserState.getHistory()); 036 } 037 038 public DependencyStructure parse(DependencyStructure goldDependencyGraph, DependencyStructure parseDependencyGraph) throws MaltChainedException { 039 parserState.clear(); 040 parserState.initialize(parseDependencyGraph); 041 currentParserConfiguration = parserState.getConfiguration(); 042 043 TransitionSystem transitionSystem = parserState.getTransitionSystem(); 044 while (!parserState.isTerminalState()) { 045 GuideUserAction action = transitionSystem.getDeterministicAction(parserState.getHistory(), currentParserConfiguration); 046 if (action == null) { 047 action = oracleGuide.predict(goldDependencyGraph, currentParserConfiguration); 048 try { 049 classifierGuide.addInstance(featureModel, (GuideDecision)action); 050 } catch (NullPointerException e) { 051 throw new MaltChainedException("The guide cannot be found. ", e); 052 } 053 } 054 parserState.apply(action); 055 } 056 copyEdges(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph); 057 parseDependencyGraph.linkAllTreesToRoot(); 058 oracleGuide.finalizeSentence(parseDependencyGraph); 059 return parseDependencyGraph; 060 } 061 062 public OracleGuide getOracleGuide() { 063 return oracleGuide; 064 } 065 066 public void train() throws MaltChainedException { } 067 public void terminate() throws MaltChainedException { 068 } 069}