Censor Dispenser Challenge Project (Python)

Hi all! Can anyone please tell me how to see the code output on Jupyter Notebook? For Reggie’s Linear Regression there was an output box… :thinking: (I was trying to see if anyone else had the same question and I saw one person asked it, but I didn’t see an answer) TY!

You have to run the cells.

Personally I wouldn’t want anything from the notebook and would therefore either ask jupyter to convert the notebook to .py or write my code to a .py file instead of in the notebook

Hi All,

I’m very new to Python and have been stuck on the last part of this exercise for a while now. My code is a mismatch of everything from here, the internet, and my own brain, but somehow for part four I thought I had a pretty nifty idea of using enumerate, but it does not return anything to console :frowning:

Can someone check what I’m doing wrong please? Thank you! :slight_smile:

# 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()

proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself","Helena"]
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"]
punctuation = [",", "!", "?", ".", "%", "/", "(", ")", "\t", "\n","'"]

def censor_phrase(phrase):
  censor = ""
  for i in phrase:
    if i == " ":
      censor += " "
    else:
      censor += "*"
  return censor

def censor(text, phrase):
  text_lower = text.lower()
  phrase_lower = phrase.lower()
  censor = censor_phrase(phrase)
  ind = text_lower.find(phrase_lower)
  while ind > 0:
    if text[ind-1] != " " and text[ind-1] not in punctuation:
        ind = text_lower.find(phrase_lower, ind+1)
    elif text[ind+len(phrase)] != " " and text[ind+len(phrase)] not in punctuation and text[ind+len(phrase)] != "s":
        ind = text_lower.find(phrase_lower, ind+1)
    else:
        text = text[:ind] + censor + text[ind+len(phrase):]
        ind = text_lower.find(phrase_lower, ind+1)
  return text

def censor_multiple(text, words):
  for i in words:
    text = censor(text, i)
  return text

def censor_multiple_and_neg(text, words, negative):
  count = 0
  for i in negative:
    for j in range(len(text)-len(i)+1):
      verify = text.lower()[j:j+len(i)]
      censor = censor_phrase(i)
      if verify == i.lower():
          count += 1
          if count >= 2:
            text = text[:j] + censor + text[j+len(i):]
  text = censor_multiple(text, words)
  return text

def censor_final(text, words, negative):
   text = censor_multiple_and_neg(text, words, negative)
   text_split = text.split()
   for c, v in enumerate(text_split):
     for j in range(len(text)-len(v)+1):
       verify = text.lower()[j:j+len(v)]
       if verify == v and "*" in v:
         censor_left = censor_phrase(text_split[c-1])
         censor_right = censor_phrase(text_split[c+1])
         text = text[:j-1-len(censor_left)] + censor_left + v + censor_right + text[j+len(censor_right):]
  return text

print(censor(email_one, "learning algorithms"))
print("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -")
print(censor_multiple(email_two, proprietary_terms))
print("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -")
print(censor_multiple_and_neg(email_three, proprietary_terms, negative_words))
print("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -")
print(censor_final(email_four, proprietary_terms, negative_words))

Hi!
This was my solution for the project.