MulaiMulai sekarang secara gratis

Handle textual request data

Another requirement in the content moderation system is to take into account user comments' sentiment. The system needs to identify specific problematic phrases to help moderators review potentially inappropriate content.

You'll create an endpoint that analyzes text coming from users and extracts standardized moderation flags.

Latihan ini adalah bagian dari kursus

Deploying AI into Production with FastAPI

Lihat Kursus

Petunjuk latihan

  • Turn the incoming text into lowercase in the analyze_comment() function to make it case-insensitive.
  • Extract problem keywords in found_issues from the processed text using the problem_keywords list.
  • Return a JSON response with the following keys - issues (list of keywords), issue_count (integer), and the original_text (string).

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

@app.post("/analyze_comment")
def analyze_comment(text: str):
    problem_keywords = ["spam", "hate", "offensive", "abuse"]
    
    # Convert the input text to lowercase
    text_lower = ____
    # Extract matching flags using list comprehension
    found_issues = [____ for ____ in problem_keywords if keyword in text_lower]
    # Return the dictionary with required keys
    return {
        "____": found_issues,
        "____": len(found_issues),
        "____": text
    }
Edit dan Jalankan Kode