-
-
Save codecademydev/f6eae6c61194a9580969063035333b6d to your computer and use it in GitHub Desktop.
Codecademy export
This file contains 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
# These are the emails you will be censoring. '''Censors emails, to keep valuable information hidden''' | |
#The open() function is opening the text file that the emails are contained in and the .read() method is allowing us to save their contexts to the following variables: | |
email_one = open("email_one.txt", "r").read() | |
email_two = open("email_two.txt", "r").read() | |
email_three = open("email_three.txt", "r").read() | |
email_four = open("email_four.txt", "r").read() | |
def censor_word(email, word): | |
'''Censors a single word/phrase and returns email''' | |
censor = '' | |
for i in range(len(word)): | |
if word[i] == ' ': | |
censor += ' ' | |
else: | |
censor += '*' | |
return email.replace(word, censor) | |
def censor_proprietary_terms(email): | |
'''Censors all proprietary terms and returns email''' | |
for term in proprietary_terms[::-1]: | |
email = censor_word(email, term) | |
email = censor_word(email, term.title()) | |
return email | |
def censor_negative_words_and_p_terms(email): | |
'''Censors al prioprietary terms and negative words and returns email''' | |
email = censor_proprietary_terms(email) #censor_proprietary_terms | |
email_words = email.split(' ') #List of each word on email | |
email_words = email.split('\n') | |
for negative_word in negative_words:#For word in negative_words | |
count = 0 | |
for word in email_words: | |
if (negative_word in word) or (negative_word.title() in word): | |
count += 1 | |
if count > 2: | |
email = censor_word(email, negative_word) | |
email = censor_word(email, negative_word.title()) | |
return email | |
def censor_it_all(email): | |
'''Censor all proprietary terms, negative words and each word that comes before and after the censored word/phrase, returns email''' | |
email = censor_proprietary_terms(email)#Censor proprietary terms | |
for negative_word in negative_words: #Censor negative words | |
email = censor_word(email, negative_word) | |
email = censor_word(email, negative_word.title()) | |
email_updates = [] | |
email_paragraphs = email.split('\n') #List of each word on email | |
for paragraph in email_paragraphs: | |
paragraph_words = paragraph.split(' ') | |
count = 0 | |
for word in range(0, len(paragraph_words)-1): | |
if ('*' in paragraph_words[word]) and (count % 2 == 0): | |
count += 1 | |
paragraph_words[word-1] = '*'*len(paragraph_words[word-1]) | |
paragraph_words[word+1] = '*'*len(paragraph_words[word+1]) | |
paragraph_words = ' '.join(paragraph_words) | |
email_updates.append(paragraph_words) | |
email_updates = '\n'.join(email_updates) | |
return email_updates | |
word = 'learning algorithms' | |
proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"] | |
negative_words = ["concerned", "behind", "danger", "dangerous", "alarming", "alarmed", "out of control", "help", "unhappy", "bad", "upset", "awful", "broken", "damage", "damaging", "dismal", "distressed", "distressed", "concerning", "horrible", "horribly", "questionable"] | |
email = censor_it_all(email_four) | |
print(email) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment