import shutil
import model_topN as mTopNTf
import model_topn_test as mTopN
import model_srl as mSRL
import trainWord2Vec as mWord2Vec
import convert2text as mDoc2txt
import os
import random
import math


# Global Variable declared here

raw_accept_dir = "/home/viswanath/workspace/code_garage/conver2txt/raw_data/accept"	
raw_reject_dir = "/home/viswanath/workspace/code_garage/conver2txt/raw_data/reject"
raw_predict_dir = "/home/viswanath/workspace/code_garage/conver2txt/raw_data/predict"

accept_dir = "/home/viswanath/workspace/code_garage/conver2txt/raw_text/accept"	
reject_dir = "/home/viswanath/workspace/code_garage/conver2txt/raw_text/reject"
predict_dir = "/home/viswanath/workspace/code_garage/conver2txt/raw_text/predict"

input_train = "/home/viswanath/workspace/code_garage/conver2txt/in_data/train"
input_test = "/home/viswanath/workspace/code_garage/conver2txt/in_data/test"
input_predict = "/home/viswanath/workspace/code_garage/conver2txt/in_data/predict"
input_total = "/home/viswanath/workspace/code_garage/conver2txt/in_data/total"

res_dir = '/home/viswanath/workspace/code_garage/conver2txt/out'
logs_dir = '/home/viswanath/workspace/code_garage/conver2txt/logs'

train_dirname = '/home/viswanath/workspace/test_resume/train'
test_dirname = '/home/viswanath/workspace/resume_data/res_dir/test'
predict_dirname = '/home/viswanath/workspace/test_resume/predict'
w2v_model_path = '/home/viswanath/workspace/code_garage/conver2txt/model/w2v_model_100v3.mod'


def chkWord2VecModel(modelPath):
	return os.path.isfile(modelPath) 

def chkTfidfModel(modelPath):
	return os.path.isfile(modelPath) 

def loadWord2Vec(modelPath):
	return mWord2Vec.load_W2Vec(modelPath)

def loadTfidfModel(path):
	return read_TfidfModel(path)

def genWord2Vec(model_name, input_data_dir, size, window, negative, sample, min_count, workers, iterations, out_model_file, is_phrased=False):
	return train(model_name, is_phrased, input_data_dir, size, window, negative, sample, min_count, workers, iterations, out_model_file)

def genTfidfModel(path):
	return get_tfidf_model(path)

def trainNtestData():
	print "====================::Resumae Shortlisting System::==============\n"
	print "Following algos are available for learning::\n(Enter respective Number to use)\n"
	print "1. TopN Model Amplified Word2vec vectors with TFIDF weights"
	print "2. TopN Model, Word2vec vectors with TFIDF to find topN words only"
	print "3. SRL based model using Word2Vec vectors and Logistic Regression"
	choice = raw_input("Enter your Choice")

	size = 100
	topN = 200


	if choice == "1":
		print "TopN Model Amplified Word2vec vectors with TFIDF weights Begins ...\n"
		gt = mTopNTf.genTopNVec(train_dirname,test_dirname,predict_dirname,w2v_model_path,size,topN)
		gt.start()

	elif choice == "2":
		print "TopN Model, Word2vec vectors with TFIDF to find topN words only Begins ...\n"
		gt = mTopN.genTopNVec(train_dirname,test_dirname,predict_dirname,w2v_model_path,size,topN)
		gt.start()

	elif choice == "3":
		gt = mSRL.genSRLVec(train_dirname,test_dirname,predict_dirname,w2v_model_path,size)
		gt.start()
	

	else:
		print "Sorry! I think its an invalid option :("

	
def NFoldTest():
	print "====================::Resumae Shortlisting System::==============\n"
	print "Following algos are available for learning::\n(Enter respective Number to use)\n"
	print "1. TopN Model Amplified Word2vec vectors with TFIDF weights"
	print "2. TopN Model, Word2vec vectors with TFIDF to find topN words only"
	print "3. SRL based model using Word2Vec vectors and Logistic Regression"
	choice = raw_input("Enter your Choice")

	size = 100
	topN = 200


	if choice == "1":
		print "TopN Model Amplified Word2vec vectors with TFIDF weights Begins ...\n"
		gt = mTopNTf.genTopNVec(train_dirname,test_dirname,predict_dirname,w2v_model_path,size,topN)
		gt.NFoldTest(iter_N=10,split =0.27)

	elif choice == "2":
		print "TopN Model, Word2vec vectors with TFIDF to find topN words only Begins ...\n"
		gt = mTopN.genTopNVec(train_dirname,test_dirname,predict_dirname,w2v_model_path,size,topN)
		gt.NFoldTest(iter_N=10,split =0.27)

	elif choice == "3":
		gt = mSRL.genSRLVec(train_dirname,test_dirname,predict_dirname,w2v_model_path,size)
		gt.NFoldTest(iter_N=10,split =0.27)
	

	else:
		print "Sorry! I think its an invalid option :("


def predictData():
	print "====================::Resumae Shortlisting System::==============\n"
	print "Following algos are available for learning::\n(Enter respective Number to use)\n"
	print "1. TopN Model Amplified Word2vec vectors with TFIDF weights"
	print "2. TopN Model, Word2vec vectors with TFIDF to find topN words only"
	print "3. SRL based model using Word2Vec vectors and Logistic Regression"
	choice = raw_input("Enter your Choice")

	size = 100
	topN = 200


	if choice == "1":
		print "TopN Model Amplified Word2vec vectors with TFIDF weights Begins ...\n"
		gt = mTopNTf.genTopNVec(train_dirname,test_dirname,predict_dirname,w2v_model_path,size,topN)
		gt.train_predict()

	elif choice == "2":
		print "TopN Model, Word2vec vectors with TFIDF to find topN words only Begins ...\n"
		gt = mTopN.genTopNVec(train_dirname,test_dirname,predict_dirname,w2v_model_path,size,topN)
		gt.train_predict()

	elif choice == "3":
		gt = mSRL.genSRLVec(train_dirname,test_dirname,predict_dirname,w2v_model_path,size)
		gt.start()		
	

	else:
		print "Sorry! I think its an invalid option :("


def trainNtestDataRaw():
	return

def NFoldTestRaw():
	return

def predictDataRaw():
	return

def convertRawFiles2Text():
	mDoc2txt.convertDirFiles(raw_accept_dir, accept_dir)
	mDoc2txt.convertDirFiles(raw_reject_dir, reject_dir)
	mDoc2txt.convertDirFiles(raw_predict_dir, predict_dir)


def loadFiles2Input(split=0.33):

	trainExList = listFIles(input_train)
	testExList = listFIles(input_test)
	acceptExList = listFIles(accept_dir)
	rejectExList = listFIles(reject_dir)

	numTr = len(trainExList) #= listFIles(input_train)
	numTst = len(testExList) #= listFIles(input_test)
	numAcpt = len(acceptExList) #= listFIles(accept_dir)
	numRej = len(rejectExList) #= listFIles(reject_dir)

	numTstAcp = int(math.ceil(split*numAcpt))
	numTrAcp = numAcpt - numTstAcp	

	numTstRej = int(math.ceil(split*numRej))
	numTrRej = numRej - numTstRej	

	diffTrAcp = numTrAcp - len(set(acceptExList) & set(trainExList))
	diffTstAcp = numTstAcp - len(set(acceptExList) & set(testExList))

	diffTrRej = numTrRej - len(set(rejectExList) & set(trainExList))
	diffTstRej = numTstRej - len(set(rejectExList) & set(testExList))

	acceptExList = list(set(acceptExList) - set(trainExList))
	acceptExList = list(set(acceptExList) - set(testExList))

	rejectExList = list(set(rejectExList) - set(trainExList))
	rejectExList = list(set(rejectExList) - set(testExList))


	if diffTrAcp>0:		
		TrAcpFile = set(random.sample(acceptExList,diffTrAcp))
		acceptExList = list(set(acceptExList) - set(TrAcpFile))
		for f in TrAcpFile:

			moveData(os.path.join(accept_dir,f),input_train)
			moveData(os.path.join(accept_dir,f),input_total)


	if diffTstAcp>0:		
		TstAcpFile = set(random.sample(acceptExList,diffTstAcp))
		for f in TstAcpFile:

			moveData(os.path.join(accept_dir,f),input_test)
			moveData(os.path.join(accept_dir,f),input_total)


	if diffTrRej>0:		
		TrRejFile = set(random.sample(rejectExList,diffTrRej))
		rejectExList = list(set(rejectExList) - set(TrRejFile))
		for f in TrRejFile:

			moveData(os.path.join(reject_dir,f),input_train)
			moveData(os.path.join(reject_dir,f),input_total)


	if diffTstRej>0:		
		TstRejFile = set(random.sample(rejectExList,diffTstRej))
		for f in TstRejFile:

			moveData(os.path.join(reject_dir,f),input_test)
			moveData(os.path.join(reject_dir,f),input_total)


	predictList = listFIles(predict_dir)
	predictExList = listFIles(input_predict)

	predictList = set(predictList) - set(predictExList)

	for f in predictList:

		moveData(os.path.join(predict_dir,f),input_predict)
		moveData(os.path.join(predict_dir,f),input_total)




def listFIles(dirname):
	files = [f for f in os.listdir(dirname) if os.path.isfile(os.path.join(dirname, f))]
	return files

def moveData(src,dest):
	shutil.copy(src,dest)

def clearDir(in_dir):

	for f in os.listdir(in_dir):
        
		f = os.path.join(in_dir, f)
		if os.path.isfile(f):
			os.remove(f)

	print "All files successfully deleted... "


def cleanShuffle(split=0.20):

	clearDir(input_train)
	clearDir(input_test)
	clearDir(input_total)
	clearDir(input_predict)

	acceptExList = listFIles(accept_dir)
	rejectExList = listFIles(reject_dir)

	numAcpt = len(acceptExList) #= listFIles(accept_dir)
	numRej = len(rejectExList) #= listFIles(reject_dir)

	numTstAcp = int(math.ceil(split*numAcpt))
	numTrAcp = numAcpt - numTstAcp	

	numTstRej = int(math.ceil(split*numRej))
	numTrRej = numRej - numTstRej	

	if numTrAcp>0:		

		TrAcpFile = set(random.sample(acceptExList,numTrAcp))
		acceptExList = list(set(acceptExList) - set(TrAcpFile))
		for f in TrAcpFile:
			moveData(os.path.join(accept_dir,f),input_train)
			moveData(os.path.join(accept_dir,f),input_total)


	if numTstAcp>0:		

		TstAcpFile = set(random.sample(acceptExList,numTstAcp))
		for f in TstAcpFile:
			moveData(os.path.join(accept_dir,f),input_test)
			moveData(os.path.join(accept_dir,f),input_total)


	if numTrRej>0:		

		TrRejFile = set(random.sample(rejectExList,numTrRej))
		rejectExList = list(set(rejectExList) - set(TrRejFile))
		for f in TrRejFile:
			moveData(os.path.join(reject_dir,f),input_train)
			moveData(os.path.join(reject_dir,f),input_total)


	if numTstRej>0:		
		TstRejFile = set(random.sample(rejectExList,numTstRej))
		for f in TstRejFile:
			moveData(os.path.join(reject_dir,f),input_test)
			moveData(os.path.join(reject_dir,f),input_total)

	predictList = listFIles(predict_dir)

	for f in predictList:
		moveData(os.path.join(predict_dir,f),input_predict)
		moveData(os.path.join(predict_dir,f),input_total)

	
	return

if __name__ == '__main__':
#	convertRawFiles2Text()
#	cleanShuffle()
	trainNtestData()