menikev commited on
Commit
74851dc
·
verified ·
1 Parent(s): 475ab32

Update sentiment_tools.py

Browse files
Files changed (1) hide show
  1. sentiment_tools.py +44 -42
sentiment_tools.py CHANGED
@@ -1,45 +1,47 @@
1
- from langchain.tools import tool
2
- from transformers import pipeline
 
 
3
 
4
- from langchain.tools import tool
 
 
5
 
6
- @tool("Analyze Sentiment")
7
- def analyze_sentiment(text: str) -> str:
8
- """Analyzes the sentiment of a given text using simple keyword analysis."""
9
- try:
10
- # Simple sentiment analysis without heavy models for faster execution
11
- text_lower = text.lower()
12
-
13
- # Positive indicators
14
- positive_words = [
15
- 'bull', 'bullish', 'up', 'rise', 'rising', 'gain', 'gains',
16
- 'positive', 'strong', 'growth', 'increase', 'rally', 'surge',
17
- 'optimistic', 'good', 'great', 'excellent', 'buy', 'moon'
18
- ]
19
-
20
- # Negative indicators
21
- negative_words = [
22
- 'bear', 'bearish', 'down', 'fall', 'falling', 'loss', 'losses',
23
- 'negative', 'weak', 'decline', 'decrease', 'crash', 'dump',
24
- 'pessimistic', 'bad', 'poor', 'terrible', 'sell', 'fear'
25
- ]
26
-
27
- positive_count = sum(1 for word in positive_words if word in text_lower)
28
- negative_count = sum(1 for word in negative_words if word in text_lower)
29
-
30
- if positive_count > negative_count:
31
- confidence = min(0.9, 0.6 + (positive_count - negative_count) * 0.1)
32
- return f"Positive (confidence: {confidence:.1f})"
33
- elif negative_count > positive_count:
34
- confidence = min(0.9, 0.6 + (negative_count - positive_count) * 0.1)
35
- return f"Negative (confidence: {confidence:.1f})"
36
- else:
37
- return "Neutral (confidence: 0.5)"
38
-
39
- except Exception as e:
40
- return f"Sentiment analysis error: {str(e)}"
41
 
42
- class SentimentTools:
43
- """Container class for sentiment tools - for backward compatibility"""
44
- def __init__(self):
45
- self.analyze_sentiment = analyze_sentiment
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # sentiment_tools.py - CrewAI Native Version
2
+ from crewai.tools import BaseTool
3
+ from typing import Type
4
+ from pydantic import BaseModel, Field
5
 
6
+ class SentimentInput(BaseModel):
7
+ """Input schema for SentimentTool."""
8
+ text: str = Field(..., description="Text to analyze for sentiment")
9
 
10
+ class SentimentTool(BaseTool):
11
+ name: str = "Analyze Sentiment"
12
+ description: str = "Analyzes the sentiment of a given text using keyword analysis"
13
+ args_schema: Type[BaseModel] = SentimentInput
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ def _run(self, text: str) -> str:
16
+ try:
17
+ # Simple sentiment analysis without heavy models for faster execution
18
+ text_lower = text.lower()
19
+
20
+ # Positive indicators
21
+ positive_words = [
22
+ 'bull', 'bullish', 'up', 'rise', 'rising', 'gain', 'gains',
23
+ 'positive', 'strong', 'growth', 'increase', 'rally', 'surge',
24
+ 'optimistic', 'good', 'great', 'excellent', 'buy', 'moon'
25
+ ]
26
+
27
+ # Negative indicators
28
+ negative_words = [
29
+ 'bear', 'bearish', 'down', 'fall', 'falling', 'loss', 'losses',
30
+ 'negative', 'weak', 'decline', 'decrease', 'crash', 'dump',
31
+ 'pessimistic', 'bad', 'poor', 'terrible', 'sell', 'fear'
32
+ ]
33
+
34
+ positive_count = sum(1 for word in positive_words if word in text_lower)
35
+ negative_count = sum(1 for word in negative_words if word in text_lower)
36
+
37
+ if positive_count > negative_count:
38
+ confidence = min(0.9, 0.6 + (positive_count - negative_count) * 0.1)
39
+ return f"Positive (confidence: {confidence:.1f})"
40
+ elif negative_count > positive_count:
41
+ confidence = min(0.9, 0.6 + (negative_count - positive_count) * 0.1)
42
+ return f"Negative (confidence: {confidence:.1f})"
43
+ else:
44
+ return "Neutral (confidence: 0.5)"
45
+
46
+ except Exception as e:
47
+ return f"Sentiment analysis error: {str(e)}"