The Artima Developer Community
Sponsored Link

Java Answers Forum
Word Frequency Data

4 replies on 1 page. Most recent reply: Mar 6, 2003 7:55 PM by Matt Gerrans

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
mike brindley

Posts: 3
Nickname: mbrindley
Registered: Mar, 2003

Word Frequency Data Posted: Mar 6, 2003 1:16 PM
Reply to this message Reply
Advertisement
I am stuck again...maybe some1 can help me again
i tried writting the public class WFreqRecord but i dont think i understand it at all....i am really stuck now..did the first bit just about. if any1 has time can someone get me started and myabe explain a little how i am meant to do this next step..i post what i have so far...and the page where the instructions are...thanx ppl!!

regards
newbie



import java.io.*;
import javax.swing.*;

import java.util.*;

public class TextHandlerImpl
{


public static void main (String[] args) throws IOException
{
JFileChooser chooser = new JFileChooser();

int status = chooser.showOpenDialog (null);

if (status != JFileChooser.APPROVE_OPTION)
{
System.out.println ("No File Chosen");
}

else
{
String fileName = chooser.getSelectedFile().getAbsolutePath();

String lines[] = getTextLines(new File(fileName));

// debug
for (int i = 0; i < lines.length; i++)
{
System.out.println("Line number " + i + " : " + lines);
}


}

}

/**
*
*/
public static String[] getTextLines(File f)
{
FileInputStream fio = null;
BufferedReader br = null;
int i = 0;

ArrayList lines = new ArrayList();

try
{
fio = new FileInputStream(f);
br = new BufferedReader(new InputStreamReader(fio));

String line = br.readLine();
while (line != null)
{

lines.add(line);

line = br.readLine();
}
}
catch(Exception e)
{
System.out.println("Got Exception = " + e);
}
finally
{
try
{
if (fio != null) fio.close();

if (br != null) br.close();
}
catch (Exception e){}
}

return (String[])lines.toArray(new String[0]);
}

public void listText(PrintWriter pwrtr, String[] text)
{

for (int i = 0; i < text.length; i++)
{
pwrtr.println(text);
}
}
}


http://www.vtr.net/~mwb/java/SYS-1A...2%20and%203.htm

source files;

http://www.vtr.net/~mwb/java//source/WFreqRecord.java

http://www.vtr.net/~mwb/java//sourc...SeqBuilder.java

http://www.vtr.net/~mwb/java//sourc...qSeqAccess.java


mike brindley

Posts: 3
Nickname: mbrindley
Registered: Mar, 2003

Re: Word Frequency Data Posted: Mar 6, 2003 1:21 PM
Reply to this message Reply
mistake...here they are

thanks again :)

source files;

http://www.vtr.net/~mwb/java/source/WFreqRecord.java

http://www.vtr.net/~mwb/java/source/WFreqSeqAccess.java

http://www.vtr.net/~mwb/java/source/WFreqSeqBuilder.java

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Word Frequency Data Posted: Mar 6, 2003 2:58 PM
Reply to this message Reply
For this task, I'd use a StreamTokenizer and a Map with a HashMap implementation. The WFreqRecord class seems unnecessary to me.

Each key in the Map would be a Strings that contains a word. The values would contain the count (or an object with more detailed information, like where it was found -- this could be used to build an indexing engine that you could sell to Google for several million dollars). You didn't say anything about case-sensitivity (do "Word" and "word" count as two different words?), but if you want case-insensitivity, you could use the lower-cased word as the key. If the entry doesn't exist, add it with a value of 1, otherwise increment its value (search this forum for "LetterFrequencyThingy" to find a very similar solution to a homework assignment that did much the same thing with individual letters).

You need to decide how words are delimited (by spaces only, or punctuation, etc.) and set up your StreamTokenizer accordingly.

By the way, it isn't good practice to throw exceptions from main(). It is better to catch the exception(s), determine the cause, deal with it, or show a useful message -- that is the point of checked exceptions.

mike brindley

Posts: 3
Nickname: mbrindley
Registered: Mar, 2003

Re: Word Frequency Data Posted: Mar 6, 2003 4:28 PM
Reply to this message Reply
hi..thanks..
hmmm, to be honest, i dont understand too well how StreamTokenizer, Map, HashMap work, i have just been reading on it and seen a few examples, but i am still totally lost...if you have time could you explain? in really lame terms, (hehehe)...i dont mean for anyone to do this work for me at all..but i have a really hard time getting started usually, i need a little push :)
sorry for being so brain dead, thanks fo the help and i will keep reading!!

thanks people!!!!!!!!!!!

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Word Frequency Data Posted: Mar 6, 2003 7:55 PM
Reply to this message Reply
Did you try searching the forum for LetterFrequencyThingy?

Flat View: This topic has 4 replies on 1 page
Topic: abstract and interface classes Previous Topic   Next Topic Topic: JAVA x WMI

Sponsored Links



Google
  Web Artima.com   

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