Hello, I’m working on the Censor Dispenser project:
I wrote the following in an attempt to solve the problem of email_two:
#defines censor function for a list of words or phrases def censor_dispenser_lst(text): proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"] proprietary_terms_title =  for i in proprietary_terms: proprietary_terms_title.append(i.title()) # created a bigger list of the terms plus the title case versions # to catch instaces where the word started the sentence super_lst = proprietary_terms + proprietary_terms_title new_text = text after = [" ", ",", ":", ";", "!", ".", ")", "-", "?", "\"", "\'", "s"] before = [" ", "\n"] for censor in sorted(super_lst, key=len, reverse=True): while censor in new_text and new_text[new_text.find(censor)+len(censor)] in after and new_text[new_text.find(censor)-1] in before: new_text = new_text[0:(new_text.find(censor))] + "x"*len(censor)+ new_text[(new_text.find(censor)+len(censor)):] # basically going from longest word to shortest, # does the big string have this word with one of these characters # before or after it? if so replace each character in the string # with an x return new_text.replace("xxs","xxx") #return the string, cleaning up the plurals # censor_lst function used on email_two email_two_censored = censor_dispenser_lst(email_two) print("subject: email_two_censored") print() print(email_two_censored) print() print()
The problem is this:
There’s that one solitary ‘her’ hanging out down there and I can’t get it to go away without butchering the word ‘researcHERs’ above it in the email. The thing is that it suits the criteria in the boolean I made above for selecting such instances. All the other instances of ‘her’ and all the other offending terms were removed. I can’t figure out why it’s there. It’s driving me up the wall!!
Is there anyone who can take a look and tell me what could possibly be causing this?
Edit: sorry about all the edits, there were a couple of different versions I came up with and I posted the wrong code and pic the first time!!