Package opennlp.tools.cmdline

Examples of opennlp.tools.cmdline.TerminateToolException


   */
  public void run(String[] args) {

    if (!ArgumentParser.validateArguments(args, Parameters.class)) {
      System.err.println(getHelp());
      throw new TerminateToolException(1);
    }

    Parameters params = ArgumentParser.parse(args, Parameters.class);

    File testData = new File(params.getCensusData());
    File dictOutFile = new File(params.getDict());

    CmdLineUtil.checkInputFile("Name data", testData);
    CmdLineUtil.checkOutputFile("Dictionary file", dictOutFile);

    FileInputStream sampleDataIn = CmdLineUtil.openInFile(testData);
    ObjectStream<StringList> sampleStream = new NameFinderCensus90NameStream(sampleDataIn,
        Charset.forName(params.getEncoding()));
   
    Dictionary mDictionary;
    try {
      System.out.println("Creating Dictionary...");
      mDictionary = createDictionary(sampleStream);
    } catch (IOException e) {
      CmdLineUtil.printTrainingIoError(e);
      throw new TerminateToolException(-1);
    } finally {
      try {
        sampleStream.close();
      } catch(IOException e) {
        // sorry this can fail..
      }
    }

    System.out.println("Saving Dictionary...");
   
    OutputStream out = null;
   
    try {
      out = new FileOutputStream(dictOutFile);
      mDictionary.serialize(out);
    } catch (IOException ex) {
      System.err.println("Error during write to dictionary file: " + ex.getMessage());
      throw new TerminateToolException(-1);
    }
    finally {
      if (out != null)
        try {
          out.close();
        } catch (IOException e) {
          // file might be damaged
          System.err.println("Attention: Failed to correctly write dictionary:");
          System.err.println(e.getMessage());
          throw new TerminateToolException(-1);
        }
    }
  }
View Full Code Here


  }

  public void run(String[] args) {
    if (!ArgumentParser.validateArguments(args, CVToolParams.class)) {
      System.err.println(getHelp());
      throw new TerminateToolException(1);
    }
   
    CVToolParams params = ArgumentParser.parse(args,
        CVToolParams.class);
   
    opennlp.tools.util.TrainingParameters mlParams = CmdLineUtil
        .loadTrainingParameters(params.getParams(), false);
   
    File trainingDataInFile = params.getData();
    CmdLineUtil.checkInputFile("Training Data", trainingDataInFile);
   
    ObjectStream<ChunkSample> sampleStream =
        ChunkerTrainerTool.openSampleData("Training Data",
        trainingDataInFile, params.getEncoding());
   
    List<EvaluationMonitor<ChunkSample>> listeners = new LinkedList<EvaluationMonitor<ChunkSample>>();
    ChunkerDetailedFMeasureListener detailedFMeasureListener = null;
    if (params.getMisclassified()) {
      listeners.add(new ChunkEvaluationErrorListener());
    }
    if (params.getDetailedF()) {
      detailedFMeasureListener = new ChunkerDetailedFMeasureListener();
      listeners.add(detailedFMeasureListener);
    }
   
    if (mlParams == null) {
      mlParams = new TrainingParameters();
      mlParams.put(TrainingParameters.ALGORITHM_PARAM, "MAXENT");
      mlParams.put(TrainingParameters.ITERATIONS_PARAM,
          Integer.toString(params.getIterations()));
      mlParams.put(TrainingParameters.CUTOFF_PARAM,
          Integer.toString(params.getCutoff()));
    }

    ChunkerCrossValidator validator = new ChunkerCrossValidator(
        params.getLang(), mlParams,
        listeners.toArray(new ChunkerEvaluationMonitor[listeners.size()]));
     
    try {
      validator.evaluate(sampleStream, params.getFolds());
    }
    catch (IOException e) {
      CmdLineUtil.printTrainingIoError(e);
      throw new TerminateToolException(-1);
    }
    finally {
      try {
        sampleStream.close();
      } catch (IOException e) {
View Full Code Here

 
  public final void run(String[] args) {

    if (!ArgumentParser.validateArguments(args, ModelUpdaterParams.class)) {
      System.err.println(getHelp());
      throw new TerminateToolException(1);
    }
   
    ModelUpdaterParams params = ArgumentParser.parse(args,
        ModelUpdaterParams.class);
   
    // Load model to be updated
    File modelFile = params.getModel();
    ParserModel originalParserModel = new ParserModelLoader().load(modelFile);

    ObjectStream<Parse> parseSamples = ParserTrainerTool.openTrainingData(params.getData(),
        params.getEncoding());
   
    ParserModel updatedParserModel;
    try {
      updatedParserModel = trainAndUpdate(originalParserModel,
          parseSamples, params);
    }
    catch (IOException e) {
      CmdLineUtil.printTrainingIoError(e);
      throw new TerminateToolException(-1);
    }
    finally {
      try {
        parseSamples.close();
      } catch (IOException e) {
View Full Code Here

  public void run(String[] args) {
   
    if (args.length != 2) {
      System.out.println(getHelp());
      throw new TerminateToolException(1);
    }
   
    File parserModelInFile = new File(args[0]);
    ParserModel parserModel = new ParserModelLoader().load(parserModelInFile);
   
View Full Code Here

  public void run(String[] args) {
   
    if (!ArgumentParser.validateArguments(args, CVToolParams.class)) {
      System.err.println(getHelp());
      throw new TerminateToolException(1);
    }
   
    CVToolParams params = ArgumentParser.parse(args, CVToolParams.class);
   
    opennlp.tools.util.TrainingParameters mlParams =
      CmdLineUtil.loadTrainingParameters(params.getParams(), false);
   
    File trainingDataInFile = params.getData();
    CmdLineUtil.checkInputFile("Training Data", trainingDataInFile);
   
    Charset encoding = params.getEncoding();
   
    ObjectStream<SentenceSample> sampleStream = SentenceDetectorTrainerTool.openSampleData("Training Data",
        trainingDataInFile, encoding);
   
    SDCrossValidator validator;
   
    SentenceDetectorEvaluationMonitor errorListener = null;
    if (params.getMisclassified()) {
      errorListener = new SentenceEvaluationErrorListener();
    }
   
    if (mlParams == null) {
      mlParams = new TrainingParameters();
      mlParams.put(TrainingParameters.ALGORITHM_PARAM, "MAXENT");
      mlParams.put(TrainingParameters.ITERATIONS_PARAM,
          Integer.toString(params.getIterations()));
      mlParams.put(TrainingParameters.CUTOFF_PARAM,
          Integer.toString(params.getCutoff()));
    }

    try {
      Dictionary abbreviations = SentenceDetectorTrainerTool.loadDict(params
          .getAbbDict());
      validator = new SDCrossValidator(params.getLang(), mlParams,
          abbreviations, errorListener);
     
      validator.evaluate(sampleStream, params.getFolds());
    }
    catch (IOException e) {
      CmdLineUtil.printTrainingIoError(e);
      throw new TerminateToolException(-1);
    }
    finally {
      try {
        sampleStream.close();
      } catch (IOException e) {
View Full Code Here

  public void run(String[] args) {
   
    if (!ArgumentParser.validateArguments(args, EvaluatorParams.class)) {
      System.err.println(getHelp());
      throw new TerminateToolException(1);
    }
   
    EvaluatorParams params = ArgumentParser.parse(args, EvaluatorParams.class);
   
    Charset encoding = params.getEncoding();
   
    SentenceModel model = new SentenceModelLoader().load(params.getModel());
   
    File trainingDataInFile = params.getData();
    CmdLineUtil.checkInputFile("Training Data", trainingDataInFile);
   
    SentenceDetectorEvaluationMonitor errorListener = null;
    if (params.getMisclassified()) {
      errorListener = new SentenceEvaluationErrorListener();
    }
   
    SentenceDetectorEvaluator evaluator = new SentenceDetectorEvaluator(
        new SentenceDetectorME(model), errorListener);
   
    System.out.print("Evaluating ... ");
      ObjectStream<SentenceSample> sampleStream = SentenceDetectorTrainerTool.openSampleData("Test",
          trainingDataInFile, encoding);
     
      try {
      evaluator.evaluate(sampleStream);
      }
      catch (IOException e) {
        CmdLineUtil.printTrainingIoError(e);
        throw new TerminateToolException(-1);
      }
      finally {
        try {
          sampleStream.close();
        } catch (IOException e) {
View Full Code Here

  }
 
  public void run(String[] args) {
    if (!ArgumentParser.validateArguments(args, TrainerToolParams.class)) {
      System.err.println(getHelp());
      throw new TerminateToolException(1);
    }
   
    TrainerToolParams params = ArgumentParser.parse(args,
        TrainerToolParams.class);

    opennlp.tools.util.TrainingParameters mlParams =
      CmdLineUtil.loadTrainingParameters(params.getParams(), false);
   
    if (mlParams != null) {
      if (TrainUtil.isSequenceTraining(mlParams.getSettings())) {
        System.err.println("Sequence training is not supported!");
        throw new TerminateToolException(-1);
      }
    }
   
    File trainingDataInFile = params.getData();
    File modelOutFile = params.getModel();

    CmdLineUtil.checkOutputFile("sentence detector model", modelOutFile);
    ObjectStream<SentenceSample> sampleStream =
        openSampleData("Training", trainingDataInFile, params.getEncoding());

    SentenceModel model;
    try {
      Dictionary dict = loadDict(params.getAbbDict());
      if (mlParams == null) {
        model = SentenceDetectorME.train(params.getLang(), sampleStream, true, dict,
            params.getCutoff(), params.getIterations());
      }
      else {
        model = SentenceDetectorME.train(params.getLang(), sampleStream, true, dict,
            mlParams);
      }
    } catch (IOException e) {
      CmdLineUtil.printTrainingIoError(e);
      throw new TerminateToolException(-1);
    }
    finally {
      try {
        sampleStream.close();
      } catch (IOException e) {
View Full Code Here

   */
  public void run(String[] args) {
   
    if (args.length != 1) {
      System.out.println(getHelp());
      throw new TerminateToolException(1);
    }

    SentenceModel model = new SentenceModelLoader().load(new File(args[0]));
   
    SentenceDetectorME sdetector = new SentenceDetectorME(model);
View Full Code Here

  public void run(String[] args) {
   
    if (args.length != 1) {
      System.out.println(getHelp());
      throw new TerminateToolException(1);
    }
   
    POSModel model = new POSModelLoader().load(new File(args[0]));
   
    POSTaggerME tagger = new POSTaggerME(model);
View Full Code Here

  public void run(String[] args) {
    if (!ArgumentParser
        .validateArguments(args, EvaluatorParams.class)) {
      System.err.println(getHelp());
      throw new TerminateToolException(1);
    }

    EvaluatorParams params = ArgumentParser.parse(args,
        EvaluatorParams.class);

    File testData = params.getData();
    CmdLineUtil.checkInputFile("Test data", testData);

    Charset encoding = params.getEncoding();

    POSModel model = new POSModelLoader().load(params.getModel());
   
    POSTaggerEvaluationMonitor missclassifiedListener = null;
    if (params.getMisclassified()) {
      missclassifiedListener = new POSEvaluationErrorListener();
    }

    POSEvaluator evaluator = new POSEvaluator(
        new opennlp.tools.postag.POSTaggerME(model), missclassifiedListener);

      System.out.print("Evaluating ... ");
     
      ObjectStream<POSSample> sampleStream =
          POSTaggerTrainerTool.openSampleData("Test", testData, encoding);
     
      try {
        evaluator.evaluate(sampleStream);
      }
      catch (IOException e) {
        System.err.println("failed");
        System.err.println("Reading test data error " + e.getMessage());
        throw new TerminateToolException(-1);
      } finally {
        try {
          sampleStream.close();
        } catch (IOException e) {
          // sorry that this can fail
View Full Code Here

TOP

Related Classes of opennlp.tools.cmdline.TerminateToolException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.