File size: 2,522 Bytes
b1acf7e |
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
"""
Text sentiment analysis model using TextBlob.
"""
import logging
from typing import Tuple, Optional
from ..config.settings import TEXT_MODEL_CONFIG
logger = logging.getLogger(__name__)
def predict_text_sentiment(text: str) -> Tuple[str, float]:
"""
Analyze text sentiment using TextBlob.
Args:
text: Input text to analyze
Returns:
Tuple of (sentiment, confidence)
"""
if not text or text.strip() == "":
return "No text provided", 0.0
try:
from textblob import TextBlob
# Create TextBlob object
blob = TextBlob(text)
# Get polarity (-1 to 1, where -1 is very negative, 1 is very positive)
polarity = blob.sentiment.polarity
# Get subjectivity (0 to 1, where 0 is very objective, 1 is very subjective)
subjectivity = blob.sentiment.subjectivity
# Convert polarity to sentiment categories
confidence_threshold = TEXT_MODEL_CONFIG["confidence_threshold"]
if polarity > confidence_threshold:
sentiment = "Positive"
confidence = min(0.95, 0.6 + abs(polarity) * 0.3)
elif polarity < -confidence_threshold:
sentiment = "Negative"
confidence = min(0.95, 0.6 + abs(polarity) * 0.3)
else:
sentiment = "Neutral"
confidence = 0.7 - abs(polarity) * 0.2
# Round confidence to 2 decimal places
confidence = round(confidence, 2)
logger.info(
f"Text sentiment analysis completed: {sentiment} (confidence: {confidence})"
)
return sentiment, confidence
except ImportError:
logger.error(
"TextBlob not installed. Please install it with: pip install textblob"
)
return "TextBlob not available", 0.0
except Exception as e:
logger.error(f"Error in text sentiment analysis: {str(e)}")
return "Error occurred", 0.0
def get_text_model_info() -> dict:
"""Get information about the text sentiment model."""
return {
"model_name": TEXT_MODEL_CONFIG["model_name"],
"description": "Natural Language Processing based sentiment analysis using TextBlob",
"capabilities": [
"Text sentiment classification (Positive/Negative/Neutral)",
"Confidence scoring",
"Real-time analysis",
"No external API required",
],
"input_format": "Plain text",
"output_format": "Sentiment label + confidence score",
}
|