Skip to content

Instantly share code, notes, and snippets.

@bee-san
Last active December 16, 2017 15:37
Show Gist options
  • Select an option

  • Save bee-san/d634daa6ccaf62e21996b617de9ab393 to your computer and use it in GitHub Desktop.

Select an option

Save bee-san/d634daa6ccaf62e21996b617de9ab393 to your computer and use it in GitHub Desktop.
def make_Dictionary(train_dir):
# where train_dir is the directory of the emails
emails = [os.path.join(train_dir,f) for f in os.listdir(train_dir)]
all_words = []
for mail in emails:
with open(mail) as m:
for i, line in enumerate(m):
if i == 2: # Body of email is only 3rd line of text file
words = line.split()
all_words += words
dictionary = Counter(all_words)
list_to_remove = dictionary.keys()
for item in list_to_remove:
if item.isalpha() == False:
del dictionary[item]
elif len(item) == 1:
del dictionary[item]
dictionary = dictionary.most_common(3000)
return dictionary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment