Bird
Raised Fist0
Prompt Engineering / GenAIml~10 mins

Content filtering in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to filter out bad words from the text.

Prompt Engineering / GenAI
bad_words = ['spam', 'scam', 'fake']
text = 'This is a spam message.'
filtered = [word for word in text.split() if [1] not in bad_words]
print(filtered)
Drag options to blanks, or click blank then click option'
Aword
Btext
Cbad_words
Dfiltered
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' instead of 'word' causes a type error.
Using 'bad_words' inside the condition is incorrect because it's the list, not the current word.
2fill in blank
medium

Complete the code to replace bad words with asterisks in the text.

Prompt Engineering / GenAI
bad_words = ['spam', 'scam', 'fake']
text = 'This is a scam message.'
filtered_text = ' '.join([[1] if word in bad_words else word for word in text.split()])
print(filtered_text)
Drag options to blanks, or click blank then click option'
A'****'
B'####'
C'****', word
D'**** '
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of the mask string causes no replacement.
Adding spaces inside the mask string causes formatting issues.
3fill in blank
hard

Fix the error in the code to correctly detect if the text contains any bad words.

Prompt Engineering / GenAI
bad_words = ['spam', 'scam', 'fake']
text = 'Beware of fake news.'
contains_bad = any(word [1] bad_words for word in text.split())
print(contains_bad)
Drag options to blanks, or click blank then click option'
Anot in
Bin
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' compares the whole list to a word, which is incorrect.
Using 'not in' reverses the logic and gives wrong results.
4fill in blank
hard

Fill both blanks to create a dictionary counting occurrences of bad words in the text.

Prompt Engineering / GenAI
bad_words = ['spam', 'scam', 'fake']
text = 'spam spam scam fake fake fake'
counts = [1](word for word in text.split() if word [2] bad_words)
print(counts)
Drag options to blanks, or click blank then click option'
ACounter
Bin
Cnot in
Ddict
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dict' instead of 'Counter' does not count occurrences automatically.
Using 'not in' reverses the filter and counts good words instead.
5fill in blank
hard

Fill all three blanks to create a function that filters and replaces bad words with a mask.

Prompt Engineering / GenAI
def filter_text(text, bad_words, mask):
    return ' '.join([[1] if word [2] bad_words else word for word in text.split()])

text = 'This is a fake scam message.'
result = filter_text(text, ['spam', 'scam', 'fake'], [3])
print(result)
Drag options to blanks, or click blank then click option'
Amask
Bin
C'****'
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of 'mask' causes no replacement.
Using 'not in' reverses the logic and replaces good words.

Practice

(1/5)
1. What is the main purpose of content filtering in AI systems?
easy
A. To block or clean harmful text to keep users safe
B. To speed up the AI model training process
C. To increase the size of the training dataset
D. To improve the AI model's accuracy on images

Solution

  1. Step 1: Understand content filtering purpose

    Content filtering is designed to detect and remove harmful or unsafe text to protect users.
  2. Step 2: Compare options to purpose

    Only To block or clean harmful text to keep users safe matches this goal; others relate to unrelated AI tasks.
  3. Final Answer:

    To block or clean harmful text to keep users safe -> Option A
  4. Quick Check:

    Content filtering = block harmful text [OK]
Hint: Content filtering = blocking harmful or unsafe text [OK]
Common Mistakes:
  • Confusing filtering with training speed
  • Thinking filtering improves image accuracy
  • Assuming filtering increases data size
2. Which of the following is a correct way to check if a text contains a banned word in Python?
easy
A. if text.has(banned_word):
B. if text.contains(banned_word):
C. if banned_word in text:
D. if banned_word inside text:

Solution

  1. Step 1: Recall Python syntax for substring check

    In Python, the correct way to check if a substring is in a string is using in.
  2. Step 2: Evaluate each option

    if banned_word in text: uses correct syntax; others use invalid or non-Python methods.
  3. Final Answer:

    if banned_word in text: -> Option C
  4. Quick Check:

    Substring check in Python uses 'in' keyword [OK]
Hint: Use 'in' keyword to check substring in Python strings [OK]
Common Mistakes:
  • Using non-existent methods like contains()
  • Using wrong keywords like 'inside'
  • Confusing syntax from other languages
3. Given the code below, what will be the output?
bad_words = ['spam', 'scam']
text = 'This message contains spam and scam.'
filtered = any(word in text for word in bad_words)
print(filtered)
medium
A. None
B. False
C. Error
D. True

Solution

  1. Step 1: Understand the any() function with generator

    The expression checks if any bad word is found in the text. Since 'spam' and 'scam' are both in the text, any() returns True.
  2. Step 2: Confirm print output

    Printing filtered will output True because the condition is met.
  3. Final Answer:

    True -> Option D
  4. Quick Check:

    any() finds bad words = True [OK]
Hint: any() returns True if any bad word is found in text [OK]
Common Mistakes:
  • Thinking any() returns False if multiple matches
  • Confusing any() with all()
  • Expecting an error due to syntax
4. Identify the error in this content filtering code snippet:
bad_words = ['bad', 'ugly']
text = 'This is a bad example.'
if bad_words in text:
    print('Filtered')
else:
    print('Clean')
medium
A. Using 'in' to check list in string is incorrect
B. Missing colon after if statement
C. bad_words should be a string, not a list
D. print statement syntax is wrong

Solution

  1. Step 1: Analyze the 'if' condition

    The code tries to check if a list is in a string, which is invalid in Python.
  2. Step 2: Correct way to check bad words in text

    We should check each word individually, e.g., using any(word in text for word in bad_words).
  3. Final Answer:

    Using 'in' to check list in string is incorrect -> Option A
  4. Quick Check:

    Cannot check list in string directly [OK]
Hint: Check each word, not whole list, when filtering text [OK]
Common Mistakes:
  • Trying to use 'in' with list and string directly
  • Ignoring need for loop or any()
  • Assuming list membership works on strings
5. You want to replace all banned words in a user message with '[CENSORED]'. Which code snippet correctly does this for the list banned = ['bad', 'ugly'] and string msg = 'This is a bad and ugly day.'?
hard
A. msg = msg.replace(banned, '[CENSORED]') print(msg)
B. for word in banned: msg = msg.replace(word, '[CENSORED]') print(msg)
C. msg = '[CENSORED]' if word in banned else msg print(msg)
D. msg = msg.filter(lambda w: w not in banned) print(msg)

Solution

  1. Step 1: Understand string replacement for multiple words

    We must replace each banned word one by one using a loop and str.replace().
  2. Step 2: Evaluate each option

    for word in banned: msg = msg.replace(word, '[CENSORED]') print(msg) correctly loops and replaces; B tries to replace list directly (invalid); C uses wrong syntax; D uses filter on string (invalid).
  3. Final Answer:

    for word in banned: msg = msg.replace(word, '[CENSORED]') print(msg) -> Option B
  4. Quick Check:

    Loop and replace each banned word [OK]
Hint: Replace banned words one by one with a loop and replace() [OK]
Common Mistakes:
  • Trying to replace list directly in string
  • Using filter on string instead of list
  • Incorrect conditional replacement syntax