-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment_analyzer.py
More file actions
67 lines (56 loc) · 2.3 KB
/
sentiment_analyzer.py
File metadata and controls
67 lines (56 loc) · 2.3 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
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
from dotenv import load_dotenv
import os
from langchain_ollama import OllamaLLM
from langchain_core.prompts import PromptTemplate
import logging
from datetime import datetime
# Configurar logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('sentiment_analysis.log', encoding='utf-8'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
class SentimentAnalyzer:
SENTIMENT_CODES = {
'POSITIVE': 1,
'NEUTRAL': 0,
'NEGATIVE': -1
}
def __init__(self, batch_size=50):
"""Inicializa el analizador de sentimientos."""
load_dotenv()
self.llm = OllamaLLM(
model='llama3.2',
base_url=os.getenv('OLLAMA_BASE_URL', 'http://localhost:11434'),
temperature=0.1
)
self.batch_size = batch_size
self.prompt_template = PromptTemplate(
input_variables=["title"],
template="""Analyze the sentiment of this news title:
"{title}"
Classify it into one of these categories:
- POSITIVE: For positive, optimistic, or uplifting content
- NEUTRAL: For factual, balanced, or objective content
- NEGATIVE: For negative, critical, or concerning content
Respond with ONLY ONE WORD: POSITIVE, NEUTRAL, or NEGATIVE."""
)
def analyze_sentiment(self, title: str) -> tuple[int, str]:
"""Analiza el sentimiento de un título y retorna (código, sentimiento)."""
try:
prompt = self.prompt_template.format(title=title)
response = self.llm.invoke(prompt).strip().upper()
# Validar respuesta
if response in self.SENTIMENT_CODES:
logger.info(f"Título: '{title[:100]}...' → Sentimiento: {response}")
return self.SENTIMENT_CODES[response], response
# Por defecto, retornar NEUTRAL
logger.warning(f"[RESPUESTA INVÁLIDA] '{response}' para título: '{title[:100]}...' → Asignando: NEUTRAL")
return self.SENTIMENT_CODES['NEUTRAL'], 'NEUTRAL'
except Exception as e:
logger.error(f"Error analizando título '{title}': {str(e)}")
return self.SENTIMENT_CODES['NEUTRAL'], 'NEUTRAL'