# Shopping1.py

dataFile = "shopping.dat"

def loadIntData(fileName):    
    '''
    Reads data table from Ascii file with comma separated integers.
    Returns table as matrix in format [[row0], [row1], ...].
    E.g. 
    1,2,3,4
    -1,1,-1,1
    20,50,100,600   
    returns
    [[1, 2, 3, 4], [-1, 1, -1, 1], [20, 50, 100, 600]]
    '''
    try:    
        fData = open(fileName, 'r')
    except:
        return []
    out = []
    for line in fData:
        li = [int(i) for i in line.split(',')]
        out.append(li)
    fData.close()
    return out

# Read dataset with attributes: 
# bread, cheese, apple, banana,  pasta, sugo
X = loadIntData(dataFile)
print "Rule: If a person buys pasta, they also buy sugo"

# How many of the cases that a person bought pasta involved 
# the person purchasing sugo too?
nbPastaPurchases = 0
ruleValid = 0
for sample in X:
    if sample[4] == 1:  # This person bought pasta
        nbPastaPurchases += 1
        if sample[5] == 1: # This person bought sugo too
            ruleValid += 1
print nbPastaPurchases, "cases of the pasta purchases were discovered."
print ruleValid, "cases of the rule being valid were discovered"

# The support is the (absolute) number of times  the rule is discovered
support = ruleValid  

# The confidence is the relative frequency the rule is discovered.
confidence = ruleValid / nbPastaPurchases 

print "The support is", support, "and the confidence is", \
       round(100 * confidence, 2), "percent"

