Esempio n. 1
0
  /**
   * Returns a list of errors for the given line.
   *
   * @require line is not null
   * @ensure result is a list of every error as determined from this SpellChecker's dictionary.
   */
  private ArrayList<Error> generateErrorList(String line) {
    /*
     * errors holds the list of errors for this line
     * currentIndex records the position of the current word in the text
     * error holds each error before being added to the list of errors.
     */
    ArrayList<Error> errors = new ArrayList<Error>();
    int currentIndex = 0;
    Error error;

    /* words is the list of space-separated strings in the text */
    String[] words = line.split("\\s"); // "\s" is regex for whitespace
    for (String word : words) {
      /* loop invariant: 	errors holds the list of misspelled words
       * which have been discovered thus far.
       * 					currentIndex holds the position in the text at
       * which the current word begins.
       * 					error holds information about the misspelled
       * word discovered this iteration.
       */

      /*
       * specWord holds word with leading and trailing non-letters
       * removed
       */
      String specWord = stripToSpec(word);
      if (specWord.equals("")) { // Disregard the empty string
        continue;
      }

      if (!dictionary.lookup(specWord)) {
        /*
         * currentIndex is the index of the word starting after the
         * previously found word.
         */
        currentIndex = line.indexOf(specWord, currentIndex);

        /*
         * Generate a new error to be added to the list
         */
        error = new Error(currentIndex, specWord, dictionary.getCorrections(specWord));
        errors.add(error);

        /*
         * Advance the currentIndex to the start of the next word
         */
        currentIndex += word.length();
      }
    }

    return errors;
  }