package edu.jhu.hltcoe;

import java.io.PrintWriter;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import edu.jhu.hltcoe.data.DepTreebank;
import edu.jhu.hltcoe.data.SentenceCollection;
import edu.jhu.hltcoe.eval.DependencyParserEvaluator;
import edu.jhu.hltcoe.eval.Evaluator;
import edu.jhu.hltcoe.inference.Trainer;
import edu.jhu.hltcoe.inference.TrainerFactory;
import edu.jhu.hltcoe.parse.IlpViterbiParser;
import edu.stanford.nlp.ling.CategoryWordTag;
import edu.stanford.nlp.trees.CollinsHeadFinder;
import edu.stanford.nlp.trees.HeadFinder;
import edu.stanford.nlp.trees.MemoryTreebank;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreeVisitor;
import edu.stanford.nlp.trees.Treebank;

public class PipelineRunner {

    private static Logger log = Logger.getLogger(PipelineRunner.class);

    public PipelineRunner() {
    }

    public void run(CommandLine cmd) throws ParseException {        
        // Read the data
        log.info("Reading data");
        String trainPath = cmd.getOptionValue("train");
        Treebank treebank = new MemoryTreebank();
        CategoryWordTag.suppressTerminalDetails = true;
        treebank.loadPath(trainPath);
        final HeadFinder chf = new CollinsHeadFinder();
        treebank.apply(new TreeVisitor() {
          public void visitTree(Tree pt) {
            pt.percolateHeads(chf);
          }
        });
        
        DepTreebank depTreebank = new DepTreebank(treebank);
        SentenceCollection sentences = new SentenceCollection(depTreebank);

        // Train the model
        log.info("Training model");
        Trainer model = TrainerFactory.getModel(cmd);
        model.train(sentences);

        // Evaluate the model
        log.info("Evaluating model");
        PrintWriter pw = new PrintWriter(System.out);
        if (cmd.hasOption("psuedo-word")) {
            Evaluator pwEval = new DependencyParserEvaluator();
            pwEval.evaluate(model);
            pwEval.print(pw);
        }
    }

    public static Options createOptions() {
        Options options = new Options();
        
        // Options not specific to the model
        options.addOption("tr", "train", true, "Training data.");

        TrainerFactory.addOptions(options);
        return options;
    }

    private static void configureLogging() {
        BasicConfigurator.configure();
    }
    
    public static void main(String[] args) {
        configureLogging();
        
        String usage = "java " + PipelineRunner.class.getName() + " [OPTIONS]";
        CommandLineParser parser = new PosixParser();
        Options options = createOptions();
        String[] requiredOptions = new String[] { "train" };

        CommandLine cmd = null;
        final HelpFormatter formatter = new HelpFormatter();
        try {
            cmd = parser.parse(options, args);
        } catch (ParseException e1) {
            formatter.printHelp(usage, options, true);
            System.exit(1);
        }
        for (String requiredOption : requiredOptions) {
            if (!cmd.hasOption(requiredOption)) {
                formatter.printHelp(usage, options, true);
                System.exit(1);
            }
        }

        PipelineRunner pipeline = new PipelineRunner();
        try {
            pipeline.run(cmd);
        } catch (ParseException e1) {
            formatter.printHelp(usage, options, true);
            System.exit(1);
        }
    }

}

@@ -384,24 +384,60 @@
 4j.Logger;%0A%0A
+import edu.jhu.hltcoe.data.DepTree;%0A
 import edu.j
@@ -456,32 +456,32 @@
 ta.DepTreebank;%0A
-
 import edu.jhu.h
@@ -696,54 +696,8 @@
 ry;%0A
-import edu.jhu.hltcoe.parse.IlpViterbiParser;%0A
 impo
@@ -1687,60 +1687,1173 @@
     
-DepTreebank depTreebank = new DepTreebank(treebank);
+// Reduce size of treebank%0A        DepTreebank depTreebank;%0A        if (cmd.hasOption(%22maxSentenceLength%22) %7C%7C cmd.hasOption(%22maxNumSentences%22)) %7B%0A            DepTreebank depTreebankTmp = new DepTreebank(treebank);%0A            depTreebank = new DepTreebank();%0A            int maxSentenceLength = Integer.MAX_VALUE;%0A            if (cmd.hasOption(%22maxSentenceLength%22)) %7B%0A                maxSentenceLength = Integer.valueOf(cmd.getOptionValue(%22maxSentenceLength%22));%0A            %7D%0A            int maxNumSentences = Integer.MAX_VALUE;%0A            if (cmd.hasOption(%22maxNumSentences%22)) %7B%0A                maxNumSentences = Integer.valueOf(cmd.getOptionValue(%22maxNumSentences%22));%0A            %7D%0A            for (DepTree tree : depTreebankTmp) %7B%0A                if (depTreebank.size() %3E maxNumSentences) %7B%0A                    break;%0A                %7D%0A                int len = tree.getNodes().size();%0A                if (len %3C= maxSentenceLength) %7B%0A                    depTreebank.add(tree);%0A                %7D%0A            %7D%0A        %7D else %7B%0A            depTreebank = new DepTreebank(treebank);%0A        %7D%0A        log.info(%22Number of sentences: %22 + depTreebank.size());%0A        
 %0A   
@@ -3520,32 +3520,32 @@
 ic to the model%0A
-
         options.
@@ -3593,16 +3593,201 @@
 data.%22);
+%0A        options.addOption(%22msl%22, %22maxSentenceLength%22, true, %22Max sentence length.%22);%0A        options.addOption(%22mns%22, %22maxNumSentences%22, true, %22Max number of sentences for training.%22);
 %0A%0A      

Adding sentence number and length limits