#!/usr/bin/env python
#
# 
#
import math
from operator import itemgetter


Data={}
Keys= {}
entropies= {}
entropies2= {}
LetterValues=[]
TargetValues=[]
numGroup=0


def entropyCalc(x,y):
    x= x/1.0
    y= y/1.0
    total= x+y
    if x>0 and y>0:
        prob1= x/total
        prob2= y/total
        ent= -prob1*math.log(prob1,2)-prob2*math.log(prob2,2)
        return ent
    else:
        return 100


    
def filein(name):
    file = open(name,"r")
    text = file.readlines()
    file.close()
    i = 0
    for x in text:
        if x[len(x)-1:] == "\n":
            text[i] = x[:len(x)-1]
        if i == 0:
            text[i] = text[i].split(" ")
        else:
            text[i] = text[i].split("  ")
        i += 1
    length = len(text)-1
    i = 0
    parsed = {}
    for x in text:
        w = 0
        for z in x:
            if i == 0:
                parsed[z]=[]
            else:
                topics = text[0]
                parsed[topics[w]].append(z)
                w +=1
        i += 1
    return parsed

fileName= raw_input("What is the name containing the data you wish to make a tree out of? ")
Data= filein(fileName)
Data.items()


for key, value in Data.items():
    numGroup=numGroup+1
numGroup=numGroup-1


v=1
def treemake(v):


    global Acount1
    global Acount0
    global Bcount1
    global Bcount0
    global Ccount1
    global Ccount0
    global Dcount1
    global Dcount0
    currentG=""

    Keys=Data.keys()
    TargetValues= Data["target"]
    LetterValues= Data[Keys[v]]
    currentG=Keys[v]
    
    Acount1=0
    Acount0=0
    Bcount1=0
    Bcount0=0
    Ccount1=0
    Ccount0=0
    Dcount1=0
    Dcount0=0



    for i in range(0, len(LetterValues)):
        if LetterValues[i]== "A" and TargetValues[i]=="1":
            Acount1=Acount1+1
        elif LetterValues[i]== "A" and TargetValues[i]=="0":
            Acount0=Acount0+1
        elif LetterValues[i]== "B" and TargetValues[i]=="1":
            Bcount1=Bcount1+1
        elif LetterValues[i]== "B" and TargetValues[i]=="0":
            Bcount0=Bcount0+1
        elif LetterValues[i]== "C" and TargetValues[i]=="1":
            Ccount1=Ccount1+1
        elif LetterValues[i]== "C" and TargetValues[i]=="0":
            Ccount0=Ccount0+1
        elif LetterValues[i]== "D" and TargetValues[i]=="1":
            Dcount1=Dcount1+1
        elif LetterValues[i]== "D" and TargetValues[i]=="0":
            Dcount0=Dcount0+1


    entA=entropyCalc(Acount1, Acount0)
    entB=entropyCalc(Bcount1, Bcount0)
    entC=entropyCalc(Ccount1, Ccount0)
    entD=entropyCalc(Dcount1, Dcount0)
    entropies["entA"]=entA
    entropies["entB"]=entB
    entropies["entC"]=entC
    entropies["entD"]=entD
    sorted(entropies.items(), key=itemgetter(1))
    entropies2=sorted(entropies.items(), key=itemgetter(1))
    minent1={}
    minent1= entropies2[0]
    minent2= minent1[1]

    for key, valuen in entropies.items():
        if entropies[key]==100 or entropies[key]!=minent2:
            if key== "entA":
                print "Is ",currentG,"= A?"
                print"      1 happens ", Acount1," times."
                print"      0 happens ", Acount0," times."
            elif key== "entB":
                print "Is ",currentG,"= B?"
                print"      1 happens ", Bcount1," times."
                print"      0 happens ", Bcount0," times."
            elif key== "entC":
                print "Is ",currentG,"= C?"
                print"      1 happens ", Ccount1," times."
                print"      0 happens ", Ccount0," times."
            elif key== "entD":
                print "Is ",currentG,"= D?"
                print"      1 happens ", Dcount1," times."
                print"      0 happens ", Dcount1," times."

        else:
            if key== "entA":
                print "Is ",currentG,"= A?"
            elif key== "entB":
                print "Is ",currentG,"= B?"
            elif key== "entC":
                print "Is ",currentG,"= C?"
            elif key== "entD":
                print "Is ",currentG,"= D?"
            print"------------------------------"
            print v
            v=v+1
            if(v<=numGroup):
                return treemake(v)
            


treemake(1)







        
        
        





    



