package com.oswad.init;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.oswad.init.WordMap;

/**
 * Classifier for Wordpress Content
 *
 */
public class Classifier implements Runnable
{
    private String spamCorpusPath;
    private String nonSpamCorpusPath;
	private WordMap spamMap;
    private WordMap nonSpamMap;
    private Map<String,Integer> documentCountMap = new HashMap<String,Integer>();
    private Map<String,Double> spamProbability;
    
	private Thread classifierThread;
    boolean start = false;
    boolean isSpam = false;
	
    public Classifier(String spamCorpusPath, String nonSpamCorpusPath){
    	this.spamCorpusPath = spamCorpusPath;
    	this.nonSpamCorpusPath = nonSpamCorpusPath;
    	classifierThread = new Thread(this,"ClassifierThread");
    	classifierThread.start();
    	start = true;
    }


	@Override
	public void run() {
		String fileName = null;
		spamMap = train(spamCorpusPath);		
		nonSpamMap = train(nonSpamCorpusPath);

		documentCountMap.put("Good", nonSpamMap.getFileCount());
		documentCountMap.put("Spam", spamMap.getFileCount());		
		biasNonSpam(nonSpamMap);
		
		while (start) {
			System.out.print("Enter Filename of content to test: ");
			BufferedReader fileReader = new BufferedReader(
					new InputStreamReader(System.in));
			try {
				fileName = fileReader.readLine();
				if(fileName.equals("q") || fileName.equals("Q")){
					System.out.println("Exiting Classifier");
					start = false;
					continue;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			isSpam = classify(fileName);
			if(isSpam){
				System.out.println("Content is spam");
			}
			else{
				System.out.println("Content is non-spam");
			}
		}
	}
	
	
	/** Bias the map of Non spam word counts to double of what actual 
	 * count is.
	 * @param nonSpamMap2
	 */
	private void biasNonSpam(WordMap nonSpamMap) {
		for(Map.Entry<String, Integer> k : nonSpamMap.entrySet()){
			k.setValue(k.getValue()*2);
		}

	}


	private WordMap train(String pathToCorpus) {
		return new WordMap(pathToCorpus);
	}


	private boolean classify(String fileName) {
	
		return false;
	}
}

@@ -311,16 +311,18 @@
 nnable%0A%7B
+%09%0A
 %0A    pri
@@ -405,36 +405,35 @@
 ordMap spamMap;%0A
-    
+%0A%0A%09
 private WordMap 
@@ -574,16 +574,96 @@
 bility;%0A
+    private final String good = %22Good%22;%0A    private final String spam = %22Spam%22;%0A
     %0A%09pr
@@ -1193,22 +1193,20 @@
 Map.put(
-%22G
+g
 ood
-%22
 , nonSpa
@@ -1250,22 +1250,20 @@
 Map.put(
-%22S
+s
 pam
-%22
 , spamMa
@@ -2282,25 +2282,965 @@
 s);%0A%09%7D%0A%0A
-%0A%09private
+%09/**%0A%09 * Given a category, what is the probability with which %0A%09 * a word can appear in it%0A%09 */%0A%09public double wordProbability(String word, String category)%7B%0A%09%09int catCount = 1;%0A%09%09int wordCount = 1;%0A%09%09catCount = documentCountMap.get(category);%0A%09%09if(category.equals(good))%7B%0A%09%09%09wordCount = getNonSpamMap().get(word);%0A%09%09%7D%0A%09%09else if(category.equals(spam))%7B%0A%09%09%09wordCount = getSpamMap().get(word);%0A%09%09%7D%0A%09%09%0A%09%09return Math.min(wordCount/catCount,1);%0A%09%09%0A%09%7D%0A%09%0A%09public double weightedProbability(String word, String category)%7B%0A%09%09int weight = 1;%0A%09%09double assumedProb = 0.5;%0A%09%09double wordProbability= wordProbability(word,category);%0A%09%09%0A%09%09int totalCount = getSpamMap().get(word)+getNonSpamMap().get(word);%0A%09%09%0A%09%09double weightedProb = ((totalCount*wordProbability) + (weight*assumedProb))/(weight+totalCount);%09%0A%09%09return weightedProb;%0A%09%09%0A%09%7D%0A%09%0A    public WordMap getSpamMap() %7B%0A%09%09return spamMap;%0A%09%7D%0A%0A    public WordMap getNonSpamMap() %7B%0A%09%09return nonSpamMap;%0A%09%7D%0A%0A%09public
  boolean
@@ -3289,10 +3289,13 @@
 lse;%0A%09%7D%0A
+%09%0A%0A
 %7D%0A

Add methods to compute probability of word appearing as spam or non-spam and also the weighted probability to deal with initial stages when the counts will be low.