#!/usr/bin/env python2

__author__ = "Andrea Fioraldi"
__mail__ = "fioraldi.1692419@studenti.uniroma1.it"

import sys

if len(sys.argv) < 2:
    print "usage: validation.py <bernoulli|multinomial>"
    exit()

def naive_bayes_classify_text(d, Pc, Pwc):
    m = 0
    am = None
    for c in Pc:
        try:
            r = Pc[c]*reduce(lambda x, y: x*y, filter(lambda x: x is not None, map(lambda x: Pwc.get((x, c)), d)))
            if r > m:
                m = r
                am = c
        except: continue
    return am

with open("validation.csv") as f:
    validation = map(lambda x: x.strip().split(","), filter(lambda x: len(x) > 0, f.readlines()))

with open("%s_Pc.csv" % sys.argv[1]) as f:
    Pc_list = map(lambda x: x.strip().split(","), filter(lambda x: len(x) > 0, f.readlines()))
with open("%s_Pwc.csv" % sys.argv[1]) as f:
    Pwc_list = map(lambda x: x.strip().split(","), filter(lambda x: len(x) > 0, f.readlines()))

Pc = {x[0]: float(x[1]) for x in Pc_list}
Pwc = {(x[0], x[1]): float(x[2]) for x in Pwc_list}

passed = 0
failed = 0
TP = 0.0
TN = 0.0
FP = 0.0
FN = 0.0

for test in validation:
    r = naive_bayes_classify_text(test[1:], Pc, Pwc)
    if r != test[0]:
        failed += 1
        if test[0] == "notmalware":
            FP += 1
        else:
            FN += 1
    else:
        passed += 1
        if test[0] == "malware":
            TP += 1
        else:
            TN += 1

with open("feature_classes.txt") as f:
    print f.read()
print
print "passed:", passed
print "failed:", failed
prec = TP / (TP + FP)
print "precision:", prec
recall = TP / (TP + FN)
print "recall:", recall
print "FP rate:", FP / (FP + TN)
print "FN rate:", FN / (FN + TP)
print "accuracy:", (TP + TN) / (TP + FN + TN + FP)
print "F-measure:", 2*(prec*recall)/(prec+recall)

