    001    package org.maltparser.parser;
    002
    003    import org.maltparser.core.exception.MaltChainedException;
    004    import org.maltparser.core.syntaxgraph.DependencyStructure;
    005    import org.maltparser.parser.guide.ClassifierGuide;
    006    import org.maltparser.parser.guide.OracleGuide;
    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 BatchTrainer extends Trainer {
    016            private OracleGuide oracleGuide;
    017            private int parseCount;
    018
    019            public BatchTrainer(DependencyParserConfig manager) throws MaltChainedException {
    020                    super(manager);
    021                    ((SingleMalt)manager).addRegistry(org.maltparser.parser.Algorithm.class, this);
    022                    setManager(manager);
    023                    initParserState(1);
    024                    setGuide(new SingleGuide(manager, (GuideHistory)parserState.getHistory(), ClassifierGuide.GuideMode.BATCH));
    025                    oracleGuide = parserState.getFactory().makeOracleGuide(parserState.getHistory());
    026            }
    027
    028            public DependencyStructure parse(DependencyStructure goldDependencyGraph, DependencyStructure parseDependencyGraph) throws MaltChainedException {
    029                    parserState.clear();
    030                    parserState.initialize(parseDependencyGraph);
    031                    currentParserConfiguration = parserState.getConfiguration();
    032                    parseCount++;
    033                    if (diagnostics == true) {
    034                            writeToDiaFile(parseCount + "");
    035                    }
    036                    while (!parserState.isTerminalState()) {
    037                            GuideUserAction action = parserState.getTransitionSystem().getDeterministicAction(parserState.getHistory(), currentParserConfiguration);
    038                            if (action == null) {
    039                                    action = oracleGuide.predict(goldDependencyGraph, currentParserConfiguration);
    040                                    try {
    041                                            classifierGuide.addInstance((GuideDecision)action);
    042                                    } catch (NullPointerException e) {
    043                                            throw new MaltChainedException("The guide cannot be found. ", e);
    044                                    }
    045                            } else if (diagnostics == true) {
    046                                    writeToDiaFile(" *");
    047                            }
    048                            if (diagnostics == true) {
    049                                    writeToDiaFile(" " + parserState.getTransitionSystem().getActionString(action));
    050                            }
    051                            parserState.apply(action);
    052                    }
    053                    copyEdges(currentParserConfiguration.getDependencyGraph(), parseDependencyGraph);
    054                    parseDependencyGraph.linkAllTreesToRoot();
    055                    oracleGuide.finalizeSentence(parseDependencyGraph);
    056                    if (diagnostics == true) {
    057                            writeToDiaFile("\n");
    058                    }
    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                    if (diagnostics == true) {
    069                            closeDiaWriter();
    070                    }
    071            }
    072    }