File size: 2,079 Bytes
74851dc
 
 
 
9b28320
74851dc
 
 
9b28320
74851dc
 
 
 
9b28320
74851dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# sentiment_tools.py - CrewAI Native Version
from crewai.tools import BaseTool
from typing import Type
from pydantic import BaseModel, Field

class SentimentInput(BaseModel):
    """Input schema for SentimentTool."""
    text: str = Field(..., description="Text to analyze for sentiment")

class SentimentTool(BaseTool):
    name: str = "Analyze Sentiment"
    description: str = "Analyzes the sentiment of a given text using keyword analysis"
    args_schema: Type[BaseModel] = SentimentInput

    def _run(self, text: str) -> str:
        try:
            # Simple sentiment analysis without heavy models for faster execution
            text_lower = text.lower()
            
            # Positive indicators
            positive_words = [
                'bull', 'bullish', 'up', 'rise', 'rising', 'gain', 'gains', 
                'positive', 'strong', 'growth', 'increase', 'rally', 'surge',
                'optimistic', 'good', 'great', 'excellent', 'buy', 'moon'
            ]
            
            # Negative indicators  
            negative_words = [
                'bear', 'bearish', 'down', 'fall', 'falling', 'loss', 'losses',
                'negative', 'weak', 'decline', 'decrease', 'crash', 'dump',
                'pessimistic', 'bad', 'poor', 'terrible', 'sell', 'fear'
            ]
            
            positive_count = sum(1 for word in positive_words if word in text_lower)
            negative_count = sum(1 for word in negative_words if word in text_lower)
            
            if positive_count > negative_count:
                confidence = min(0.9, 0.6 + (positive_count - negative_count) * 0.1)
                return f"Positive (confidence: {confidence:.1f})"
            elif negative_count > positive_count:
                confidence = min(0.9, 0.6 + (negative_count - positive_count) * 0.1)
                return f"Negative (confidence: {confidence:.1f})"
            else:
                return "Neutral (confidence: 0.5)"
                
        except Exception as e:
            return f"Sentiment analysis error: {str(e)}"