-
-
Save codecademydev/d5a590d06713026cb27e3f77fdd860c3 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() | |
""" | |
I'm back and ready for my third attempt. This time I won't be too worried about getting it perfect the first time. | |
Mr. Cloudy doesn’t care how you censor it, he just wants it done! | |
""" | |
#2 - Write a function to censor a sepcific word or phrase from a text. | |
def censor_one(email, phrase): | |
email = email | |
return email.replace(phrase, "@" * len(phrase)) | |
#2 - Testing censor_one | |
#print(censor_one(email_one, "learning algorithms")) | |
""" | |
Function censors all instances of 'learning algorithms' from email_one. Step 2 complete. | |
""" | |
#3 - Write a function that can censor a whole list of words or phrases | |
def censor_two(email, phrases): | |
email = email.lower() | |
for phrase in phrases: | |
email = email.replace(phrase, "@" * len(phrase)) | |
return email | |
#3 - Testing censor_two | |
proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself",] | |
#print(censor_two(email_two, proprietary_terms)) | |
""" | |
Attempt #1 - Function censors MOST instances of strings in proprietary_terms, but gets tripped up with capitals, punctuation, and small words in larger words ('her' in 'herself' and 'researchers). | |
""" | |
#3 - attempt #2 | |
def censor_two(email, phrases): | |
space_or_punct = [" ", ",", ".", "'", "!", "?", ":", ";", "-"] | |
phrases_with_capitals = [] | |
for phrase in phrases: | |
phrases_with_capitals.append(phrase) | |
phrases_with_capitals.append(phrase.capitalize()) | |
phrases_with_capitals.append(phrase.upper()) | |
for phrase in phrases_with_capitals: | |
email_copy = email | |
phrase_indexes = [] | |
phrase_length = len(phrase) | |
for x in range(email.count(phrase)): | |
phrase_indexes.append(email_copy.find(phrase)) | |
email_copy = email_copy.replace(phrase, "X" * phrase_length, 1) | |
for index in phrase_indexes: | |
if email[index - 1] in space_or_punct or index == 0 or email[index - 1] == "\n" : | |
if email[index + phrase_length] in space_or_punct: | |
email = email[:index] + "@" * len(phrase) + email[index + phrase_length:] | |
elif email[index + phrase_length] == "s": | |
if email[index + phrase_length + 1] in space_or_punct: | |
email = email[:index] + "@" * len(phrase) + email[index + phrase_length:] | |
else: | |
continue | |
return email | |
#3 - Testing attempt #2 | |
#print(censor_two(email_two, proprietary_terms)) | |
""" | |
Yes! I did it! Attempt #2 censors all instances of the terms in proprietary_terms and deals with with capitals, punctuation, and small words in larger words! Step 3 complete! | |
""" | |
#4 - Write a function to censor any occurance of a word from negative_words after any of the words have occured twice, as well as censoring everything from step 3. | |
def censor_three(email, neg_words): | |
space_or_punct = [" ", ",", ".", "'", "!", "?", ":", ";", "-"] | |
email = censor_two(email, proprietary_terms) | |
negatives_with_capitals = [] | |
for word in neg_words: | |
negatives_with_capitals.append(word) | |
negatives_with_capitals.append(word.capitalize()) | |
negatives_with_capitals.append(word.upper()) | |
negative_indexes = [] | |
for word in negatives_with_capitals: | |
email_copy = email | |
for x in range(email.count(word)): | |
negative_indexes.append(email_copy.find(word)) | |
email_copy = email_copy.replace(word, "X" * len(word), 1) | |
negative_indexes.sort() | |
for index in negative_indexes[2:]: | |
if email[index - 1] in space_or_punct or index == 0 or email[index - 1] == "\n" : | |
if email[index + len(word)] in space_or_punct: | |
email = email[:index] + "@" * len(word) + email[index + len(word):] | |
elif email[index + len(word)] == "s": | |
if email[index + len(word) + 1] in space_or_punct: | |
email = email[:index] + "@" * len(word) + email[index + len(word):] | |
else: | |
continue | |
return email | |
#4 - Testing censor_three | |
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(email_three) | |
#print(censor_three(email_three, negative_words)) | |
""" | |
Yay! Success on the first attempt. All the work I did on censor_two made it fairly easy to write this one. Step 4 complete. | |
""" | |
#5 - Write a function that runs censor_two, censors ALL negative words from censor_three, and then censors any words that come before AND after any of these censored words | |
def censor_four(email, negative_words): | |
space_or_punct = [" ", ",", ".", "'", "!", "?", ":", ";", "-"] | |
email = censor_two(email, proprietary_terms) | |
negatives_with_capitals = [] | |
for word in negative_words: | |
negatives_with_capitals.append(word) | |
negatives_with_capitals.append(word.capitalize()) | |
negatives_with_capitals.append(word.upper()) | |
negative_indexes = [] | |
for word in negatives_with_capitals: | |
email_copy = email | |
for x in range(email.count(word)): | |
negative_indexes.append(email_copy.find(word)) | |
email_copy = email_copy.replace(word, "X" * len(word), 1) | |
negative_indexes.sort() | |
for index in negative_indexes: | |
if email[index - 1] in space_or_punct or index == 0 or email[index - 1] == "\n" : | |
if email[index + len(word)] in space_or_punct: | |
email = email[:index] + "@" * len(word) + email[index + len(word):] | |
elif email[index + len(word)] == "s": | |
if email[index + len(word) + 1] in space_or_punct: | |
email = email[:index] + "@" * len(word) + email[index + len(word):] | |
else: | |
continue | |
email_words = email.split(" ") | |
print(email_words) | |
email_word_iter = iter(range(len(email_words))) | |
for x in email_word_iter: | |
if email_words[x] == "": | |
print("ugh") | |
if "@" in email_words[x]: | |
email_words[x - 1] = "@" * len(email_words[x - 1]) | |
email_words[x + 1] = "@" * len(email_words[x + 1]) | |
next(email_word_iter) | |
censored_email = "" | |
for word in email_words: | |
censored_email += word + " " | |
return censored_email | |
#testing censor_four | |
#print(email_four) | |
#print(censor_four(email_four, negative_words)) | |
""" | |
Attempt #1 censors all words from both lists, and almost all words next to them. | |
It gets tripped up when there is a new line, and the code is a little bloaty. | |
Gonna try again to iron out the kinks and try to write it more efficiently. | |
""" | |
#5 - Attempt #2 | |
def censor_four(email, negative_words, proprietary_terms): | |
bad_words = negative_words + proprietary_terms | |
email = censor_two(email, bad_words) | |
email = email.replace("\n"," ") | |
email = email.replace(" "," ") | |
email_words = email.split() | |
for x in range(len(email_words)): | |
if "@" in email_words[x]: | |
email_words[x] = "#" * len(email_words[x]) | |
email_words[x - 1] = "#" * len(email_words[x - 1]) | |
email_words[x + 1] = "#" * len(email_words[x + 1]) | |
censored_email = "" | |
for word in email_words: | |
censored_email += word + " " | |
return censored_email | |
#testing Attempt #2 | |
print(email_four) | |
print(censor_four(email_four, negative_words, proprietary_terms)) | |
""" | |
Attempt #2 deals with the previous issues by getting rid of all new lines in the email. It doesn't look as nice and I'm not completely satisfied, but "Mr. Cloudy doesn’t care how you censor it, he just wants it done."". A lot fewer lines of code as well which is nice. | |
""" | |
#6 | |
""" | |
I think I have addressed these possible issues already. | |
""" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment