Exemple #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();
  }
  private void writeObject(ObjectOutputStream out) throws IOException {
    out.writeInt(CURRENT_SERIAL_VERSION);
    out.writeInt(string1Length);
    out.writeInt(string2Length);
    out.writeObject(string1);
    out.writeObject(string2);

    if (string1Blocks == null) {
      out.writeInt(NULL_INTEGER);
    } else {
      int size = string1Blocks.length;
      out.writeInt(size);
      for (int i = 0; i < size; i++) {
        out.writeObject(string1Blocks[i]);
      }
    }

    if (string2Blocks == null) {
      out.writeInt(NULL_INTEGER);
    } else {
      int size = string2Blocks.length;
      out.writeInt(size);
      for (int i = 0; i < size; i++) {
        out.writeObject(string2Blocks[i]);
      }
    }

    out.writeObject(string1Present);
    out.writeObject(string2Present);
    out.writeObject(lexicon);

    if (block1Indices == null) {
      out.writeInt(NULL_INTEGER);
    } else {
      int size = block1Indices.length;
      out.writeInt(size);
      for (int i = 0; i < size; i++) {
        out.writeInt(block1Indices[i]);
      }
    }

    if (block2Indices == null) {
      out.writeInt(NULL_INTEGER);
    } else {
      int size = block2Indices.length;
      out.writeInt(size);
      for (int i = 0; i < size; i++) {
        out.writeInt(block2Indices[i]);
      }
    }

    out.writeChar(delim);
  }