public String toString() {
   StringBuffer toReturn = new StringBuffer();
   for (int i = 0; i < size(); i++) {
     toReturn.append(((EntityConfidence) confidences.get(i)).toString() + " ");
   }
   return toReturn.toString();
 }
Beispiel #2
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 String toString() {
   StringBuffer toReturn = new StringBuffer();
   toReturn.append(
       this.entity
           + " / "
           + this.confidence
           + " / "
           + (this.correct ? "correct" : "incorrect")
           + "\n");
   return toReturn.toString();
 }
 public EntityConfidence(double conf, boolean corr, Sequence input, int start, int end) {
   this.confidence = conf;
   this.correct = corr;
   StringBuffer buff = new StringBuffer();
   if (input != null) {
     for (int j = start; j <= end; j++) {
       FeatureVector fv = (FeatureVector) input.get(j);
       for (int k = 0; k < fv.numLocations(); k++) {
         String featureName = fv.getAlphabet().lookupObject(fv.indexAtLocation(k)).toString();
         if (featureName.startsWith("W=") && featureName.indexOf("@") == -1) {
           buff.append(featureName.substring(featureName.indexOf('=') + 1) + " ");
         }
       }
     }
   }
   this.entity = buff.toString();
 }
 public String getNextLineGroup() {
   StringBuffer sb = new StringBuffer();
   String line;
   while (true) {
     try {
       line = reader.readLine();
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
     // System.out.println ("LineGroupIterator: got line: "+line);
     if (line == null) {
       break;
     } else if (!line.isEmpty()) {
       sb.append(line);
       sb.append('\n');
       break;
     }
   }
   if (sb.length() == 0) return null;
   else return sb.toString();
 }