File size: 13,281 Bytes
ec8f374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
"""
Gap Analyzer Module

Analyzes model performance to identify knowledge gaps and weak areas.
"""

from typing import List, Dict, Optional, Any, Tuple
import json
from pathlib import Path
from collections import defaultdict
import statistics


class GapAnalyzer:
    """
    Analyzes evaluation results to identify knowledge gaps.

    Features:
    - Topic-level performance analysis
    - Trend tracking across evaluations
    - Weakness identification
    - Strength identification
    - Improvement recommendations
    """

    def __init__(self):
        """Initialize gap analyzer."""
        self.evaluation_history: List[Dict[str, Any]] = []
        self.performance_by_category: Dict[str, List[float]] = defaultdict(list)
        self.gaps: List[Dict[str, Any]] = []

    def add_evaluation_results(self, results: Dict[str, Any]):
        """
        Add evaluation results for analysis.

        Args:
            results: Evaluation results dictionary
        """
        self.evaluation_history.append(results)

        # Extract category performance if available
        if 'examples' in results:
            category_scores = defaultdict(list)

            for example in results['examples']:
                category = example.get('category', 'general')

                # Calculate score for this example
                prediction = example.get('prediction', '').lower()
                reference = example.get('reference', '').lower()

                # Simple scoring: 1 if similar, 0 otherwise
                score = 1.0 if self._calculate_similarity(prediction, reference) > 0.5 else 0.0
                category_scores[category].append(score)

            # Store average scores by category
            for category, scores in category_scores.items():
                avg_score = (sum(scores) / len(scores)) * 100 if scores else 0
                self.performance_by_category[category].append(avg_score)

    def _calculate_similarity(self, text1: str, text2: str) -> float:
        """Calculate simple similarity between two texts."""
        words1 = set(text1.split())
        words2 = set(text2.split())

        if not words1 or not words2:
            return 0.0

        intersection = words1 & words2
        union = words1 | words2

        return len(intersection) / len(union) if union else 0.0

    def analyze_gaps(
        self,
        weak_threshold: float = 60.0,
        strong_threshold: float = 85.0
    ) -> List[Dict[str, Any]]:
        """
        Analyze performance and identify gaps.

        Args:
            weak_threshold: Score below this is considered weak
            strong_threshold: Score above this is considered strong

        Returns:
            List of identified gaps with details
        """
        gaps = []

        # Analyze each category
        for category, scores in self.performance_by_category.items():
            if not scores:
                continue

            avg_score = statistics.mean(scores)
            latest_score = scores[-1] if scores else 0

            # Calculate trend
            trend = "stable"
            if len(scores) >= 2:
                recent_avg = statistics.mean(scores[-3:]) if len(scores) >= 3 else statistics.mean(scores[-2:])
                older_avg = statistics.mean(scores[:-3]) if len(scores) >= 3 else scores[0]

                if recent_avg > older_avg + 5:
                    trend = "improving"
                elif recent_avg < older_avg - 5:
                    trend = "declining"

            # Classify performance level
            if avg_score < weak_threshold:
                level = "WEAK"
                priority = "HIGH"
            elif avg_score < strong_threshold:
                level = "MODERATE"
                priority = "MEDIUM"
            else:
                level = "STRONG"
                priority = "LOW"

            gap = {
                'category': category,
                'avg_score': avg_score,
                'latest_score': latest_score,
                'num_evaluations': len(scores),
                'trend': trend,
                'level': level,
                'priority': priority,
                'scores_history': scores
            }

            gaps.append(gap)

        # Sort by priority (weak areas first)
        priority_order = {'HIGH': 0, 'MEDIUM': 1, 'LOW': 2}
        gaps.sort(key=lambda x: (priority_order.get(x['priority'], 3), x['avg_score']))

        self.gaps = gaps
        return gaps

    def get_weakest_topics(self, n: int = 5) -> List[Dict[str, Any]]:
        """
        Get the N weakest topics.

        Args:
            n: Number of topics to return

        Returns:
            List of weakest topics
        """
        if not self.gaps:
            self.analyze_gaps()

        weak_gaps = [g for g in self.gaps if g['level'] in ['WEAK', 'MODERATE']]
        return weak_gaps[:n]

    def get_strongest_topics(self, n: int = 5) -> List[Dict[str, Any]]:
        """
        Get the N strongest topics.

        Args:
            n: Number of topics to return

        Returns:
            List of strongest topics
        """
        if not self.gaps:
            self.analyze_gaps()

        strong_gaps = [g for g in self.gaps if g['level'] == 'STRONG']
        return strong_gaps[:n]

    def get_declining_topics(self) -> List[Dict[str, Any]]:
        """Get topics with declining performance."""
        if not self.gaps:
            self.analyze_gaps()

        return [g for g in self.gaps if g['trend'] == 'declining']

    def get_improving_topics(self) -> List[Dict[str, Any]]:
        """Get topics with improving performance."""
        if not self.gaps:
            self.analyze_gaps()

        return [g for g in self.gaps if g['trend'] == 'improving']

    def generate_gap_report(self) -> str:
        """
        Generate a human-readable gap analysis report.

        Returns:
            Formatted report string
        """
        if not self.gaps:
            self.analyze_gaps()

        report = ["=" * 80]
        report.append("KNOWLEDGE GAP ANALYSIS REPORT")
        report.append("=" * 80)
        report.append("")

        # Overall summary
        weak_count = sum(1 for g in self.gaps if g['level'] == 'WEAK')
        moderate_count = sum(1 for g in self.gaps if g['level'] == 'MODERATE')
        strong_count = sum(1 for g in self.gaps if g['level'] == 'STRONG')

        report.append(f"Total Categories Analyzed: {len(self.gaps)}")
        report.append(f"  - WEAK (needs immediate attention): {weak_count}")
        report.append(f"  - MODERATE (needs improvement): {moderate_count}")
        report.append(f"  - STRONG (performing well): {strong_count}")
        report.append("")

        # Weak areas (priority)
        weak_topics = [g for g in self.gaps if g['level'] == 'WEAK']
        if weak_topics:
            report.append("πŸ”΄ WEAK AREAS (Priority Training Needed):")
            report.append("-" * 80)
            for gap in weak_topics:
                report.append(f"  β€’ {gap['category']}: {gap['avg_score']:.1f}% (Trend: {gap['trend']})")
            report.append("")

        # Moderate areas
        moderate_topics = [g for g in self.gaps if g['level'] == 'MODERATE']
        if moderate_topics:
            report.append("🟑 MODERATE AREAS (Recommended Improvement):")
            report.append("-" * 80)
            for gap in moderate_topics[:5]:  # Top 5
                report.append(f"  β€’ {gap['category']}: {gap['avg_score']:.1f}% (Trend: {gap['trend']})")
            report.append("")

        # Strong areas
        strong_topics = [g for g in self.gaps if g['level'] == 'STRONG']
        if strong_topics:
            report.append("🟒 STRONG AREAS (Excellent Performance):")
            report.append("-" * 80)
            for gap in strong_topics[:5]:  # Top 5
                report.append(f"  β€’ {gap['category']}: {gap['avg_score']:.1f}% (Trend: {gap['trend']})")
            report.append("")

        # Trends
        declining = self.get_declining_topics()
        improving = self.get_improving_topics()

        if declining:
            report.append("πŸ“‰ DECLINING PERFORMANCE (Needs Attention):")
            report.append("-" * 80)
            for gap in declining:
                report.append(f"  β€’ {gap['category']}: {gap['avg_score']:.1f}%")
            report.append("")

        if improving:
            report.append("πŸ“ˆ IMPROVING PERFORMANCE (Keep It Up!):")
            report.append("-" * 80)
            for gap in improving:
                report.append(f"  β€’ {gap['category']}: {gap['avg_score']:.1f}%")
            report.append("")

        report.append("=" * 80)

        return "\n".join(report)

    def get_performance_summary(self) -> Dict[str, Any]:
        """
        Get overall performance summary.

        Returns:
            Summary statistics
        """
        if not self.gaps:
            self.analyze_gaps()

        all_scores = [g['avg_score'] for g in self.gaps]

        summary = {
            'num_categories': len(self.gaps),
            'overall_avg_score': statistics.mean(all_scores) if all_scores else 0,
            'min_score': min(all_scores) if all_scores else 0,
            'max_score': max(all_scores) if all_scores else 0,
            'weak_count': sum(1 for g in self.gaps if g['level'] == 'WEAK'),
            'moderate_count': sum(1 for g in self.gaps if g['level'] == 'MODERATE'),
            'strong_count': sum(1 for g in self.gaps if g['level'] == 'STRONG'),
            'declining_count': sum(1 for g in self.gaps if g['trend'] == 'declining'),
            'improving_count': sum(1 for g in self.gaps if g['trend'] == 'improving')
        }

        return summary

    def export_gaps(self, filepath: str):
        """
        Export gap analysis to JSON file.

        Args:
            filepath: Output file path
        """
        if not self.gaps:
            self.analyze_gaps()

        Path(filepath).parent.mkdir(parents=True, exist_ok=True)

        data = {
            'summary': self.get_performance_summary(),
            'gaps': self.gaps,
            'report': self.generate_gap_report()
        }

        with open(filepath, 'w', encoding='utf-8') as f:
            json.dump(data, f, indent=2, ensure_ascii=False)

        print(f"Gap analysis exported to: {filepath}")

    def load_gaps(self, filepath: str):
        """
        Load gap analysis from JSON file.

        Args:
            filepath: Input file path
        """
        with open(filepath, 'r', encoding='utf-8') as f:
            data = json.load(f)

        self.gaps = data.get('gaps', [])

        # Reconstruct performance_by_category
        for gap in self.gaps:
            category = gap['category']
            scores = gap.get('scores_history', [])
            self.performance_by_category[category] = scores

    def compare_evaluations(
        self,
        eval1: Dict[str, Any],
        eval2: Dict[str, Any]
    ) -> Dict[str, Any]:
        """
        Compare two evaluation results.

        Args:
            eval1: First evaluation results
            eval2: Second evaluation results

        Returns:
            Comparison details
        """
        comparison = {
            'improvement': {},
            'decline': {},
            'stable': {}
        }

        # Extract metrics from both
        metrics1 = eval1.get('metrics', {})
        metrics2 = eval2.get('metrics', {})

        # Compare each metric
        for metric in set(metrics1.keys()) | set(metrics2.keys()):
            if metric in metrics1 and metric in metrics2:
                val1 = metrics1[metric]
                val2 = metrics2[metric]

                if isinstance(val1, (int, float)) and isinstance(val2, (int, float)):
                    diff = val2 - val1
                    percent_change = (diff / val1 * 100) if val1 != 0 else 0

                    if diff > 1:  # Improved
                        comparison['improvement'][metric] = {
                            'old': val1,
                            'new': val2,
                            'change': diff,
                            'percent_change': percent_change
                        }
                    elif diff < -1:  # Declined
                        comparison['decline'][metric] = {
                            'old': val1,
                            'new': val2,
                            'change': diff,
                            'percent_change': percent_change
                        }
                    else:  # Stable
                        comparison['stable'][metric] = {
                            'old': val1,
                            'new': val2,
                            'change': diff
                        }

        return comparison

    def get_category_details(self, category: str) -> Optional[Dict[str, Any]]:
        """
        Get detailed analysis for a specific category.

        Args:
            category: Category name

        Returns:
            Category details or None if not found
        """
        if not self.gaps:
            self.analyze_gaps()

        for gap in self.gaps:
            if gap['category'] == category:
                return gap

        return None