The Artima Developer Community
Sponsored Link

Java Answers Forum
Word Frequency Help

4 replies on 1 page. Most recent reply: Apr 8, 2002 8:39 PM by Hoody

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Word Frequency Help Posted: Apr 8, 2002 12:18 PM
Reply to this message Reply
Advertisement
I had to design a program that reads a body of text
from an input file, isolates the words in the text
and writes them one per line to an output file then
put the wordlist in alphabetical order and have a
frequency count for each word in the word list.

now i had to use an array of objects so i made
another class to use it from called WordsAndCount which is this...
class WordsAndCount
{
    // instance variables - replace the example below with your own
    private String Word;
    private int Frequency;
 
    /**
     * Constructor for objects of class WordsAndCount
     */
    public WordsAndCount()
    {
        // initialise instance variables
       Word = "unknown";
       Frequency = 0;
    }
 
   WordsAndCount(String w, int f)
    {
        // initialise instance variables
        Word = w;
        Frequency = f;
    }
}
 


this is the main class that i got an error in and
cant seem to figure out whats wrong. can someone take a look and see if they can find any errors or just help me fix the program??

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.StringTokenizer;
 
/**
 * Write a description of class WordsFrequency here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class WordsFrequency
{
	public static void main(String[] args)
        throws IOException
    {
 
 
 
	    // Initializations
 
        FileReader reader = null;
        FileWriter writer = null;
 
        String inFileName = "TextIn.txt";
        String outFileName = "WordsReport.txt";
 
 
        // Open input and output files
 
   try
		{
			reader = new FileReader(inFileName);
			writer = new FileWriter(outFileName);
		}
 
   catch(FileNotFoundException e)
		{
			System.err.println("Cannot find input file ");
			System.exit(1); // abnormal termination status code
		}
 
   catch(IOException e)
		{
			System.err.println("Cannot open input/output file ");
			System.exit(2);
		}
 
	// Set up to read a line and write a line
	   BufferedReader in = new BufferedReader(reader);
	   PrintWriter out = new PrintWriter(writer);
	   WordsAndCount [] words;
	   WordsAndCount w = null;
	   words = new WordsAndCount[20];
        int wordCount = 0;
 
		for (int i = 0; i <wordCount; i++)
		{
 
						String word;
						String inputLine = in.readLine();
						
						word = in.readLine();
	            		words[i] = w;
 
 
			    // initialize a tokenizer and attach it to a line
			   boolean done = false;
			   	   while(!done)
			   	   {
			   		   if(inputLine == null)
 
			   		   done = true;
 
			   		   else
			   		   {
			   			   StringTokenizer tokenizer = new StringTokenizer(inputLine, " ");
 
			   			   while( tokenizer.hasMoreElements() )
 
			   			   out.println( (String)tokenizer.nextElement() );
			   		   }
			       }
 
	  	}
 
 
    static void insertWord(String w)
   {
    // Insert the word w into the array of words, unless it already
    // appears there.  If the word already appears in the list,
    // add 1 to the counter for that word.  The words in the array are in
    // lower case,  and w is converted to lower case before it is processed.
    // Note that the words in the array are kept in alphabetical order.
    // If the array has no more space to hold w, then it is doubled
    // in size.
 
    int pos = 0;
 
    w = w.toLowerCase();
 
    /* Find the position in the array where w belongs, after all the
	   words that precede w alphabetically.  If a copy of w already
	   occupies that position, then it is not necessary to insert
	   w, so just add 1 to the counter associated with the word
       and return.
    */
 
    while (pos < wordCount && words[pos].word.compareTo(w) < 0)
	      pos++;
		if (pos < wordCount && words[pos].word.equals(w))
		{
	      words[pos].count++;
	      return;
		}
		insertWord(in.getAlpha());
   }
 
   static void putWordList(FileWriter outFileName)
   {
   // Write the list of words from the words array, along with their
   // associated frequencies, to the output stream specified in the
   // parameter to this subroutine.  Words are output in a column
   // that is 20 spaces wide.  If an individual word is longer than
   // 15 spaces, the output won't be quite so neat.
          for (int i = 0; i < wordCount; i++)
          {
             out.print("   ");
             out.println(words[i].word);
             for (int space = words[i].word.length(); space < 20; space++)
             out.println(" ");
             out.println(" ");
             out.println(words[i].count);
          }
   }
 
   static void sortByFrequency()
   {
    // Use insertion sort to sort the words in the list so that they
    // are in order of decreasing frequency.  (Note that insertion sort
    // has the following neat property:  In a group of words that all
    // have the same frequency, the words will remain in alphabetical
    // order after the words have been sorted by frequency.)
 
            for (int i = 1; i < wordCount; i++)
            {
                WordsAndCount temp = words[i];  // Save the word in position i.
                int position;              // An index in the array.
                position = i - 1;
                while (position >= 0 && words[position].count < temp.count)
              	{
                      // Bump words that have frequencies less than the frequency
                      // of temp up one space in the array.
                   words[position + 1] = words[position];
                   position--;
            	}
                words[position + 1] = temp;  // Put temp in last vacated space.
			}
    };
 
   }
}
 
 


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Word Frequency Help Posted: Apr 8, 2002 5:12 PM
Reply to this message Reply
Without analysing your code I have a few remarks about it :
1?. There is nothing but static methods (you are therefore to my mind doing procedural code within a suppporting class).
2?. You comment a bogging / clogging the code. Use proper javacode conventions.
3?. With aproblem like your you will definitively need Factory to instanciate only once the class holding a specific word.

Should you modify your code accordingly..., I believe you would be already half way tru'

Thomas,
tsmets @ lautre . net

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Word Frequency Help Posted: Apr 8, 2002 5:29 PM
Reply to this message Reply
yea i modifified and got mostly everything working... i was just wondering if someone knows if there is a method in the java class library or somewhere that strips punctuation from a string

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Word Frequency Help Posted: Apr 8, 2002 6:05 PM
Reply to this message Reply
I came accross smthg like that not long ago on the site. It's a Tokenizer you are looking for, maybe ?

java.io.StreamTokenizer

Thomas,

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: Word Frequency Help Posted: Apr 8, 2002 8:39 PM
Reply to this message Reply
thanx for all ur help

Flat View: This topic has 4 replies on 1 page
Topic: Form onSubmit Question Previous Topic   Next Topic Topic: Why does do I see spaces?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use