def poiFlagEmail(f): """ given an email file f, return a trio of booleans for whether that email is to, from, or cc'ing a poi """ to_emails, from_emails, cc_emails = getToFromStrings(f) ### poi_emails.poiEmails() returns a list of all POIs' email addresses. poi_email_list = poi_emails.poiEmails() to_poi = False from_poi = False cc_poi = False if to_emails: ctr = 0 while not to_poi and ctr < len(to_emails): if to_emails[ctr] in poi_email_list: to_poi = True ctr += 1 if cc_emails: ctr = 0 while not cc_poi and ctr < len(cc_emails): if cc_emails[ctr] in poi_email_list: cc_poi = True ctr += 1 if from_emails: ctr = 0 while not from_poi and ctr < len(from_emails): if from_emails[ctr] in poi_email_list: from_poi = True ctr += 1 return to_poi, from_poi, cc_poi
def poiFlagEmail(f): """ given an email file f, return a trio of booleans for whether that email is to, from, or cc'ing a poi """ to_emails, from_emails, cc_emails = getToFromStrings(f) ### poi_emails.poiEmails() returns a list of all POIs' email addresses. poi_email_list = poi_emails.poiEmails() to_poi = False from_poi = False cc_poi = False ### to_poi and cc_poi are boolean variables which flag whether the email ### under inspection is addressed to a POI, or if a POI is in cc, ### respectively. You don't have to change this code at all. ### There can be many "to" emails, but only one "from", so the ### "to" processing needs to be a little more complicated if to_emails: ctr = 0 while not to_poi and ctr < len(to_emails): if to_emails[ctr] in poi_email_list: to_poi = True ctr += 1 if cc_emails: ctr = 0 while not cc_poi and ctr < len(cc_emails): if cc_emails[ctr] in poi_email_list: cc_poi = True ctr += 1 ################################# ######## your code below ######## ### set from_poi to True if ##### ### the email is from a POI ##### ################################# if from_emails: ctr = 0 while not from_poi and ctr < len(from_emails): if from_emails[ctr] in poi_email_list: from_poi = True ctr += 1 # print to_poi, from_poi, cc_poi # used a print statement to test the code ################################# return to_poi, from_poi, cc_poi
import os import sys import zipfile from poi_flag_email import poiFlagEmail, getToFromStrings data_dict = {} with zipfile.ZipFile('emails.zip', "r") as z: z.extractall() for email_message in os.listdir("emails"): if email_message == ".DS_Store": continue message = open(os.getcwd() + "/emails/" + email_message, "r") to_addresses, from_addresses, cc_addresses = getToFromStrings(message) to_poi, from_poi, cc_poi = poiFlagEmail(message) for recipient in to_addresses: # initialize counter if recipient not in data_dict: data_dict[recipient] = {"from_poi_to_this_person": 0} # add to count if from_poi: data_dict[recipient]["from_poi_to_this_person"] += 1 message.close() for item in data_dict: print item, data_dict[item]
import os import sys import zipfile from poi_flag_email import poiFlagEmail, getToFromStrings data_dict = {} with zipfile.ZipFile('emails.zip', r) as z z.extractall() for email_message in os.listdir(emails) if email_message == .DS_Store continue message = open(os.getcwd()+emails+email_message, r) to_addresses, from_addresses, cc_addresses = getToFromStrings(message) to_poi, from_poi, cc_poi = poiFlagEmail(message) for recipient in to_addresses if recipient not in data_dict data_dict[recipient] = {from_poi_to_this_person0} else if from_poi data_dict[recipient][from_poi_to_this_person] += 1 message.close() for item in data_dict print item, data_dict[item]