File size: 8,356 Bytes
198ccb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"""Thread length and sentiment correlation analysis."""

import logging
from typing import List, Dict, Optional, Tuple
import pandas as pd
import numpy as np
from scipy import stats
from sklearn.linear_model import LinearRegression

from analysis.sentiment_analyzer import SentimentAnalyzer

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class ThreadAnalyzer:
    """
    Analyze correlation between thread length and sentiment.
    
    Thread length is the number of comments under a news article.
    Temperature is the probability that a comment is negative.
    """
    
    def __init__(self, sentiment_analyzer: Optional[SentimentAnalyzer] = None):
        """
        Initialize thread analyzer.
        
        Args:
            sentiment_analyzer: SentimentAnalyzer instance (creates new if None)
        """
        if sentiment_analyzer is None:
            self.analyzer = SentimentAnalyzer()
        else:
            self.analyzer = sentiment_analyzer
    
    def calculate_thread_lengths(
        self,
        data: List[Dict],
        news_id_key: str = "news_id",
        comment_id_key: str = "id"
    ) -> Dict[str, int]:
        """
        Calculate thread length (number of comments) for each news item.
        
        Args:
            data: List of comment dictionaries
            news_id_key: Key for news ID in data dict
            comment_id_key: Key for comment ID in data dict
            
        Returns:
            Dictionary mapping news_id to thread length
            
        Example:
            >>> analyzer = ThreadAnalyzer()
            >>> data = [
            ...     {"news_id": 1, "id": 1},
            ...     {"news_id": 1, "id": 2},
            ...     {"news_id": 2, "id": 3},
            ... ]
            >>> lengths = analyzer.calculate_thread_lengths(data)
            >>> lengths[1]
            2
        """
        thread_lengths = {}
        
        for item in data:
            news_id = item.get(news_id_key)
            if news_id:
                thread_lengths[news_id] = thread_lengths.get(news_id, 0) + 1
        
        return thread_lengths
    
    def calculate_temperature(
        self,
        data: List[Dict],
        news_id_key: str = "news_id",
        text_key: str = "text"
    ) -> Dict[str, float]:
        """
        Calculate temperature (negative sentiment probability) for each news item.
        
        Temperature is the probability that a comment is negative.
        
        Args:
            data: List of comment dictionaries
            news_id_key: Key for news ID in data dict
            text_key: Key for text in data dict
            
        Returns:
            Dictionary mapping news_id to average temperature
        """
        # Group comments by news_id
        news_comments = {}
        for item in data:
            news_id = item.get(news_id_key)
            text = item.get(text_key)
            if news_id and text:
                if news_id not in news_comments:
                    news_comments[news_id] = []
                news_comments[news_id].append(text)
        
        # Calculate temperature for each news item
        temperatures = {}
        
        for news_id, texts in news_comments.items():
            # Analyze sentiment
            results = self.analyzer.analyze_batch(texts)
            
            # Calculate average temperature (probability of negative)
            negative_scores = []
            for result in results:
                label = result["label"]
                score = result["score"]
                
                if label == "NEGATIVE":
                    # High confidence negative = high temperature
                    negative_scores.append(score)
                elif label == "POSITIVE":
                    # High confidence positive = low temperature
                    negative_scores.append(1.0 - score)
                else:
                    # Neutral = medium temperature
                    negative_scores.append(0.5)
            
            avg_temperature = np.mean(negative_scores) if negative_scores else 0.5
            temperatures[news_id] = avg_temperature
        
        return temperatures
    
    def analyze_correlation(
        self,
        thread_lengths: Dict[str, int],
        temperatures: Dict[str, float]
    ) -> Dict:
        """
        Analyze correlation between thread length and temperature.
        
        Args:
            thread_lengths: Dictionary mapping news_id to thread length
            temperatures: Dictionary mapping news_id to temperature
            
        Returns:
            Dictionary with correlation statistics
        """
        # Get common news_ids
        common_ids = set(thread_lengths.keys()) & set(temperatures.keys())
        
        if len(common_ids) < 2:
            return {
                "correlation": 0.0,
                "p_value": 1.0,
                "significant": False,
                "sample_size": len(common_ids),
                "error": "Insufficient data for correlation analysis"
            }
        
        # Prepare data
        lengths = [thread_lengths[id] for id in common_ids]
        temps = [temperatures[id] for id in common_ids]
        
        # Calculate Pearson correlation
        correlation, p_value = stats.pearsonr(lengths, temps)
        
        # Linear regression
        X = np.array(lengths).reshape(-1, 1)
        y = np.array(temps)
        
        reg = LinearRegression()
        reg.fit(X, y)
        
        slope = reg.coef_[0]
        intercept = reg.intercept_
        r_squared = reg.score(X, y)
        
        return {
            "correlation": float(correlation),
            "p_value": float(p_value),
            "significant": p_value < 0.05,
            "sample_size": len(common_ids),
            "slope": float(slope),
            "intercept": float(intercept),
            "r_squared": float(r_squared),
            "interpretation": self._interpret_correlation(correlation, p_value)
        }
    
    def _interpret_correlation(self, correlation: float, p_value: float) -> str:
        """Interpret correlation results."""
        if p_value >= 0.05:
            return "No significant correlation (p >= 0.05)"
        
        if abs(correlation) < 0.1:
            strength = "negligible"
        elif abs(correlation) < 0.3:
            strength = "weak"
        elif abs(correlation) < 0.5:
            strength = "moderate"
        elif abs(correlation) < 0.7:
            strength = "strong"
        else:
            strength = "very strong"
        
        direction = "positive" if correlation > 0 else "negative"
        
        return f"{strength.capitalize()} {direction} correlation (r={correlation:.3f}, p={p_value:.4f})"
    
    def analyze_from_dataframe(
        self,
        df: pd.DataFrame,
        news_id_col: str = "news_id",
        text_col: str = "text"
    ) -> Tuple[pd.DataFrame, Dict]:
        """
        Analyze thread-sentiment correlation from DataFrame.
        
        Args:
            df: DataFrame with news_id and text columns
            news_id_col: Name of news_id column
            text_col: Name of text column
            
        Returns:
            Tuple of (DataFrame with thread stats, correlation results)
        """
        # Convert to list of dicts
        data = df[[news_id_col, text_col]].to_dict('records')
        
        # Calculate thread lengths and temperatures
        thread_lengths = self.calculate_thread_lengths(
            data,
            news_id_key=news_id_col
        )
        temperatures = self.calculate_temperature(
            data,
            news_id_key=news_id_col,
            text_key=text_col
        )
        
        # Analyze correlation
        correlation_results = self.analyze_correlation(thread_lengths, temperatures)
        
        # Create DataFrame with thread statistics
        common_ids = set(thread_lengths.keys()) & set(temperatures.keys())
        thread_stats = pd.DataFrame([
            {
                "news_id": news_id,
                "thread_length": thread_lengths[news_id],
                "temperature": temperatures[news_id]
            }
            for news_id in common_ids
        ])
        
        return thread_stats, correlation_results