import xgboost as xb
import face_recognition
import os
from os import listdir
from os.path import isfile, join
import sys
import time
import cv2
import numpy
import array
import glob
import errno
import pickle



def getReferences(dir):
    subdirs=listdir(dir) 
    names=[];
    emb=[]
    for subdir in (subdirs):
        name=subdir
        subdir=join(dir,subdir)
        txtFile = open(subdir + '/' + name + '.txt', 'r')
        text=txtFile.read()
        text=text.replace("]", "")
        text=text.replace("[", "")
        result=text.split()
        f=[]
        for n in range(0,128):
            f.append(float(result[n]))
        features=numpy.array(f)
        emb.append(features)
        names.append(name)
        txtFile.close()
    return emb, names

def main(dir,distance):
    arrayOf0 = []
    txtData= open('./symptoms.txt', 'r')
    text2=txtData.read()
    text2=text2.replace("]", " ")
    text2=text2.replace("[", "[ ")
    result2=text2.split()    
    first=True
    idx=0;
    for i,r in enumerate(result2):
        if (r=='['):
            f=[]
            idx=idx+1
            for n in range(i+1,i+129):
                f.append(float(result2[n]))
            if (first):
                arrayOf0.append(0)
                features2 = numpy.array(f)
                first=False
            else:
                features2 = numpy.vstack([features2,f])
                arrayOf0.append(0)  
            if len(arrayOf0)%100==0:
                print (len(arrayOf0))
    subdirs=listdir(dir)  
    resultFeatures = []
    for subdir in (subdirs):
        name=subdir
        subdir=join(dir,subdir)
        arrayOf1 = []
        arrayOfData = []
        files=[f for f in listdir(subdir) if isfile(join(subdir,f))]
        txtFile = open(subdir + '/' + 'ModifiedCalculs.txt', 'r')
        text=txtFile.read()
        text=text.replace("]", " ")
        text=text.replace("[", "[ ")
        result=text.split()
        first=True
        idx=0;
        for i,r in enumerate(result):
            if (r=='['):
                f=[]
                idx=idx+1
                for n in range(i+1,i+129):
                    f.append(float(result[n]))
                if (first):
                    arrayOf1.append(1)
                    features = numpy.array(f)
                    first=False
                else:
                    features = numpy.vstack([features,f])
                    arrayOf1.append(1)       

        print(len(arrayOf0))
        print(len(arrayOf1))
        arrayOf0and1Together = arrayOf1 + arrayOf0
        print(len(arrayOf0and1Together))
        featuresTogether = numpy.concatenate((features, features2), axis=0)
        resultFeatures = numpy.array(featuresTogether)
        print(len(resultFeatures))
        print(resultFeatures)
        resultFeatures.shape
        resultArrayOf0And1 = numpy.array(arrayOf0and1Together)
        resultArrayOf0And1.shape
        gbm = xb.XGBClassifier(max_depth=3, learning_rate = 0.05).fit(resultFeatures, resultArrayOf0And1)
        pickle.dump(gbm, open("xgboost_results_"+name+".txt", "wb"))

if __name__=="__main__":
	if len(sys.argv) > 1:
		path = sys.argv[1]
	else:
		path = "./database2/"
	sys.exit(int(main(path,0.04)) or 0)