Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created March 5, 2020 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/59264389482672d90e9f750154b145b6 to your computer and use it in GitHub Desktop.
Save codecademydev/59264389482672d90e9f750154b145b6 to your computer and use it in GitHub Desktop.
Codecademy export
# These are the emails you will be censoring. 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()
censor_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"]
censor_words = proprietary_terms + negative_words
# censor one
def censor_one(email, word):
length = len(word)
email = email.replace(word, len(word) * '*')
return email
#print(censor_one(email_one, censor_word))
# censor two
def censor_two(email, lst):
email_in_lower = email.lower()
# make it a list since strings are immutable
email = list(email)
for term in lst:
index = 0
while index < len(email):
index = email_in_lower.find(term, index)
# if nothing more is found, -1 is returned
if index == -1:
break
if email[index-1] == ' ' or email[index-1] == '\n':
for i in range(index, index + len(term)):
email[i] = '*'
index += len(term)
email = ''.join(email)
return email
#print(censor_two(email_two, proprietary_terms))
# censor three
def censor_three(email, lst):
email = censor_two(email_three, proprietary_terms)
email_in_lower = email.lower()
email = list(email)
indexes = []
for term in lst:
index = email_in_lower.find(term)
if index != -1:
indexes.append(index)
indexes.sort()
for term in lst:
index = indexes[0] + 1
while index < len(email):
index = email_in_lower.find(term, index)
if index == -1:
break
if email[index-1] == ' ' or email[index-1] == '\n':
for i in range(index, index + len(term)):
email[i] = '*'
index += len(term)
email = ''.join(email)
return email
#print(censor_three(email_three, negative_words))
# censor four
def censor_four(email, lst):
email_split = email.split()
censor_words_indexes = []
new_indexes = []
for i in range(len(email_split)):
for j in range(len(lst)):
if email_split[i].lower() == lst[j] or email_split[i][:-1].lower() == lst[j]:
censor_words_indexes.append(i)
for i in censor_words_indexes:
new_indexes.append(i - 1)
new_indexes.append(i)
new_indexes.append(i + 1)
for i in new_indexes:
email_split[i] = len(email_split[i]) * '*'
email_split = ' '.join(email_split)
return email_split
#print(censor_four(email_four, censor_words))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment