Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created March 6, 2020 00:46
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/566c1c992fcf1377a996a8c459523ff0 to your computer and use it in GitHub Desktop.
Save codecademydev/566c1c992fcf1377a996a8c459523ff0 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()
proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithms", "herself", "her", "She", "Her"]
negative_words = ["concerned", "behind", "dangerous", "danger", "alarming", "alarmed", "out of control", "help", "unhappy", "bad", "upset", "awful", "broken", "damage", "damaging", "dismal", "distressing", "distressed", "concerning", "horrible", "horribly", "questionable"] #distressed was twice, change it to distressing.
def censor_all_instances (phrase, email):
if len(phrase) < 2:
return email # we are not censoring 1 or null characters
censor_phrase = ""
email_censored = email
for i in phrase:
censor_phrase += "*" # will be the same lenght as phrase
email_censored = email.replace(phrase, censor_phrase)
return email_censored
def censor_all_words_or_phrases (email):
"""
While performing tests I noticed that the email says "learning algorithms" instead of "learning algorithm",
"""
email_censored = email
for phrase in proprietary_terms:
email_censored = censor_all_instances (phrase, email_censored)
return email_censored
def censor_negative_language (email):
"""
I decided to censor all negative words in the email if more than 2 are detected and not follow the hint allowing the first negative word to appear and censor all the rest as the task says: "...censor any occurance of a word from the “negative words” list after any “negative” word has occurred twice..."
"""
censor = []
email_censored = email
for word in negative_words:
if email.find(word) >= 0: # if -1 the word is not found
censor.append(word)
if len(censor) > 2: #detected more than 2 negative words
for word in censor:
email_censored = censor_all_instances (word, email_censored)
email_censored = censor_all_words_or_phrases(email_censored)
return email_censored
def censor_all (email):
email_censored = email
censor = negative_words + proprietary_terms
for word in censor:
index = 0
start = 0
while index < len(email):
index = email.find(word, index)
if index == -1: # word not found
break
else: # we found one instance of word
# Looking for all words BEFORE to censor.
# We will stop if we found a "," or a "."
before = index - 1
before = email.rfind(".", start,before)
if before == -1: # no point before word
before = email.rfind(",", start, index-1)
if before == -1: # no comma before word
censor_phrase = email[start:index]
if censor_phrase != " ":
email_censored = censor_all_instances (censor_phrase, email_censored)
else: # found a comma
censor_phrase = email[before+1:index]
if censor_phrase != " ":
email_censored = censor_all_instances (censor_phrase, email_censored)
else: # found a point
censor_phrase = email[before+1:index]
if censor_phrase != " ":
email_censored = censor_all_instances (censor_phrase, email_censored)
# Looking for all words AFTER to censor.
# We will stop if we found a "," or a "."
index += len(word) #looking for the next instance
start = index
after = email.find(".", start,len(email))
if after == -1: # no point after word
after = email.find(",", start, len(email))
if after == -1: # no comma after word
censor_phrase = email[start:index]
email_censored = censor_all_instances (censor_phrase, email_censored)
else: # found a comma
censor_phrase = email_censored[start:after]
email_censored = censor_all_instances (censor_phrase, email_censored)
else: # found a point
censor_phrase = email[start:after]
email_censored = censor_all_instances (censor_phrase, email_censored)
email_censored = censor_all_instances (word, email_censored) # we need to censor the word as well
return email_censored
print (censor_all_instances ("learning algorithms", email_one))
print ("\n")
print (censor_all_words_or_phrases (email_two))
print ("\n")
print (censor_negative_language (email_three))
print ("\n")
print (censor_all(email_four))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment