7 Replies - 5905 Views - Last Post: 23 November 2013 - 03:26 PM Rate Topic: -----

#1 idontkn0w   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 05-April 13

Counting unique words in a file using a sorted array

Posted 23 November 2013 - 01:28 PM

How do I go about counting unique words in "mobydick.txt" using a sorted array?

These are the classes + txt file I'm using (part 1C of countWords class):

import java.util.ArrayList;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Iterator;

// This is just a "helper" file. Feel free to read it if you are curious,
// but you will not need to change it in any way!

public class WordStream implements Iterable<String> {

    ArrayList<String> words;
    int position;

    public WordStream(String filename) throws FileNotFoundException {
	position = 0;
	words = new ArrayList<String>();
	Scanner scanner  = new Scanner(new FileReader(filename));
	while (scanner.hasNext()) {
	    String word = scanner.next();
	    words.add(word);
	}
    }

    public String nextWord() {
	if (position < words.size()) {
	    position++;
	    return words.get(position-1);
	}
	else 
	    return null;
    }

    public boolean hasNextWord() {
	return (position < words.size());
    }

    public void reset() {
	position=0;
    }

    public Iterator<String> iterator() {
	return new WSIterator();
    }
    
    private class WSIterator implements Iterator<String> {
	private int mypos;

	public WSIterator() {
	    mypos=0;
	}

	public boolean hasNext() {
	    return (mypos < words.size());
	}

	public String next() {
	    String word = words.get(mypos);
	    mypos++;
	    return word;
	}

	public void remove() {
	    throw new UnsupportedOperationException();
	}
    }
}



import java.io.FileNotFoundException;
//import java.util.Scanner;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.ArrayList;

public class countWords {

	public static int countUnique1A(WordStream words) {
		HashSet<String> wordList = new HashSet<String>();
		for (String word : words) {
			if (!wordList.contains(word)) {
				wordList.add(word);
			}
		}
		return wordList.size(); // Change this
	}

	public static int countUnique1B(WordStream words) {
		TreeSet<String> ts = new TreeSet<String>();
		for (String word : words) {
			if (!ts.contains(word)) {
				ts.add(word);
			}
		}
		return ts.size(); // Change this
	}

	public static int countUnique1C(WordStream words) {
		// Put code for question 1C here: Count unique words using a sorted
		// array.
		// You will find the ArrayList class useful, but you will have to write
		// some extra functions of your own.

		return 0; // Change this
	}

	public static int countUnique1D(WordStream words) {
		ArrayList<String> wordList = new ArrayList<String>();
		for (String word : words) {
			if (!wordList.contains(word)) {
				wordList.add(word);
			}
		}
		return wordList.size();
	}

	// Example function: Summing up the length of all words in the file
	public static int countlength(WordStream words) {
		int length = 0;
		for (String word : words) {
			length = length + word.length();
		}
		return length;
	}

	public static void main(String[] args) throws FileNotFoundException {
		WordStream words = new WordStream("mobydick.txt");
		long starttime = System.nanoTime();
		int length = countlength(words);
		long elapsedtime = System.nanoTime() - starttime;
		System.out.println("Counted total length " + length + " in "
				+ elapsedtime + " nanoseconds.");
		// Calling 1a
		starttime = System.nanoTime();
		length = countUnique1A(words);
		elapsedtime = System.nanoTime() - starttime;
		System.out.println("Function 1A found " + length + " unique words in "
				+ elapsedtime + " nanoseconds.");
		// Calling 1b
		starttime = System.nanoTime();
		length = countUnique1B(words);
		elapsedtime = System.nanoTime() - starttime;
		System.out.println("Function 1B found " + length + " unique words in "
				+ elapsedtime + " nanoseconds.");
		// Calling 1c
		starttime = System.nanoTime();
		length = countUnique1C(words);
		elapsedtime = System.nanoTime() - starttime;
		System.out.println("Function 1C found " + length + " unique words in "
				+ elapsedtime + " nanoseconds.");
		// Calling 1d
		starttime = System.nanoTime();
		length = countUnique1D(words);
		elapsedtime = System.nanoTime() - starttime;
		System.out.println("Function 1D found " + length + " unique words in "
				+ elapsedtime + " nanoseconds.");
	}

}



mobydick.txt
Spoiler