Skip to content
scipython.com

P2.6.2Censoring a text

Write a program to read in a text file and censor any words in it which are on a list of banned words by replacing their letters with the same number of asterisks. Your program should store the banned words in lower case but censor examples of these words in any case. Assume there is no punctuation.

As a bonus exercise, handle text which contains punctuation. For example, given the list of banned words: ['C', 'Perl', 'Fortran'] the sentence:

'Some alternative programming languages to Python are C, C++, Perl, Fortran and Java.'

becomes:

'Some alternative programming languages to Python are *, C++, ****, ******* and Java.'
Show solution

In the code presented below, we read in the whole text and loop over every word in every paragraph checking to see if it is on the banned list and replacing it with the same length of asterisks if it is.

# Lower-case versions of all words will be compared with these banned words:
banned_words = ['c', 'perl', 'fortran']

fi = open('censor-test.txt', 'r')
text = fi.read()
fi.close()

# Loop over each word in each paragraph: split the text by the newline
# character to obtain a list of paragraphs; split each paragraph on
# whitespace to obtain a list of words.
censored_paras = []
for para in text.split('\n'):
    censored_words = []
    # For each paragraph, split into words on whitespace
    for word in para.strip().split():
        compare_word = word.lower()
        # Strip punctuation characters from the word
        for punctuation in (',.:;!?\'"'):
            compare_word = compare_word.replace(punctuation, '')
        if compare_word.lower() in banned_words:
            word = word.lower().replace(compare_word, '*'*len(compare_word))
        censored_words.append(word)
    censored_paras.append(' '.join(censored_words))
censored_text = '\n'.join(censored_paras)
print(censored_text)

When fed the file censor_text.txt it produces:

Some alternative programming languages to Python are *, C++, ****,
******* and Java.
Of these, Python is the best. Not Java or ****!
The fastest are *, C++ and *******.