Last active
December 16, 2017 15:37
-
-
Save bee-san/d634daa6ccaf62e21996b617de9ab393 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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