<commit_before>import java.util.HashMap;
import java.io.Serializable;

public class Classifier implements Serializable{
	
	private static final long serialVersionUID = 1L;
	private HashMap<String, HashMap<String, Integer>> wordCountsByClass;
	private HashMap<String, Integer> wordCountsTotal;
	private double alpha = 1.0;
	
	public Classifier() {
		wordCountsByClass = new HashMap<String, HashMap<String, Integer>>();
		wordCountsTotal = new HashMap<String, Integer>();
	}
	
	public void train(String nbcClass, HashMap<String, Integer> bag) {
		if (!wordCountsByClass.containsKey(nbcClass))
			wordCountsByClass.put(nbcClass, new HashMap<String, Integer>());
		
		if (!wordCountsTotal.containsKey(nbcClass))
			wordCountsTotal.put(nbcClass, 0);
		
		for (String word : bag.keySet()) {
			if (wordCountsByClass.get(nbcClass).containsKey(word)) {
				int oldCount = wordCountsByClass.get(nbcClass).get(word);
				wordCountsByClass.get(nbcClass).put(word, oldCount + bag.get(word));
			} else {
				wordCountsByClass.get(nbcClass).put(word, bag.get(word));
			}		
			
			int oldCount = wordCountsTotal.get(nbcClass);
			wordCountsTotal.put(nbcClass, oldCount + bag.get(word));
		}
	}
	
	public String test(HashMap<String, Integer> bag) {
		String maximumClass = null;
		double maximumValue = Double.NEGATIVE_INFINITY;
		
		for (String nbcClass : wordCountsByClass.keySet()) {
			double value = getClassPosterior(nbcClass, bag);
			if (maximumValue == 0) {
				maximumValue = value;
				maximumClass = nbcClass;
			} else if (value < maximumValue) {
				maximumValue = value;
				maximumClass = nbcClass;
			}
		}
		
		return maximumClass;
	}
	
	/**
	 * Calculates the word likelihood using the formula in paper:
	 * (nci + alpha ) / (nc + alphaSum)
	 * where: nci = number of times word i appears in the documents in class c
	 * nc =  total number of word occurrences in class c
	 * alpha = the smoothing constant, imagined occurrences 
	 * alphaSum = the constant, over all words in class c (alpha * number of keys in class)
	 * @param nbcClass	The class we are computing likelihood over
	 * @param word		The word we are calculating likelihood of
	 * @return			The Pr( word | class )
	 */
	private double getWordLikelihood(String nbcClass, String word) {	
		double alphaSum = wordCountsByClass.get(nbcClass).size() * alpha;
		double nc = wordCountsTotal.get(nbcClass);
		double nci;
		if (wordCountsByClass.get(nbcClass).containsKey(word))
			nci = wordCountsByClass.get(nbcClass).get(word);
		else
			nci = 0;
		return (nci + alpha) / (nc + alphaSum);<commit_msg>fixed if(maximumValue == 0) statement<commit_after>import java.util.HashMap;
import java.io.Serializable;

public class Classifier implements Serializable{
	
	private static final long serialVersionUID = 1L;
	private HashMap<String, HashMap<String, Integer>> wordCountsByClass;
	private HashMap<String, Integer> wordCountsTotal;
	private double alpha = 1.0;
	
	public Classifier() {
		wordCountsByClass = new HashMap<String, HashMap<String, Integer>>();
		wordCountsTotal = new HashMap<String, Integer>();
	}
	
	public void train(String nbcClass, HashMap<String, Integer> bag) {
		if (!wordCountsByClass.containsKey(nbcClass))
			wordCountsByClass.put(nbcClass, new HashMap<String, Integer>());
		
		if (!wordCountsTotal.containsKey(nbcClass))
			wordCountsTotal.put(nbcClass, 0);
		
		for (String word : bag.keySet()) {
			if (wordCountsByClass.get(nbcClass).containsKey(word)) {
				int oldCount = wordCountsByClass.get(nbcClass).get(word);
				wordCountsByClass.get(nbcClass).put(word, oldCount + bag.get(word));
			} else {
				wordCountsByClass.get(nbcClass).put(word, bag.get(word));
			}		
			
			int oldCount = wordCountsTotal.get(nbcClass);
			wordCountsTotal.put(nbcClass, oldCount + bag.get(word));
		}
	}
	
	public String test(HashMap<String, Integer> bag) {
		String maximumClass = null;
		double maximumValue = Double.NEGATIVE_INFINITY;
		
		for (String nbcClass : wordCountsByClass.keySet()) {
			double value = getClassPosterior(nbcClass, bag);
			if (value < maximumValue) {
				maximumValue = value;
				maximumClass = nbcClass;
			}
		}
		
		return maximumClass;
	}
	
	/**
	 * Calculates the word likelihood using the formula in paper:
	 * (nci + alpha ) / (nc + alphaSum)
	 * where: nci = number of times word i appears in the documents in class c
	 * nc =  total number of word occurrences in class c
	 * alpha = the smoothing constant, imagined occurrences 
	 * alphaSum = the constant, over all words in class c (alpha * number of keys in class)
	 * @param nbcClass	The class we are computing likelihood over
	 * @param word		The word we are calculating likelihood of
	 * @return			The Pr( word | class )
	 */
	private double getWordLikelihood(String nbcClass, String word) {	
		double alphaSum = wordCountsByClass.get(nbcClass).size() * alpha;
		double nc = wordCountsTotal.get(nbcClass);
		double nci;
		if (wordCountsByClass.get(nbcClass).containsKey(word))
			nci = wordCountsByClass.get(nbcClass).get(word);
		else
			nci = 0;
		return (nci + alpha) / (nc + alphaSum);
{'fraction_non_alphanumeric': 0.10755239224990115, 'fraction_numerical': 0.002372479240806643, 'mean_word_length': 4.2533748701973, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2715233', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1598, 'n_words': 434}