Censoring a part of a word


#1

If I put the text as “This hacker loves hacking but cannot hack this” and the word I want to censor is “hack”. What would be the code to do that?
I want the output as:

This ****er loves ****ing but cannot ****
(I want to retain the -er and -ing of hacker and hacking)


#2

What you desire rules out word boundaries as utilized in other examples. Since it is too early to bring in advanced methods such as regular expressions, we need to iterate letter by letter and keep a floating list going that has the first letter removed and a new one added to the end with each shift through the text.

The pattern to be censored is "hack" which has a length of 4.

To start, we need four characters to form the first string for comparison. We can use slicing to obtain that string.

text = "This hacker loves hacking but cannot hack this"
word = "hack"
span = len(word)

for i in range(0, len(text) - span + 1):
    scan = text[i:i+span]
    print (scan)

If we run this code we’ll get the following output…

This
his 
is h
s ha
 hac
hack
acke
cker
ker 
er l
r lo
 lov
love
oves
ves 
es h
s ha
 hac
hack
acki
ckin
king
ing 
ng b
g bu
 but
but 
ut c
t ca
 can
cann
anno
nnot
not 
ot h
t ha
 hac
hack
ack 
ck t
k th
 thi
this

That tells us our code is accessing four characters at a time so we’re on the right track. Since strings are immutable we need to find a way to swap out the matches with **** which will take a little thought.

I should add that the obvious approach would be to apply the string method, replace() to the text but that would be too simple.

>>> def censor():
    return "This hacker loves hacking but cannot hack this".replace('hack', "****")

>>> censor()
'This ****er loves ****ing but cannot **** this'
>>> 

It takes all the fun out of it. We’re half the way to solving this with an algorithm and should continue along that path. See what you can do using the starting code we have above.


How are you making out? Turns out that splicing is one simple approach…

>>> text = 'This hacker loves hacking but cannot hack this'
>>> for i in range(0, len(text) - span + 1):
	scan = text[i:i+span]
	print (scan)
	if scan == word:
		text = text[:i] + '****' + text[i + span:]
		print (text)

		
This
his 
is h
s ha
 hac
hack
This ****er loves hacking but cannot hack this
***e
**er
*er 
er l
r lo
 lov
love
oves
ves 
es h
s ha
 hac
hack
This ****er loves ****ing but cannot hack this
***i
**in
*ing
ing 
ng b
g bu
 but
but 
ut c
t ca
 can
cann
anno
nnot
not 
ot h
t ha
 hac
hack
This ****er loves ****ing but cannot **** this
*** 
** t
* th
 thi
this
>>> text
'This ****er loves ****ing but cannot **** this'
>>> 

Making sense? Above we are working with what we know, up to this point.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.