Censor Dispenser Challenge Project (Python)

Hi Everyone!
Have just finished with the projected.
Cheated a bit with the “researcHERs”, but all other parts were solved strictly to the task.
Please, check out my code on GItHub:
https://gist.github.com/nktnlx/96d4720d31b0ee98c62dee150be91c97
Any feedback and comments are welcomed.
Cheers and happy coding!

For the solution code they give if you run the censor function for email 2 it has some censor issues for letters inside a word censoring so that the word itself cannot be fully censored. For example the list has both ‘her’ and ‘herself’ so when it runs code it gets rid of the her in the email but also causes herself to come out as ‘XXXself’ and now it cannot be censored as a full word. Is there a way to make sure it only replaces the term if it is a full word and not just part of another word?

Censor Dispenser - My solution. Probably not the most compact but I’m proud of it; it handles most cases of punctuation, casing and position. A couple of remaining cases to resolve:

  1. ‘counting’ negative phrases as well as individual negative words
  2. Adjusting words before and after where word appears immediately before a \n

your freaking smart lol

Not so fast…

>>> for w in ['east', 'eastern', 'eastend', 'easter', 'easterly',
        'yeast', 'beast', 'feast', 'least']:
	print (censor_word(w, 'east'))

	
####
####ern
####end
####er
####erly
y####
b####
f####
l####
>>> 

This is what happens when word is found in other words. string.replace() does not look for word boundaries, plurals, etc. It only looks for the character group in the string, and is greedy about it.

1 Like

Hello, everyone. I have decided to complete this project in Visual Studio Code (which I have never used before for code, AND I am a beginner in Python) and…
I spent two days only reading how to write a function to a txt file in VS Code and I tried everything possible (which I could find on the Internet) but it just doesn’t work. I get all sorts of errors, or I get nothing in the output window at all. I am very frustrated but I don’t want to give up just yet.

I know that I probably jumped the gun with the VS Code but now I really want to understand how this thing works.
I would like to hear any relatable advices on how to write a function to the txt file, so I could see the result of my censoring right away. Please give me an exampe for email_one.txt and the task 1 where we need to censor “learning algorithms”.

For the moment I returned to the point to where I started, so I want to share my very first humbe attempt to complete this task (but of cource it didn’t work):

email_one = open('email_one.txt', 'r').read()
new_email_one = open('rewrite.txt','w')

def censor(email_one, word):
   return new_email_one.write(email_one.replace("learning algorithms", "*"*len("learning algorithms")))
#print(text)

new_email_one.write(email_one)
#print(email_one)

new_email_one.close()

print(email_one)

Please, judge it however you want :slight_smile:

Not sure what you mean by writing a function to file.

You’d write code to file. Sure there might be functions in that code but that’s not … special.

You’re opening other files from your program, maybe you’d want to start by seeing if you can get your own program to run before you consider other files? You might run a simpler program to test your new environment:

print('hi')

If that runs successfully, but reading other files doesn’t, then you’d want to read the error messages, they’re probably telling you something like “hey that file you tried to open, it doesn’t exist”

1 Like

simple things like this surely work :slight_smile: I already checked and its fine. Problems start when i try to open txt file and try to write a function to it…

Edit: I am sure that the problem isn’t VS Code but my lack of skills in coding, so I want to learn

you’re probably not writing a function to it. you’re probably writing text to it.

and if that’s failing, you’d need to read the error message.

The Errors I got so far were very different, and I fixed each of them when it appearred. Currently, the result in the output is the absolutely unchanged text in email_one.txt. (the code I sent above gives this result)
Obviously, something is wrong with my code.

My Idea was to “rewrite” the email_one.txt with the only change that instead of “learning algorithms” I would get as many “*” as there are symbols in the length of this frase. Any tips here?

PS: Sorry if I was wrong with the terminology. Yes, I meant write code to a file, not function :slight_smile:

So in other words, no error.

If you look at your code, you write what you got from the other file. It shouldn’t change. You have this:

email_one = open('email_one.txt', 'r').read()
open('rewrite.txt','w').write(email_one)

You’re not calling censor, so the code in censor does not execute.


Your function should probably not be touching any files. Instead you’d want it to accept a string as input … make whirring noises, and return a new string. Then you could read a file, send that text to your function, get a new piece of text back, and write the new text to a different file.

Currently your censor function both changes a file and returns some value. It should probably only do one of those, and out of the two it’s generally more useful to return a string, allowing the caller to decide what to do with that string.

1 Like

I tried again and now it says "AttributeError: ‘int’ object has no attribute ‘write’ ". Ah…
Ok, let me think a bit more on what you wrote to me, maybe I will have some new ideas.

Thanks :’)

You’d get that if you … tried to call write on an int

# (parenthesis because otherwise that looks like a float --> 5.  <--)
(5).write()
1 Like

yeah i know, I already had this error before haha
I just don’t understand where it saw an integer there…

Maybe from the result of write, which you return in your censor

write(text, /) method of _io.TextIOWrapper instance
    Write string to stream.
    **Returns the number of characters written** (which is always equal to
    the length of the string).
1 Like

Just here to say: after a few more hours of trying, I did it! The code works within VS Code, holy moly! Thanks for the advice earlier today, it really gave me some food for thought.
Cheers!

I really thought its hopeless already

Can someone help me understand why my function for email three is returning ‘alarming’ as a double when it’s not on the list twice and I can’t see an obvious reason for this? I’ve been messing with it and I just can’t figure it out.

this project was, hard on my…very particular self. https://github.com/PillarofMorning/yup/blob/master/censer_dispenser

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

bad_combo = ["learning algorithms"]
negative_words = ["censored", "concerned", "suffering", "culling", "helena", "Horribly"  "behind", "danger", 
"dangerous", "alarming", "alarmed", "help", "unhappy", "bad", "upset", "awful", "broken", "damage", "damaging", 
"spiral", "dismal", "distressed","concerning", "horrible", "crises", "horribly", "questionable", "wrong", 
"distressing", "cut"]
proprietary_terms = ["she", "personality", "personality matrix", "sense of self", "self-preservation", "her", 
"herself","learning algorithms", "crises"]

#parsing global storage variant
# btb = ''


###################################################################################
# uncomment/comment this section and root for autopilot/userinput
#needs work. 
###################################################################################

# print('''please select a number for sensitivity of document
# 1 = clearence level: lobby for bobby the snickers guard
# 2 = clearence level: secretary hannish mcmannish, deligent but loose lipped
# 3 = clearence lever: negativity filter
# 4 = restricted
# ''')
# mop_bucket = float(input('please select a number for sensitivity of document:   '))
# stringency = mop_bucket
##
# mr_cleans_bald_head = input('if you have any additional terms you desire, otherwise \'n\':  ')
# if mr_cleans_bald_head == 'n':
#     mr_clean = ['']
#     pass
# else:
#     mr_clean = [mr_cleans_bald_head]
# if mop_bucket == 1:
#     banned_words = mr_clean + bad_combo
# if mop_bucket == 2:
#     banned_words = mr_clean + bad_combo + proprietary_terms
# if mop_bucket == 3:
#     banned_words = mr_clean + bad_combo + proprietary_terms
# if mop_bucket == 4:
#     banned_words = mr_clean + bad_combo + proprietary_terms
# else:
#     banned_words = mr_clean + bad_combo + proprietary_terms

###################################################################################
#greater list comprehension for filtering words. I'm lazy, and it really shows here. 
#might be a less clunky way, but doesn't seem to be a more thorough way.
###################################################################################

def redacted_terms(words_list):
    master_list, punctuation = [], [",", "!", "?", ".", "/", "(", ")", "'s"]
    for word in words_list:
        truth = word.find(' ')
        if truth != -1:
            simplify = word.split(' ')
            [master_list.append(simpleton) for simpleton in simplify if not 'of'] 
        master_list.append(word)  
        master_list.append(word.title())
        master_list.append(word.upper())
        for entry in punctuation:
            sub_list = ''.join(word+entry)
            master_list.append(sub_list)
    return master_list

###################################################################################
#redact content, smallest of function, replaces desired text with ascii blocks, 
#just copy paste them from the browser, python seems to recognize
###################################################################################

def delete(word): 
    return "█"*len(word)

###################################################################################
#base redactor, nothin fancy, keep it simple. dont cut things up or change'em if not 
# needed. dont like uniformity loss but do like lack of headache
###################################################################################

def redactor(sensitive_document, banned_words):
    transient_document = sensitive_document 
    for word in banned_words: 
        transient_document = transient_document.replace(str(''+word+''), str('' +delete(word)+ ''))
    return transient_document

###################################################################################
#wheres the beef? here it is. struggle_bus.count() = 1 struggle_bus.find() me[here]
###################################################################################

def classified_redaction(sensitive_document, to_redact):  
   empty_seat, struggle_bus = -1, sensitive_document.split(' ')
    for term in struggle_bus:
        if term in to_redact:
            empty_seat += 1
            if empty_seat+1 < len(struggle_bus): 
                struggle_bus[empty_seat-1], struggle_bus[empty_seat+1], struggle_bus[empty_seat] = delete(term), delete(term), delete(term)
            elif empty_seat+1 > len(struggle_bus) and empty_seat-1 >= 0:
                struggle_bus[empty_seat-1], struggle_bus[empty_seat]= delete(term), delete(term)
            else:
                struggle_bus[empty_seat]= delete(term)
        else:
            empty_seat += 1
    return ' '.join(struggle_bus)

###################################################################################
#core operating module. split into two nodes for userinput and autopilot
#autopilot needs to aggregate banned lists before hand so just look/comment below.
###################################################################################

def redact_root(sensitive_document, proprietary_terms, stringency):
    banned_words = redacted_terms(proprietary_terms+bad_combo)
    if stringency == 1:
        for word in bad_combo: 
            return sensitive_document.replace(str(''+word+''), str('' +delete(word)+ ''))
    if stringency == 2:
        return redactor(sensitive_document, banned_words)
    if stringency == 3:
        for word in negative_words:
            occurences = sensitive_document.count(word)
            if occurences > 2:
                banned_words.append(word)
        return redactor(sensitive_document, banned_words)
    if stringency == 4:
        return classified_redaction(sensitive_document, redacted_terms(proprietary_terms+negative_words+bad_combo))
    else: 
        return redactor(sensitive_document, banned_words)


stringency = float(1)
print('''
number %f ''' %(stringency))
print(redact_root(email_one, [], stringency))
stringency = float(2)
print('''
number %f ''' %(stringency))
print(redact_root(email_two, proprietary_terms, stringency))
stringency = float(3)
print('''
number %f ''' %(stringency))
print(redact_root(email_three, proprietary_terms, stringency))
stringency = float(4)
print('''
number %f ''' %(stringency))
print(redact_root(email_four, proprietary_terms, stringency))



###################################################################################
#n hier we gaht the good ol' goshdarn usahemput for flexibility n testin.
#jus go ahead and uncomment the below between hashies and comment the above
###################################################################################


# def redact_root(sensitive_document, banned_words, stringency):
#     print(stringency)
#     if stringency == 1:
#         for word in bad_combo: 
#             redacted_document = sensitive_document.replace(str(''+word+''), str('' +delete(word)+ ''))
#     if stringency == 3:
#         for word in negative_words:
#             occurences = sensitive_document.count(word)
#             if occurences > 2:
#                 banned_words.append(word)
#                 redacted_document = redactor(sensitive_document, banned_words)
#     if stringency == 4:
#         redacted_document = classified_redaction(sensitive_document, banned_words)
#     else: 
#         print('amigoinhere?')
#         redacted_document = redactor(sensitive_document, banned_words)
#     return redacted_document



# stringency = mop_bucket
# print(redact_root(email_one, [], stringency))
# print(redact_root(email_two, redacted_terms(banned_words), stringency))
# print(redact_root(email_three, redacted_terms(banned_words), stringency))
# print(redact_root(email_four, redacted_terms(banned_words), stringency))



###################################################################################
#thats all folks.

###################################################################################




This is my solution to the challenge. It works, although the “censor everything” function isn’t perfect. I was happy to get a solution that was able to preserve formatting, and punctuation.

Tried my part of coding this problem https://gist.github.com/cf89d792dc0fcca4f8a4636bf513488f