-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfident_analysis.py
More file actions
37 lines (28 loc) · 1.29 KB
/
confident_analysis.py
File metadata and controls
37 lines (28 loc) · 1.29 KB
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
from textblob import TextBlob
def analyze_text_confidence(text):
"""
Analyze the sentiment of the provided text, returning both the sentiment analysis and a derived confidence score.
The confidence score is a heuristic based on the polarity and subjectivity of the sentiment analysis.
Parameters:
- text (str): The text to analyze.
Returns:
- dict: A dictionary containing the sentiment analysis and a derived confidence score.
"""
# Create a TextBlob object for the given text
blob = TextBlob(text)
# Analyze the sentiment of the text
sentiment = blob.sentiment
# Derive a simple confidence score
# Here, we're considering the absolute value of polarity (for sentiment strength)
# and adjusting it by the objectivity (1 - subjectivity) for confidence
confidence = abs(sentiment.polarity) * (1 - sentiment.subjectivity)
# Returning the sentiment analysis results along with the derived confidence score
return {
"polarity": sentiment.polarity,
"subjectivity": sentiment.subjectivity,
"confidence": confidence
}
# Example text for analysis
text = "I love this product. It works amazingly well!"
analysis_result = analyze_text_confidence(text)
print(f"Analysis Result: {analysis_result}")