Пример #1
0
  public static Word readIn(Word listStart) throws FileNotFoundException {
    Scanner in = new Scanner(System.in);
    File inFile = new File(in.next());
    Scanner freader = new Scanner(inFile);
    StringTokenizer sentences = new StringTokenizer("");
    StringTokenizer words = new StringTokenizer("");
    String delims = ".!?";
    String sentence = "";
    String word = "";
    String previous = "";
    String line = null;
    String paragraph = null;

    while (freader.hasNextLine()) {

      paragraph = freader.nextLine();
      sentences = new StringTokenizer(paragraph, delims, true);
      while (sentences.hasMoreTokens()) {
        sentence = sentences.nextToken();
        words = new StringTokenizer(sentence);
        while (words.hasMoreTokens()) {
          word = words.nextToken();
          listStart = Word.addWord(listStart, word, previous);
          previous = word;
        }
      }
    }
    freader.close();

    return listStart;
  }
Пример #2
0
  public static Word addWord(Word listStart, String nextWord, String previous) {
    Word c = listStart;
    Word d = null;
    // .right list
    if (c == null) // first word
    {
      listStart = new Word(nextWord);
      listStart.beginsSentence = true;
      return listStart;
    } else // next word
    {
      if (count(listStart, nextWord) > 0) // if in the list, get to it and increment
      {
        while (c.right != null) {
          if (c.word.equals(nextWord)) {
            c.numOccurrences++;
          }
          c = c.right;
        }
      } else // if not in the list, get to the end and add word
      {
        while (c.right != null) {
          c = c.right;
        }
        c.right = new Word(nextWord);
        if (previous.equals(".") || previous.equals("?") || previous.equals("!")) {
          c.right.beginsSentence = true;
        }
      }
      c = listStart; // reset

      // .down list
      while (c
          != null) // go through again, looking for previous word to add following word to down list
      {
        d = c;
        if (c.word.equals(".") || c.word.equals("?") || c.word.equals("!")) {
          c.endsSentence = true;
        } else {
          if (d.word.equals(previous)) {
            if (d.down == null) // no Words in .down list
            {
              d.down = new Word(nextWord);
              if (d.down.word.equals(".") || d.down.word.equals("?") || d.down.word.equals("!")) {
                d.endsSentence = true;
              }
            } else { // if there are words in the .down list
              if (countDown(c, nextWord) > 0) // find it and increment
              {
                while (d.down != null) {
                  if (d.down.word.equals(nextWord)) {
                    d.down.numOccurrences++;
                  }
                  d = d.down;
                }
              } else // get to end and add new
              {
                while (d.down != null) {
                  d = d.down;
                }
                d.down = new Word(nextWord);
                if (d.down.word.equals(".") || d.down.word.equals("?") || d.down.word.equals("!")) {
                  d.endsSentence = true;
                }
              }
            }
          }
        }
        c = c.right;
      }
    }
    return listStart;
  }