package ca.ubc.ca.ocrreader;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Context {

	private Map<String, Integer> words;

	// private long timestamp;

	public Context(String text) {
		words = new HashMap<String, Integer>();
		parseText(text);
	}

	/*
	 * Given a string, store all words and their frequency in the words field.
	 * Omits punctuation, spaces, and the empty string, but includes digits.
	 */
	private void parseText(String text) {
		String word = "";
		
		for (int i = 0; i < text.length(); i++) {
			Character ch = text.charAt(i);
			if (Character.isLetterOrDigit(ch)) {
				word += ch;
			}
			if (!Character.isLetterOrDigit(ch) || i >= (text.length() - 1)) {
				// skip empty string, otherwise add the word to words map
				if (!word.equals("")) {
					addWord(word);
					word = "";
				}
			}
		}
	}

	/*
	 * Helper method for parseImgText
	 */
	private void addWord(String word) {
		int freq;
		if (words.containsKey(word)) {
			freq = words.get(word) + 1;
		} else {
			freq = 1;
		}
		words.put(word, freq);
	}

	/*
	 * Given another context to compare to, return a set of all words common to
	 * both contexts
	 */
	public Set<String> getSharedWords(Context other) {
		Set<String> sharedWords = new HashSet<String>();
		Set<String> otherWords = other.getWords().keySet();
		for (String word : otherWords) {
			if (words.containsKey(word)) {
				sharedWords.add(word);
			}
		}
		return sharedWords;
	}

	/*
	 * Return a set of all words that appear only in this context (i.e., are not
	 * present in the other, given context)
	 */
	public Set<String> getUniqueWords(Context other) {
		Set<String> uniqueWords = words.keySet();
		Set<String> otherWords = other.getWords().keySet();
		for (String word : otherWords) {
			if (uniqueWords.contains(word)) {
				uniqueWords.remove(word);
			}
		}
		return uniqueWords;
	}

	public Map<String, Integer> getWords() {
		return words;
	}
}

@@ -157,96 +157,331 @@
 ate 
-Map%3CString, Integer%3E words;%0A%0A%09// private long timestamp;%0A%0A%09public Context(String text) %7B
+String name;%0A%09private long timestamp;%0A%09private Map%3CString, Integer%3E words;%0A%0A%09public Context(String text) %7B%0A%09%09this.name = null;%0A%09%09this.timestamp = 0;%0A%09%09words = new HashMap%3CString, Integer%3E();%0A%09%09parseText(text);%0A%09%7D%0A%0A%09public Context(String name, long timestamp, String text) %7B%0A%09%09this.name = name;%0A%09%09this.timestamp = timestamp;
 %0A%09%09w

Add name and timestamp fields to Context and its constructor