示例#1
0
  public static void main(String[] args) throws Exception {
    Reader trainingFile = null;

    // Process arguments
    int restArgs = commandOptions.processOptions(args);

    // Check arguments
    if (restArgs != args.length) {
      commandOptions.printUsage(true);
      throw new IllegalArgumentException("Unexpected arg " + args[restArgs]);
    }
    if (trainFileOption.value == null) {
      commandOptions.printUsage(true);
      throw new IllegalArgumentException("Expected --train-file FILE");
    }
    if (modelFileOption.value == null) {
      commandOptions.printUsage(true);
      throw new IllegalArgumentException("Expected --model-file FILE");
    }

    // Get the CRF structure specification.
    ZipFile zipFile = new ZipFile(modelFileOption.value);
    ZipEntry zipEntry = zipFile.getEntry("crf-info.xml");
    CRFInfo crfInfo = new CRFInfo(zipFile.getInputStream(zipEntry));

    StringBuffer crfInfoBuffer = new StringBuffer();
    BufferedReader reader =
        new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
    String line;
    while ((line = reader.readLine()) != null) {
      crfInfoBuffer.append(line).append('\n');
    }
    reader.close();

    // Create the CRF, and train it.
    CRF4 crf = createCRF(trainFileOption.value, crfInfo);

    // Create a new zip file for our output.  This will overwrite
    // the file we used for input.
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(modelFileOption.value));

    // Copy the CRF info xml to the output zip file.
    zos.putNextEntry(new ZipEntry("crf-info.xml"));
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(zos));
    writer.write(crfInfoBuffer.toString());
    writer.flush();
    zos.closeEntry();

    // Save the CRF classifier model to the output zip file.
    zos.putNextEntry(new ZipEntry("crf-model.ser"));
    ObjectOutputStream oos = new ObjectOutputStream(zos);
    oos.writeObject(crf);
    oos.flush();
    zos.closeEntry();
    zos.close();
  }
  public static void main(String[] args) throws bsh.EvalError, java.io.IOException {
    // Process the command-line options
    commandOptions.process(args);

    System.out.println("Trainer = " + trainerConstructorOption.value.toString());
    ClassifierTrainer trainer = (ClassifierTrainer) trainerConstructorOption.value;
    InstanceList ilist = InstanceList.load(new File(instanceListFilenameOption.value));

    Random r = randomSeedOption.wasInvoked() ? new Random(randomSeedOption.value) : new Random();
    double t = trainingProportionOption.value;
    double v = validationProportionOption.value;
    InstanceList[] ilists = ilist.split(r, new double[] {t, v, 1 - t - v});
    System.err.println("Training...");
    Classifier c =
        trainer.train(
            ilists[0],
            ilists[1],
            null,
            (ClassifierEvaluating) classifierEvaluatorOption.value,
            null);
    if (printTrainAccuracyOption.value)
      System.out.print("Train accuracy = " + c.getAccuracy(ilists[0]) + "  ");
    if (printTestAccuracyOption.value)
      System.out.print("Test accuracy  = " + c.getAccuracy(ilists[2]));
    if (printTrainAccuracyOption.value || printTestAccuracyOption.value) System.out.println("");
    if (outputFilenameOption.wasInvoked()) {
      try {
        ObjectOutputStream oos =
            new ObjectOutputStream(new FileOutputStream(instanceListFilenameOption.value));
        oos.writeObject(c);
        oos.close();
      } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalArgumentException(
            "Couldn't write classifier to filename " + instanceListFilenameOption.value);
      }
    }
  }