private static void runAnnotation()

in languagetool-http-client/src/main/java/org/languagetool/remote/SentenceAnnotator.java [102:286]


  private static void runAnnotation(AnnotatorConfig cfg) throws IOException, NoSuchAlgorithmException, InterruptedException {
    List<String> lines = Files.readAllLines(Paths.get(cfg.inputFilePath));
    int numSentence = 0;
    Scanner sc = new Scanner(System.in);
    System.out.print("Start at line? ");
    String response = sc.nextLine();
    int startLine = 0;
    try {
      startLine = Integer.valueOf(response);
    } catch (NumberFormatException ex) {
      startLine = 0;
    }
    System.out.println("Starting at line " + String.valueOf(startLine) + " of file " + cfg.inputFilePath);
    boolean quit = false;
    for (String line : lines) {
      if (quit) {
        break;
      }
      numSentence++;
      if (numSentence < startLine) {
        continue;
      }
      String[] partsLine = line.split("\t");
      String sentenceID;
      String originalSentence;
      if (partsLine.length == 2) {
        originalSentence = partsLine[1];
        sentenceID = partsLine[0];
      } else {
        originalSentence = partsLine[0];
        sentenceID = "N/A";
      }
      String sentence = originalSentence;
      String sentenceHash = md5FromSentence(sentence);
      boolean done = false;
      List<String> fpMatches = new ArrayList<>();
      int annotationsPerSentence = 0;
      while (!done) {
        List<RemoteRuleMatch> matches = getMatches(cfg, sentence);
        RemoteRuleMatch match = null;
        int i = 0;
        boolean isValidMatch = false;
        while (!isValidMatch && i < matches.size()) {
          match = matches.get(i);
          i++;
          isValidMatch = !fpMatches.contains(getMatchIdentifier(sentence, match));
          if (cfg.ignoreStyleRules && match.getLocQualityIssueType().get().equals("style")) {
            isValidMatch = false;
          }
          if (!isValidMatch) {
            match = null;
          }
        }
        String formattedSentence = formatedSentence(sentence, match);
        String formattedCorrectedSentence = formattedSentence;
        String detectedErrorStr = "";
        System.out.println(cfg.ansiDefault + "=============================================");
        System.out.println("Sentence no. " + String.valueOf(numSentence));
        System.out.println("---------------------------------------------");
        System.out.println(cfg.ansiHighlight + formattedSentence + cfg.ansiDefault);
        System.out.println("---------------------------------------------");
        if (match != null) {
          System.out.println(match.getMessage());
          detectedErrorStr = sentence.substring(match.getErrorOffset(),
              match.getErrorOffset() + match.getErrorLength());
        }
        System.out.println(listSuggestions(match, detectedErrorStr));
        System.out.println("---------------------------------------------");
        System.out.print("Action? ");
        response = sc.nextLine();
        if (!response.contains(">>")) {
          response = response.toLowerCase();
        }
        String errorType = "";
        int suggestionPos = -1;
        String suggestionApplied = "";
        int suggestionsTotal = 0;
        if (match != null) {
          suggestionsTotal = match.getReplacements().get().size();
        }
        switch (response) {
        case "r":
          sentence = originalSentence;
          fpMatches.clear();
          cfg.outStrB = new StringBuilder();
          break;
        case "q":
          done = true;
          quit = true;
          writeToOutputFile(cfg);
          break;
        case "d":
          done = true;
          if (annotationsPerSentence == 0) {
            errorType = "OK";
          }
          writeToOutputFile(cfg);
          break;
        case "g":
          done = true;
          errorType = "IG";
          cfg.outStrB = new StringBuilder();
          match = null;
          break;
        case "i":
          fpMatches.add(getMatchIdentifier(sentence, match));
          errorType = "IM";
          break;
        case "b":
          fpMatches.add(getMatchIdentifier(sentence, match));
          errorType = "BO";
          break;
        case "f":
          fpMatches.add(getMatchIdentifier(sentence, match));
          errorType = "FP";
          break;
        case "1":
        case "2":
        case "3":
        case "4":
        case "5":
          errorType = "TP";
          if (suggestionsTotal > 1) {
            errorType = "TPmultiple";
          }
          int r = Integer.valueOf(response);
          if (match != null && r >= 1 && r <= 5) {
            formattedCorrectedSentence = formattedCorrectedSentence(sentence, match, r);
            sentence = replaceSuggestion(sentence, match, r);
            suggestionPos = r;
            suggestionApplied = match.getReplacements().get().get(suggestionPos - 1);
          }
          break;
        }
        if (quit) {
          break;
        }
        if (response.startsWith(">>") && match != null) { // alternative suggestion
          formattedCorrectedSentence = sentence.substring(0, match.getErrorOffset()) + "___" + response.substring(2)
              + "___" + sentence.substring(match.getErrorOffset() + match.getErrorLength());
          sentence = sentence.substring(0, match.getErrorOffset()) + response.substring(2)
              + sentence.substring(match.getErrorOffset() + match.getErrorLength());
          if (suggestionsTotal == 0) {
            errorType = "TPno";
          } else {
            errorType = "TPwrong";
          }
          suggestionApplied = response.substring(2);
        } else if (response.contains(">>")) {
          String[] parts = response.split(">>");
          String toReplace = parts[0];
          String replacement = parts[1];
          int ind = sentence.indexOf(toReplace);
          if (ind > -1) {
            if (sentence.substring(ind + toReplace.length()).indexOf(toReplace) > -1) {
              System.out.println("Cannot replace duplicate string in sentence.");
            } else {
              formattedSentence = sentence.substring(0, ind) + "___" + toReplace + "___"
                  + sentence.substring(ind + toReplace.length());
              formattedCorrectedSentence = sentence.substring(0, ind) + "___" + replacement + "___"
                  + sentence.substring(ind + toReplace.length());
              sentence = sentence.substring(0, ind) + replacement + sentence.substring(ind + toReplace.length());
              System.out.println("FN: replacement done.");
              errorType = "FN";
              suggestionApplied = replacement;
              detectedErrorStr = toReplace;
            }
          }
        }
        String suggestionGolden = ""; // TODO
        if (!errorType.isEmpty()) {
          printOutputLine(cfg, sentenceHash, sentenceID, formattedSentence, formattedCorrectedSentence, errorType, detectedErrorStr,
              suggestionApplied, suggestionGolden, suggestionPos, suggestionsTotal, getFullId(match), getRuleCategoryId(match),
              getRuleType(match));
          annotationsPerSentence++;
          if (errorType.equals("OK") || errorType.equals("IG")) {
            writeToOutputFile(cfg);
            cfg.outStrB = new StringBuilder();
          }
        }
      }
    }
    sc.close();
    cfg.out.close();
  }