import sys
import math
import csv
from AbstractLearner import AbstractLearner

class SimpleLearner(AbstractLearner):
	"""
	Derived class implementation of AbstractLearner. This class implements the learn() 
	and classify() functions using a simple approach to classify all as true or false
	"""

	def __init__(self, trainingInputFile, testInputFile, isMachineReadable, outputFile):
		AbstractLearner.__init__(self, trainingInputFile, testInputFile, 
			isMachineReadable, outputFile)
		
		#global variable for classification of data, default false
		self.classification = False


	def learn(self):
		"""
		Creates a classification model based on data held in the AbstractLearner's 
		trainingData list-of-lists
		"""
		numRows = len(self.trainingData)
		numTrue = 0
		numFalse = 0
		#get number of true and false elements in the data
		for i in range(numRows):
			if self.trainingData[i][-1]: 
				numTrue += 1
			else:
				numFalse += 1
		#if there are more trues, set classification to true, otherwise it will be false
		if numTrue >= numFalse:
			self.classification = True

	def classify(self):
		"""
		Based on the classification model generated by learn(), this function will read from
		the testData list-of-lists in AbstractLearner and output the prediction for each 
		variable
		"""
		#create a csv writer for output
		myWriter = csv.writer(open(self.outputFile, "wb"))
		#if classification is true
		if self.classification:
			numRows = len(self.testData)
			#write index of all rows, since all are true
			for i in range(numRows):
				myWriter.writerow([i])
		sys.exit(0)
