So in this Author Dective program. we are reading a config file in xml. That part works and has been tested by instructor
We have most of the information printing out in main. while the words are being adding by a method we had to build, that makes words by reading the file for each char and find the whitespace to see if the word is over.
Now we are needing a file to get the Frequancy of words and that is were it is breaking.
Main()
package edu.neumont.csc110.authordetective;
import java.io.IOException;
import java.text.DecimalFormat;
public class AuthorDetective // The rest of the main does not matter for what is going wrong, if needed later I will post the whole thing. But the configReader is not used in what is going wrong
{
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
WordStream wordStream = new WordStream(configFileReader.getAnonymousTextPath());
System.out.println("\nWords from the file: ");
String Dexter = wordStream.Next();
while(!Dexter.equals(""))
{
System.out.println(Dexter);
Dexter = wordStream.Next();
}
System.out.println("Word Count: " + wordStream.wordCount);
AuthorProfile anonymousProfile = new AuthorProfile();
anonymousProfile.setAuthor("Anonymous");
anonymousProfile.addStream(wordStream);
System.out.println("Author Profie for : " + anonymousProfile.getAuthor());
System.out.println("Unique Word Count : " + anonymousProfile.getUniqueWordCount());
System.out.println("Total Word Count : " + anonymousProfile.getTotalWordCount());
System.out.println("Frequancy's" +
"\nand : " + anonymousProfile.getRelWordFreq("and") +
"\nor : " + anonymousProfile.getRelWordFreq("or") +
"\nis : " + anonymousProfile.getRelWordFreq("is") +
"\nthe : " + anonymousProfile.getRelWordFreq("the") +
"\nto : " + anonymousProfile.getRelWordFreq("to"));
}
}
WordStream -- Whole file
package edu.neumont.csc110.authordetective;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class WordStream // WordStream to get all the words from a file
{
static FileReader reader;
int wordCount;
public WordStream(String Path) // Constructor, needs a String file path to work
{
try { reader = new FileReader(Path); }
catch( FileNotFoundException e) { e.printStackTrace(); }
}
public String Next() throws IOException // Filters out the punctuation and counts and gets the words.
{
String word = "", nextWord = "";
boolean wordOver = false;
while (reader.ready() && !wordOver) // see if word is complete
{
char nextPossibleChar = (char) reader.read(); // reads in next char
while (Character.isLetterOrDigit(nextPossibleChar))
{
char nextChar = (char) Character.toLowerCase(nextPossibleChar); // changes to lower case
nextWord = nextWord + nextChar; // adds latest char to current word in progress
nextPossibleChar = (char) reader.read(); // read in the next char
if (!Character.isLetterOrDigit(nextPossibleChar) && nextWord.length() > 0)
{
wordOver = true;
wordCount++;
}
}
}
word = nextWord;
return word;
}
}
Author Profile class -- This is what is screwing up, and yes the addWord method is never used.
package edu.neumont.csc110.authordetective;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.TreeMap;
import java.util.Locale;
public class AuthorProfile // Description: This class represents the profile of an author.
{
private String author;
private TreeMap<String, Integer> wordMap;
private int totalWordCount;
private int uniqueWordCount;
DecimalFormat format = new DecimalFormat("#.#####");
public AuthorProfile() // Description: Constructor
{
wordMap = new TreeMap<String, Integer>();
reset();
}
public void setAuthor(String auth) // Gets the authors name and set its to the var author
{
author = auth;
}
public String getAuthor() // returns the authors name
{
return author;
}
public void addStream(WordStream wordStream) throws IOException // Adds a stream to the author profile by extracting each word from the
// stream, and adding it to the profile.
{
String word = wordStream.Next();
while (WordStream.reader.read() != -1)
{
while ("" != word)
{
addWord(word);
word = wordStream.Next();
}
}
}
public void addWord(String word) // Adds a word (if it does not already exist) and increments the reference count
{
// Make sure the n-gram is not empty
if ((null != word) && (0 < word.length()))
{
String lcWord = new String(word.toLowerCase(Locale.ENGLISH)); // Lower case the word
totalWordCount++; // Keep track of the total number of n-grams added
Integer wordCount = wordMap.get(lcWord); // Check to see if this word has already been added to the profile; null will be returned if the word is not found.
if (null == wordCount) // If null is returned, this word has not been added yet occurred.
{
wordMap.put(lcWord, 1);
uniqueWordCount++; // Unique word count
}
else // Otherwise increment and update the count for this word
{
wordCount++;
wordMap.put(lcWord, wordCount);
}
}
return;
}
public int getWordCount(String word) // Searches for the requested word, and returns the word count.
{
return wordMap.get(word);
}
public double getRelWordFreq(String word) // Returns the relative frequency of the passed in word.
{
double relFreq = 0;
if (0 < totalWordCount)
{
Integer count = wordMap.get(word);
if (null != count)
{
relFreq = ((double)count / (double)totalWordCount);
//relFreq = format.format(relFreq);
}
}
return relFreq;
}
public int getTotalWordCount() // Returns the total number of words that have been added to the profile.
{
return totalWordCount;
}
public int getUniqueWordCount() // Returns the number of unique words that have been added to the datastore.
{
return uniqueWordCount;
}
// Note: see http://stackoverflow.com/questions/1066589/java-iterate-through-hashmap : was referenced to make this code
public void printProfileWords() // Prints the words in the profile and their frequency count to the console.
{
for (Map.Entry<String, Integer> entry : wordMap.entrySet())
{
System.out.println(entry.getKey() + " occurs: " + entry.getValue() + " times.");
}
}
public void reset() // Constructor
{
wordMap.clear();
totalWordCount = 0;
uniqueWordCount = 0;
}
}
We are using Eclipse for this project as that is what our instructor want us to use.
When I run this code I get 0 on the relFreq print outs, and when I try to print the word I do not get any output.
But it prints from the same stream before that.
I Thank you for your time and help.

New Topic/Question
Reply




MultiQuote



|