Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created August 7, 2020 06:44
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/8b94e7fd3f26f2937845101aa38ec109 to your computer and use it in GitHub Desktop.
Save codecademydev/8b94e7fd3f26f2937845101aa38ec109 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()
def censor(text, word):
return text.replace(word, "*******")
#print(censor(email_one, "learning algorithms"))
def censor_more(text, proprietary_terms):
for term in proprietary_terms:
if term in text:
text = censor(text, term)
return text
proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]
#print(censor_more(email_two, proprietary_terms))
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"]
def censor_negative(text, negative_words):
revised_text = censor_more(text, proprietary_terms)
count = 0 #negative word count
for term in negative_words:
if term in revised_text:
count += 1
if term in revised_text and count == 2:
revised_text = censor(revised_text, term)
return revised_text
#print(censor_negative(email_three, negative_words))
def max_censorship(text):
censor_char = "*"
redact_word_indices = []
max_revised = censor_negative(text, negative_words)
temp_email = max_revised.split()
for i in range(len(temp_email)):
if censor_char in temp_email[i]:
redact_word_indices.append(i)
for i in redact_word_indices:
temp_email[i-1] = "*" * len(temp_email[i-1])
temp_email[i+1] = "*" * len(temp_email[i+1])
max_revised = " ".join(temp_email)
return max_revised
#print(max_censorship(email_four))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment