Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created October 22, 2020 06:41
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/7aebbe84e72280cf0a524fa7bc18a8c5 to your computer and use it in GitHub Desktop.
Save codecademydev/7aebbe84e72280cf0a524fa7bc18a8c5 to your computer and use it in GitHub Desktop.
Codecademy export
import re
# 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()
punctuation = [' ', '.',',','!','?',';',':','-','[',']','{','}','(',')',"'",'"']
def censor_one(email, word):
censored_text = ""
for i in range(len(word)):
if (word[i] in punctuation):
censored_text = censored_text + word[i]
else:
censored_text = censored_text + "X"
return re.sub(r'\b' + word + r'\b', censored_text, email)
#print(censor_one(email_one, "learning algorithms"))
def cencor_word_list(lists_censor, email):
for word in lists_censor:
censored_text = ""
for i in range(len(word)):
if(word[i] in punctuation):
censored_text = censored_text + word[i]
else:
censored_text = censored_text + "X"
email = re.sub(r'\b' + word + r'\b', censored_text, email)
return email
proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]
#print(cencor_word_list(proprietary_terms, email_two))
def cencor_negative_words(negative_words, proprietary_terms, email):
for word in negative_words:
word_list = re.findall(r'\b' + word + r'\b', email)
if (len(word_list) > 2):
censored_text = ""
for i in range(len(word)):
if(word[i] in punctuation):
censored_text = censored_text + word[i]
else:
censored_text = censored_text + 'X'
email = re.sub(r'\b' + word + r'\b', censored_text, email)
email = cencor_word_list(proprietary_terms, email)
return email
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(cencor_negative_words(negative_words, proprietary_terms, email_three))
def cencor_any_words(proprietary_terms, negative_words, email):
lists = proprietary_terms + negative_words
for word in lists:
censored_text = ""
for i in range(len(word)):
if word[i] in punctuation:
censored_text = censored_text + word[i]
else:
censored_text = censored_text + "X"
email = re.sub(r'\b' + word + r'\b', censored_text, email)
email_split = email.split('\n')
new_email_split = []
for item in email_split:
if item != '':
item_split = item.split()
new_item_split = [word for word in item_split]
for i in range(len(item_split)):
if not 'X' in item_split[i]:
continue
if(i > 0):
word_before = item_split[i-1]
if not 'X' in word_before:
censored_text = ""
for letter in word_before:
if letter in punctuation:
censored_text = censored_text + letter
else:
censored_text = censored_text + 'X'
new_item_split[i - 1] = censored_text
if (i < len(item_split) - 2):
word_after = item_split[i + 1]
if not 'X' in word_after:
censored_text = ""
for letter in word_after:
if letter in punctuation:
censored_text = censored_text + letter
else:
censored_text = censored_text + 'X'
new_item_split[i + 1] = censored_text
new_item = ' '.join(new_item_split)
new_email_split.append(new_item)
else:
new_email_split.append(' ')
new_email = '\n'.join(new_email_split)
return new_email
#print(cencor_any_words(proprietary_terms, negative_words, email_two))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment