lundi 14 novembre 2016

Loop and conditions, trying to find str in file directory

im using python 3.5 and im trying to implement a spam filter.. I need to loop the file for my directory, a nested loop with conditions...

def bayes(probspam, probham):
        lista={k: [probspam[k][0]/probham[k][0] if probham[k][0] != 0 else 0.0] for k in probham} 
        return lista
def condizprobfinalspam(dictionary):

    dictionary = { key: value for key, value in dictionary.items() if value[0] < 0.5}
    return dictionary
def condizprobfinalham (dictionary):
    dictionary = { key: value for key, value in dictionary.items() if value[0] > 0.5 }
    return dictionary    

ultimaprob=(bayes(probfinalespam,probtot))

These are my previous function to help you to understand my problem. The loop im talking about is this.. I would have the output of each conditions..

sommaemailham=int()
sommaemailspam=int()
for file in os.listdir("C:\\database\\test\\"):
    if file.endswith("ham.txt"):
        sommaemailham=len(condizprobfinalham(ultimaprob))
    elif file.endswith("spam.txt"):
        sommaemaispam=len(condizprobfinalspam(ultimaprob))

This funciton should find all text that end with "ham.txt" or "spam.txt" and should apply condizprobfinalham() and condizprobfinalspam() It prints only the number of sommaemailhamwhich is 5 but the elif prints 0.. The true output are sommaemailham=6 and sommaemailspam=3 It's like that second condition doesn't work because print 0.. Sorry for my bad english and thanks in advance!

Aucun commentaire:

Enregistrer un commentaire