Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created February 14, 2021 23:23
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/59ab4211cf34f4c60cc6e77ac7cb1975 to your computer and use it in GitHub Desktop.
Save codecademydev/59ab4211cf34f4c60cc6e77ac7cb1975 to your computer and use it in GitHub Desktop.
Codecademy export
import string
# 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_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_email(input_text, words_to_censor, before_and_after=False):
censor_list = []
censor_char = "********************"
word_list = []
split_input_text = split_the_text(input_text)
for censor_word in words_to_censor:
find_words = find_the_word_or_index(input_text, censor_word)
word_list += find_words
for word, index in word_list:
if before_and_after:
split_input_text[index - 1] = censor_char[:len(split_input_text[index - 1])]
split_input_text[index] = censor_char[:len(split_input_text[index])]
split_input_text[index + 1] = censor_char[:len(split_input_text[index + 1])]
else:
split_input_text[index] = censor_char[:len(word)]
output_text = ""
for word in split_input_text:
output_text += word + " "
return output_text
def split_the_text(input_text):
split_input_text = input_text.split()
text_split = input_text.split(" ")
newline_index_list = []
#find \n in text
newline_list = []
for i in range(0, len(text_split)):
if "\n" in text_split[i]:
for x in range(0, text_split[i].count("\n")):
newline_list.append(text_split[i].replace("\n", " ").split()[0])
for item in newline_list:
try:
index = split_input_text.index(item)
split_input_text.insert(index + 1, "\n")
except:
pass
return split_input_text
def find_the_word_or_index(input_text, word_or_index):
get_text = split_the_text(input_text)
result_list = []
index_value = 0
punctuation_list = ["'", ",", ".", "!", "?", "%", "/", "(", ")"]
punch_flag = False
unedited_word = word_or_index
if " " not in word_or_index:
for punch in punctuation_list:
if punch in str(word_or_index):
word_or_index = word_or_index.translate(None, string.punctuation)
punch_flag = True
try:
if str(unedited_word).isdigit():
return get_text[word_or_index]
elif str(unedited_word).isalpha() or (punch_flag):
counter = 0
for i in range(0, len(get_text)):
if get_text[i].translate(None, string.punctuation).lower() == word_or_index.lower():
if punch_flag == False:
result_list.append((word_or_index, counter))
else:
result_list.append((unedited_word, counter))
counter += 1
return result_list
#deals with multi censor word e.g sense of self.
elif " " in word_or_index:
split_word = word_or_index.split()
for i in range(0, len(get_text)):
if split_word[0].lower() == get_text[i].lower():
if get_text[i + 1] == split_word[1]:
counter = 0
for censor_word in split_word:
if censor_word == get_text[i + counter]:
result_list.append((censor_word, i + counter))
counter += 1
return result_list
except:
return result_list
return result_list
proprietary_terms = ["she", "personality matrix", "sense of self", "learning algorithms", "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"]
#print(censor_email(email_one, ["learning algorithms"]))
#print(censor_email(email_two, proprietary_terms))
#print(censor_email(email_three, negative_words))
#print(censor_email(email_four, proprietary_terms + negative_words, True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment