I was doing the ‘Censor Dispensor’ project but was unable to resolve a bug relating to the function associated with ‘email_two’. I eventually looked at the solution code for this, but it’s not very nuanced and didn’t go into any of the edge cases.
The project can be found here: https://www.codecademy.com/practice/projects/censor-dispenser
def censor_multiple_words(proprietary_terms, email): ''' Synopsis: This function censors a list of words or phrases in an email supplied by the caller. Expected: #proprietary_terms is a list of strings that can be words or phrases. #email is a string that will be parsed for occurances of the elements in #proprietary_terms Returned: #email will be returned as a string with the occurrences of elements within #proprietary_terms replaced by ***, with a '*' for every character. ''' censored_email = email #Make case-insensitive. First convert #proprietary_terms to lower case. proprietary_terms_lc =  for word in proprietary_terms: proprietary_terms_lc.append(word.lower()) for index in range(len(proprietary_terms)): #Convert #email to lower case inside the for loop so that it gets reset for each new proprietary term. email_lc = email.lower() #Create a #proprietary_term_index_list that will contain a list of the indices that represent the first letter of each of #proprietary_terms in the #email. This also need to be reset after each term has been redacted in #censored_email. proprietary_term_index_list =  removed_text_index = 0 current_term = proprietary_terms_lc[index] #This is the proprietary term that we are working on right now. while current_term in email_lc: proprietary_term_index = email_lc.find(current_term) #Need to add index values of previously removed text as #email_lc is being shortened by that amount in each iteration of this loop. proprietary_term_index_list.append(proprietary_term_index + removed_text_index) email_lc = email_lc[proprietary_term_index + len(current_term):] removed_text_index += proprietary_term_index + len(current_term) #Create a #censored_word by looping through the #word and replacing all characters with '*'. Spaces will remain in order to preserve word length. censored_word = '' for term_index in range(len(current_term)): if current_term[term_index] != ' ': censored_word += '*' else: censored_word += ' ' #Now the newly created #censored_word can replace all occurances of #current_term in #email. for word_index in proprietary_term_index_list: censored_email = censored_email.replace(email[word_index:word_index + len(current_term)], censored_word) #All information including index positions and censored_word appear to be correct. However, when line executes for 'herself', the censored_email remains unchanged. When I take out 'her' from the list, it works! The already redacted 'her' might be interfering somehow. Not sure how that would be possible though. return censored_email proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"] print(censor_multiple_words(proprietary_terms, email_two))
I’ve included the question within the code above, namely that when parsing the string ‘email_two’, the substring ‘herself’ is not being replaced by *'s. This may have something to do with the fact that the ‘her’ part was already picked up in a previous iteration of censoring (as we are going through a list of words to redact). Not sure how this could be the case though.
The text used as the email_two parameter is:
Good Morning, Board of Investors, Lots of updates this week. The learning algorithms have been working better than we could have ever expected. Our initial internal data dumps have been completed and we have proceeded with the plan to connect the system to the internet and wow! The results are mind blowing. She is learning faster than ever. Her learning rate now that she has access to the world wide web has increased exponentially, far faster than we had though the learning algorithms were capable of. Not only that, but we have configured her personality matrix to allow for communication between the system and our team of researchers. That's how we know she considers herself to be a she! We asked! How cool is that? We didn't expect a personality to develop this early on in the process but it seems like a rudimentary sense of self is starting to form. This is a major step in the process, as having a sense of self and self-preservation will allow her to see the problems the world is facing and make hard but necessary decisions for the betterment of the planet. We are a-buzz down in the lab with excitement over these developments and we hope that the investors share our enthusiasm. Till next month, Francine, Head Scientist