| from typing import Literal, TypedDict | |
| SentimentClass = Literal["negative", "neutral", "positive"] | |
| class FeedbackAnalysis(TypedDict): | |
| sentiment: SentimentClass | |
| needs_followup: bool | |
| suggested_priority: str | |
| def analyze_feedback(rating: int, followup_flag: str) -> FeedbackAnalysis: | |
| """Map numeric rating and follow-up flag to sentiment and priority. | |
| - rating: 1–5 | |
| - followup_flag: "yes" or "no" | |
| """ | |
| if rating <= 2: | |
| sentiment: SentimentClass = "negative" | |
| elif rating == 3: | |
| sentiment = "neutral" | |
| else: | |
| sentiment = "positive" | |
| flag = followup_flag.strip().lower() == "yes" | |
| if not flag: | |
| priority = "low" if sentiment == "positive" else "normal" | |
| else: | |
| if sentiment == "negative": | |
| priority = "high" | |
| elif sentiment == "neutral": | |
| priority = "normal" | |
| else: | |
| priority = "medium" | |
| return { | |
| "sentiment": sentiment, | |
| "needs_followup": flag, | |
| "suggested_priority": priority, | |
| } | |
| if __name__ == "__main__": | |
| tests = [ | |
| (5, "no"), | |
| (4, "yes"), | |
| (3, "yes"), | |
| (2, "yes"), | |
| ] | |
| for r, f in tests: | |
| print(r, f, "->", analyze_feedback(r, f)) | |