Exemplo n.º 1
0
 private static boolean fileExists(String fileName) {
   File checker = new File(fileName);
   return checker.exists();
 }
Exemplo n.º 2
0
  private static String createUnifiedRefFile(String prefix, int numFiles) throws Exception {
    if (numFiles < 2) {
      println(
          "Warning: createUnifiedRefFile called with numFiles = " + numFiles + "; doing nothing.");
      return prefix;
    } else {
      File checker;
      checker = new File(prefix + "1");

      if (!checker.exists()) {
        checker = new File(prefix + ".1");
        if (!checker.exists()) {
          println("Can't find reference files.");
          System.exit(50);
        } else {
          prefix = prefix + ".";
        }
      }

      String outFileName;
      if (prefix.endsWith(".")) {
        outFileName = prefix + "all";
      } else {
        outFileName = prefix + ".all";
      }

      PrintWriter outFile = new PrintWriter(outFileName);

      BufferedReader[] inFile = new BufferedReader[numFiles];

      int nextIndex;
      checker = new File(prefix + "0");
      if (checker.exists()) {
        nextIndex = 0;
      } else {
        nextIndex = 1;
      }
      int lineCount = countLines(prefix + nextIndex);

      for (int r = 0; r < numFiles; ++r) {
        if (countLines(prefix + nextIndex) != lineCount) {
          println("Line count mismatch in " + (prefix + nextIndex) + ".");
          System.exit(60);
        }
        inFile[r] = new BufferedReader(new FileReader(prefix + nextIndex));
        ++nextIndex;
      }

      String line;

      for (int i = 0; i < lineCount; ++i) {
        for (int r = 0; r < numFiles; ++r) {
          line = inFile[r].readLine();
          outFile.println(line);
        }
      }

      outFile.close();

      for (int r = 0; r < numFiles; ++r) {
        inFile[r].close();
      }

      return outFileName;
    }
  } // createUnifiedRefFile(String prefix, int numFiles)
Exemplo n.º 3
0
  private static void processArgsAndInitialize(String[] args) throws Exception {
    EvaluationMetric.set_knownMetrics();

    // set default values
    candFileName = "candidates.txt";
    candFileFormat = "plain";
    candRank = 1;
    refFileName = "references.txt";
    refsPerSen = 1;
    metricName = "BLEU";
    metricOptions = new String[2];
    metricOptions[0] = "4";
    metricOptions[1] = "closest";
    evaluateRefs = false;
    verbose = false;

    int i = 0;

    while (i < args.length) {
      String option = args[i];
      if (option.equals("-cand")) {
        candFileName = args[i + 1];
      } else if (option.equals("-format")) {
        candFileFormat = args[i + 1];
        if (!candFileFormat.equals("plain") && !candFileFormat.equals("nbest")) {
          println("candFileFormat must be either plain or nbest.");
          System.exit(10);
        }
      } else if (option.equals("-rank")) {
        candRank = Integer.parseInt(args[i + 1]);
        if (refsPerSen < 1) {
          println("Argument for -rank must be positive.");
          System.exit(10);
        }
      } else if (option.equals("-ref")) {
        refFileName = args[i + 1];
      } else if (option.equals("-rps")) {
        refsPerSen = Integer.parseInt(args[i + 1]);
        if (refsPerSen < 1) {
          println("refsPerSen must be positive.");
          System.exit(10);
        }
      } else if (option.equals("-m")) {
        metricName = args[i + 1];
        if (EvaluationMetric.knownMetricName(metricName)) {
          int optionCount = EvaluationMetric.metricOptionCount(metricName);
          metricOptions = new String[optionCount];
          for (int opt = 0; opt < optionCount; ++opt) {
            metricOptions[opt] = args[i + opt + 2];
          }
          i += optionCount;
        } else {
          println("Unknown metric name " + metricName + ".");
          System.exit(10);
        }
      }

      /*
            else if (option.equals("-m")) {
              metricName = args[i+1];
              if (!EvaluationMetric.knownMetricName(metricName)) { println("Unknown metric name " + metricName + "."); System.exit(10); }
              if (metricName.equals("BLEU")) {
                metricOptions = new String[2];
                metricOptions[0] = args[i+2];
                metricOptions[1] = args[i+3];
                i += 2;
              }
            }
      */
      else if (option.equals("-evr")) {
        int evr = Integer.parseInt(args[i + 1]);
        if (evr == 1) evaluateRefs = true;
        else if (evr == 0) evaluateRefs = false;
        else {
          println("evalRefs must be either 0 or 1.");
          System.exit(10);
        }
      } else if (option.equals("-v")) {
        int v = Integer.parseInt(args[i + 1]);
        if (v == 1) verbose = true;
        else if (v == 0) verbose = false;
        else {
          println("verbose must be either 0 or 1.");
          System.exit(10);
        }
      } else {
        println("Unknown option " + option);
        System.exit(10);
      }

      i += 2;
    } // while (i)

    if (refsPerSen > 1) {
      // the provided refFileName might be a prefix
      File dummy = new File(refFileName);
      if (!dummy.exists()) {
        refFileName = createUnifiedRefFile(refFileName, refsPerSen);
      }
    } else {
      checkFile(refFileName);
    }

    // initialize
    numSentences = countLines(refFileName) / refsPerSen;

    // read in reference sentences
    refSentences = new String[numSentences][refsPerSen];
    BufferedReader inFile_refs = new BufferedReader(new FileReader(refFileName));
    String line;

    for (i = 0; i < numSentences; ++i) {
      for (int r = 0; r < refsPerSen; ++r) {
        // read the rth reference translation for the ith sentence
        refSentences[i][r] = inFile_refs.readLine();
      }
    }

    inFile_refs.close();

    // set static data members for the EvaluationMetric class
    EvaluationMetric.set_numSentences(numSentences);
    EvaluationMetric.set_refsPerSen(refsPerSen);
    EvaluationMetric.set_refSentences(refSentences);

    // do necessary initialization for the evaluation metric
    evalMetric = EvaluationMetric.getMetric(metricName, metricOptions);

    println("Processing " + numSentences + " sentences...");
  } // processArgsAndInitialize(String[] args)