Results 1 to 3 of 3
Thread: Word Frequency
- 10-09-2008, 05:09 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 1
- Rep Power
- 0
Word Frequency
Hello Everyone,
I am trying to create an arraylist of object that reads the word and computes the words frequency from the input file. The problem that I am having is that when the file that is being read and has multiple lines something goes wrong. I think it creates another arraylist and it seems that it has something to do with how the file is read and how it process it. I also attached the text file. Any advise?
Thanks so much...
public class WordsFrequency
{
public static void main(String[] args)
{
//Set up a Scanner object to read from the console to get
//input and out file names
Scanner console = new Scanner(System.in);
System.out.print("Enter the input file name: ");
String inputFileName = console.next();
System.out.print("Enter the output file name: ");
String outputFileName = console.next();
// Open input and output files
try
{
//Construct a FileReader object to read from the disk
FileReader reader = new FileReader(inputFileName);
//Use FileReader to set up a Scanner object to read from the file
Scanner in = new Scanner(reader);
PrintWriter out = new PrintWriter(outputFileName);
while(in.hasNextLine())
{
String inputLine = in.nextLine();
StringTokenizer tokenizer = new StringTokenizer(inputLine);
WordList myWords = new WordList();
while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
token = token.toLowerCase();
token.replaceAll("[?!\"(),.;:]","");
// boolean found = false;
int num1 =0;
int x = myWords.search(token); //myWords.getFreq()
if (x > -1) //found
{
Word word2 = new Word(token, myWords.getFreq(x)+1);
myWords.updateFreq(x,word2);
//System.out.println(word2);
}
else // not found
{
Word word1 = new Word(token,1);
myWords.setWord(word1);
//System.out.println(word1);
}
}
myWords.bubbleSort();
for(int k = 0; k < myWords.getSize(); k++)
{
out.print(myWords.getWord(k) + " " + myWords.getFreq(k));
out.printf("%6.2f",myWords.getPercent(k));
out.println("%");
}
}
//close files
in.close();
out.close();
}
// end try
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);
}
}
public static class WordList
{
private ArrayList <Word> words;
private int totalcount;
public WordList() // Default Constructor
{
words = new ArrayList <Word>();
totalcount = 0;
}
public void setWord(Word object1)
{
words.add(object1);
}
public String getWord(int num)
{
return words.get(num).getWord();
}
public int getFreq(int num) //num = place in ArrayList
{
return words.get(num).getFrequency();
}
public Word getObject(int num)
{
return words.get(num);
}
public void updateFreq(int num1, Word word1)
{
words.set(num1,word1);
}
public int getSize()
{
return words.size();
}
public int search(String pass)// sequential search method
{
for (int i =0; i<words.size(); i++)
{
if (words.get(i).getWord().equals(pass))
return i;
}
return -1;
}
public void bubbleSort()
{
for (int x = 0; x < words.size() - 1; x++)
{
for (int y = x + 1; y < words.size(); y++)
{
if (words.get(y).getFrequency() > words.get(x).getFrequency())
{
Word temp = words.get(x);
words.set(x,words.get(y));
words.set(y,temp);
}
}
}
}
public double getTotal()
{
double sum = 0.0;
for (int i =0; i < words.size(); i++)
{
sum = sum + words.get(i).getFrequency();
}
return sum;
}
public Double getPercent(int num)
{
return words.get(num).getFrequency()*100.00 / getTotal();
}
public String toString (int index)
{
return getWord(index) + getFreq(index) ;
}
}
public static class Word
{
private String word;
private int count;
public Word(String newWord, int freq)
{
word = newWord;
count = freq;
}
public String getWord()
{
return word;
}
public int getFrequency()
{
return count;
}
public int increment()
{
return count++;
}
public String toString()
{
return getWord() + getFrequency();
}
}
}
- 10-09-2008, 06:11 AM #2
Break your code into some subroutines.
Have your main loop be simple, and put the functions in the subroutines.
Pseudo code would look something like:
Java Code:String inLine = null; while ((inLine = fileReader.readLine() ) != null) { processLine(inLine); } //then define some suitable implementation for processLine public void processLine ( String inLine) { // ... }
- 10-09-2008, 02:03 PM #3
Similar Threads
-
Word to xml Conversion
By kushagra in forum Advanced JavaReplies: 3Last Post: 10-16-2008, 08:23 AM -
Java ME beep(frequency, time)
By Morfmor in forum New To JavaReplies: 2Last Post: 08-28-2008, 10:34 PM -
Word OLE
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:33 PM -
Frequency Counter
By justlearning in forum New To JavaReplies: 0Last Post: 05-07-2008, 10:50 PM -
[SOLVED] BST Frequency Counter
By theonly in forum Advanced JavaReplies: 7Last Post: 04-29-2008, 11:33 PM


LinkBack URL
About LinkBacks

Bookmarks