File size: 1,240 Bytes
e201cb1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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))
|