Creating a concordance from a text file

Viewed 144

I have a text file and need to build a concordance out of it. I believe I need a method to update my line number and word count in my WordCount class, but I have trouble on how to do it. I know the method should be of type void since it just updates, and doesn't return any value. But I'm stuck on what to write. I've put a comment in the tester class on where I think this method should go. provided below are my tester, circularlist, and wordcount class. I appreciate any help on this, thanks.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Tester
{
    public static final int WordsPerLine = 10;

    public static void main() throws FileNotFoundException
    {
        //build then output hash table
        HashTable ht = new HashTable();
        System.out.println(ht.toString());

        String word; //read from input file
        WordCount wordToFind;  //search for this in the bst
        WordCount wordInTree;  //found in the bst

        //create generic BST, of WordCount here
        BSTree<WordCount> t = new BSTree<WordCount>();

        //want to read word at a time from input file
        Scanner wordsIn = new Scanner(new File("Hamlet.txt"));
        wordsIn.useDelimiter("[^A-Za-z']+");

        int wordCount = 0;
        int lineNum = 1;
        System.out.printf("%3d:  ", lineNum);
        while (wordsIn.hasNext()) {
            word = wordsIn.next();
            ++wordCount;
            System.out.print(word + " ");
            word = word.toLowerCase();
            
            if(t.find(new WordCount(word)) != null){
                wordToFind=  new WordCount(word);
                wordInTree= t.find(wordToFind); 
                //I need to have a method here that update word count and line number

            
            }



            
            
            if (wordCount % WordsPerLine == 0) {
                ++lineNum;
                System.out.printf("\n%3d:  ", lineNum);
            }
        }
        //EOF
        System.out.println();

        //print bst in alpha order
        System.out.println(t.toString());
    }
}

public class WordCount implements Comparable<WordCount>
{
    protected String word;
    protected int count;
    protected CircularList lineNums;

    //required for class to compile
    public int compareTo(WordCount other)
    {
        return word.compareTo(other.word);
    }

    {
        word = "";
        count = 0;
        lineNums= new CircularList();
    }

    public WordCount(String w)
    {
        word = w;
        count = 0;
        lineNums= new CircularList();
    }
    

  

    public String toString()
    {
        return String.format("%-12s %3d %3d", word, count, lineNums);
    }
}

public class CircularList
{
    private Item list;

    public CircularList()
    {
        list = null;
    }

    public Boolean isEmpty()
    {
        return list == null;
    }

    public void append(int x)
    {
        Item r = new Item(x);
        if (isEmpty()) {
            r.next = r;
        }
        else {
            r.next = list.next;
            list.next = r;
        }
        list = r;
    }

    public int nextLine(int x)
    {
        Item r= new Item(x);
        if (!isEmpty()) {
            r = list.next;
            while (r != list) {
                r = r.next;
            }
            //append last item
        }
        return r.info;
    }
    

    public String toString()
    {
        StringBuilder s = new StringBuilder("");

        if (!isEmpty()) {
            Item r = list.next;
            while (r != list) {
                s.append(r.info + ", ");
                r = r.next;
            }
            //append last item
            s.append(r.info);
        }
        return s.toString();
    }
}
1 Answers

Instead, I suggest you focus now on putting WordCount objects into the bst. Than you'll have something that will print out. So, to put WordCount objects into the bst, in pseudocode, I suggest doing:

create a new WordCount object to find. Set this to the word, count of 1, for lineNums create a new circular list object and use CL::append() to add the lineNum to it try to find this in the tree if word is found in the tree //I need to have a method here that update word count and line number <- don't worry about this bit until you've got words into the tree else // word is not found in bst, so insert it use insertBST() to insert the new WordCount object into the tree

Once you get some data into the tree, then it will print out after the wordsIn.hasNext() while loop, at the t.toString().

Related