3 Replies - 305 Views - Last Post: 15 November 2012 - 11:15 AM Rate Topic: -----

#1 Drive137  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-February 11

File Reader issues, Author Dective

Posted 14 November 2012 - 09:55 PM

Hello, I have been looking stuff up here and attempting to help others if I could without confusing people. But I am stuck in my java Class, I am not asking for this to be done for me. I just want a syntax that would work or help with errors that I am not seeing. and possible understanding why mine does not work but what is suggested does so I can get my error and learn from it.

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.

Is This A Good Question/Topic? 0
  • +

Replies To: File Reader issues, Author Dective

#2 JHol  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 10-February 11

Re: File Reader issues, Author Dective

Posted 15 November 2012 - 06:28 AM

When your reader have finsihed reading the file the first time, its pointing to the end of the file. in your reader you lack the ability to move the pointer to the first position in the file.

You should at some point close the file.

when reading the file the second time()

you could do this:
anonymousProfile.addStream(new wordstream("example.txt"));



hope this helps you in the right direction
Was This Post Helpful? 0
  • +
  • -

#3 Drive137  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-February 11

Re: File Reader issues, Author Dective

Posted 15 November 2012 - 07:23 AM

that still does not work, even with creating a while new WordStream, its then not terminating and not even getting to were it should print the author
Was This Post Helpful? 0
  • +
  • -

#4 JHol  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 10-February 11

Re: File Reader issues, Author Dective

Posted 15 November 2012 - 11:15 AM

Let's try again!

please don't create a while(new WordStream), it would probably hang.
you would only need to do it once.


i have changed parameter in new wordstream to test this on my computer.

in the main method of AutohorDetective. you should create a new WordStream in anonymousProfile.addStream.

you should also close the reader, since the reader might keep a lock on your file. closing it and creating a new might help.


public static void main(String[] args) throws IOException 
	{
		

		wordstream wordStream = new wordstream("c:\\AndroidManifest.xml");
		
		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);
		[b]wordStream.reader.close();[/b]

		AuthorProfile anonymousProfile = new AuthorProfile();
		
		anonymousProfile.setAuthor("Anonymous");
		
		[b]anonymousProfile.addStream(new WordStream("c:\\AndroidManifest.xml"));[/b]
		
		
		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"));
			
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1