File size: 2,891 Bytes
876b12f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
import os
from typing import Dict, Any, List

logger = logging.getLogger(__name__)

class BiasAnalyzer:
    def __init__(self):
        self.resources_dir = os.path.join(os.path.dirname(__file__), '..', 'resources')
        self.left_keywords = self._load_keywords('left_bias_words.txt')
        self.right_keywords = self._load_keywords('right_bias_words.txt')

    def _load_keywords(self, filename: str) -> List[str]:
        """Load keywords from file."""
        try:
            filepath = os.path.join(self.resources_dir, filename)
            with open(filepath, 'r', encoding='utf-8') as f:
                return [line.strip().lower() for line in f if line.strip() and not line.startswith('#')]
        except Exception as e:
            logger.error(f"Error loading {filename}: {str(e)}")
            return []

    def analyze(self, text: str) -> Dict[str, Any]:
        """Detect bias using keyword analysis."""
        try:
            text_lower = text.lower()
            
            # Count matches
            left_count = sum(1 for word in self.left_keywords if word in text_lower)
            right_count = sum(1 for word in self.right_keywords if word in text_lower)
            
            total_words = left_count + right_count
            if total_words == 0:
                return {
                    "bias": "Neutral",
                    "bias_score": 0.0,  # True neutral
                    "bias_percentage": 0  # Neutral percentage
                }
            
            # New bias score formula (-1.0 left, 0.0 neutral, 1.0 right)
            bias_score = (right_count - left_count) / total_words
            
            # Convert bias_score to percentage (-100% to +100%)
            bias_percentage = bias_score * 100
            logger.info(f"Bias score: {bias_score:.2f}, Bias percentage: {bias_percentage:.1f}%")
            
            # Determine bias label
            if bias_score < -0.8:
                bias = "Strongly Left"
            elif bias_score < -0.5:
                bias = "Moderately Left"
            elif bias_score < -0.2:
                bias = "Leaning Left"
            elif bias_score > 0.8:
                bias = "Strongly Right"
            elif bias_score > 0.5:
                bias = "Moderately Right"
            elif bias_score > 0.2:
                bias = "Leaning Right"
            else:
                bias = "Neutral"
            
            return {
                "bias": bias,
                "bias_score": round(bias_score, 2),  # Keep 2 decimal places
                "bias_percentage": abs(round(bias_percentage, 1))
            }
            
        except Exception as e:
            logger.error(f"Error in bias analysis: {str(e)}")
            return {
                "bias": "Error",
                "bias_score": 0.0,
                "bias_percentage": 0
            }