mcp-gradio-test / sentiment_tools.py
ritikjain51's picture
Upload 7 files
2ef9bf7 verified
raw
history blame contribute delete
947 Bytes
from textblob import TextBlob
def analyze_sentiment(text):
"""Analyze the sentiment of a given text.
Args:
text (str): The text to analyze
Returns:
dict: A dictionary containing polarity, subjectivity, and assessment
"""
blob = TextBlob(text)
return {
"polarity": round(blob.polarity, 2),
"subjectivity": round(blob.subjectivity, 2),
"assessment": "positive" if blob.polarity > 0 else "negative" if blob.polarity < 0 else "neutral"
}
def register_sentiment_tool(mcp):
"""Register the sentiment analysis tool with the MCP server."""
@mcp.tool()
def sentiment_analysis(text: str) -> dict:
"""
Analyze the sentiment of a given text.
Args:
text: The text to analyze
Returns:
A dictionary with polarity, subjectivity, and assessment
"""
return analyze_sentiment(text)