count_by_emotion
Count and calculate percentages of Twitter messages labeled with specific emotions (sadness, joy, love, anger, fear, surprise) in the dair-ai/emotion dataset for statistical analysis.
Instructions
Count samples for a specific emotion.
Args: emotion: One of 'sadness', 'joy', 'love', 'anger', 'fear', 'surprise'
Returns: JSON string with count and percentage
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| emotion | Yes |
Implementation Reference
- server.py:79-107 (handler)Implementation of the `count_by_emotion` MCP tool, which counts dataset entries matching a specified emotion and returns the count and percentage as a JSON string.
@mcp.tool() def count_by_emotion(emotion: str) -> str: """Count samples for a specific emotion. Args: emotion: One of 'sadness', 'joy', 'love', 'anger', 'fear', 'surprise' Returns: JSON string with count and percentage """ emotion = emotion.lower().strip() if emotion not in EMOTION_TO_ID: return json.dumps({ "error": f"Invalid emotion. Choose from: {list(EMOTION_TO_ID.keys())}" }) emotion_id = EMOTION_TO_ID[emotion] dataset = get_dataset() count = sum(1 for sample in dataset if sample["label"] == emotion_id) percentage = (count / len(dataset)) * 100 return json.dumps({ "emotion": emotion, "count": count, "total": len(dataset), "percentage": round(percentage, 2) }, indent=2)