Exemple #1
0
  private static void execPrintTypesDoeCorpus(String[] args) {
    boolean ascending = true;

    // Ascending or descending?
    if (args.length > 1)
      if ("asc".equalsIgnoreCase(args[1])) ascending = true;
      else if ("desc".equalsIgnoreCase(args[1])) ascending = false;
      else throw new IllegalArgumentException("Second parameter must be either 'asc' or 'desc'");

    // Save to file?
    String path = "";
    List<String> lines = null;
    boolean writeToFile = false;
    if (args.length > 2) {
      path = args[2];
      lines = new ArrayList<>();
      writeToFile = true;
    }

    // Load corpus first
    execParseDoeCorpus(false /* doSave */);

    HashMap<String, Integer> types = corpus.getVocabulary();
    Map<String, Integer> sortedTypes = ListAndMapUtil.sortByComparator(types, ascending);

    for (String key : sortedTypes.keySet()) {
      String line = key + ";" + sortedTypes.get(key);
      System.out.println(line);

      if (writeToFile) lines.add(line);
    }

    // Save to file
    if (writeToFile)
      try {
        FileUtils.writeLines(new File(path), lines);
      } catch (IOException e) {
        System.out.println("Error while trying to write to file " + path);
        e.printStackTrace();
      }
  }