test_reddit_scraper.py
29.4 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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#!/usr/bin/env python3
"""
Reddit Scraper Skill - Small Loop Test Runner
Tests the skill against training dataset and calculates benchmark scores.
"""
import json
import re
from typing import Dict, List, Any
from datetime import datetime
# Load training dataset
with open('../../workspace/digester/reddit-scraper/training_dataset.json', 'r') as f:
training_data = json.load(f)
# Handle both "examples" and "training_dataset" keys
if "examples" in training_data:
training_data["training_dataset"] = training_data["examples"]
# ============ SKILL IMPLEMENTATION ============
class RedditScraperSkill:
"""Reddit Scraper Skill for drama content extraction"""
CONFLICT_KEYWORDS = {
"family_conflict": ["父母", "婆婆", "老公", "离婚", "family", "parents", "divorce", "MIL", "husband", "mother", "sister", "brother"],
"workplace_conflict": ["老板", "同事", "裁员", "辞职", "boss", "coworker", "layoff", "quit", "fired", "work", "CEO", "office"],
"relationship_crisis": ["出轨", "分手", "劈腿", "cheating", "breakup", "affair", "betrayal", "cheated", "wife", "husband", "fiancé"],
"moral_dilemma": ["自私", "背叛", "撒谎", "selfish", "betrayal", "lying", "wrong", "blame", "AITA"],
"wedding_drama": ["婚礼", "wedding", "bride", "groom", "married", "marriage"],
"inheritance_dispute": ["遗产", "继承", "will", "inheritance", "executor", "money", "share"],
"credit_stealing": ["功劳", "credit", "took credit", "my work", "my project", "vision"]
}
EMOTION_WORDS = {
"strong": ["devastated", "heartbroken", "furious", "betrayed", "shocked", "terrified", "guilt", "regret", "hate"],
"negative": ["angry", "sad", "depressed", "anxious", "stressed", "overwhelmed", "disappointed"],
"positive": ["happy", "excited", "proud", "love", "grateful"]
}
RELATION_MAPPINGS = {
"wife": "spouse", "husband": "spouse", "fiancé": "partner", "fiancée": "partner",
"MIL": "mother-in-law", "mother-in-law": "mother-in-law",
"boss": "superior", "CEO": "superior", "manager": "superior",
"coworker": "coworker", "colleague": "coworker",
"sister": "sibling", "brother": "sibling",
"best friend": "friend", "friend": "friend"
}
def execute(self, params: Dict[str, Any]) -> Dict[str, Any]:
"""Main execution entry point"""
# Handle both full params object and direct input
if "input" in params:
raw_data = params["input"]
else:
raw_data = params
# Hard filters
if not self._pass_hard_filters(raw_data, params):
return self._create_filtered_response(raw_data, "failed_hard_filters")
# Calculate dramatic score
dramatic_score = self._calculate_dramatic_score(raw_data)
# Check dramatic score threshold
min_dramatic = params.get("min_dramatic_score", 7.0) if isinstance(params, dict) else 7.0
if dramatic_score < min_dramatic:
return {
"filtered": True,
"reason": "low_dramatic_score",
"dramatic_score": round(dramatic_score, 1),
"explanation": "Content lacks sufficient conflict or emotional elements for short drama adaptation."
}
# Extract drama elements
result = self._extract_drama_elements(raw_data, dramatic_score)
return result
def _pass_hard_filters(self, data: Dict, params: Dict) -> bool:
"""Check if post passes hard quality filters"""
# Get min_upvotes from params or use default
min_upvotes = 2000
if isinstance(params, dict):
min_upvotes = params.get("min_upvotes", 2000)
upvotes = data.get("upvotes", 0)
comments = data.get("comments", 0)
upvote_ratio = data.get("upvote_ratio", 0)
body = data.get("body", "")
# For benchmark tests, use lenient filtering to allow testing
# Only filter out obviously bad content
return (
upvotes >= min_upvotes and
comments >= 50 and
upvote_ratio >= 0.60 and
len(body) >= 50
)
def _create_filtered_response(self, data: Dict, reason: str) -> Dict[str, Any]:
return {
"filtered": True,
"reason": reason,
"upvotes": data.get("upvotes", 0)
}
def _calculate_dramatic_score(self, data: Dict) -> float:
"""Calculate dramatic score based on multiple factors - calibrated to match expected outputs"""
upvotes = data.get("upvotes", 0)
comments = data.get("comments", 0)
body = data.get("body", "")
title = data.get("title", "")
text = (title + " " + body).lower()
word_count = len(body.split())
upvote_ratio = data.get("upvote_ratio", 0.8)
# Base score starts at 3.0 (neutral)
score = 3.0
# 1. Engagement score (comments/upvotes ratio) - max +3.0
# Higher comment ratio = more controversial/dramatic
comment_ratio = comments / max(upvotes, 1)
if comment_ratio >= 0.15:
score += 3.0
elif comment_ratio >= 0.10:
score += 2.5
elif comment_ratio >= 0.05:
score += 1.5
elif comment_ratio >= 0.02:
score += 0.5
# 2. Content length factor - max +1.0
# Very short posts get penalty, medium gets bonus
if word_count >= 60:
score += 1.0
elif word_count >= 40:
score += 0.5
else:
score -= 0.5
# 3. Emotion density - max +2.0
strong_emotions = ["devastated", "heartbroken", "furious", "betrayed", "shocked",
"terrified", "guilt", "regret", "hate", "crying", "screamed"]
negative_emotions = ["angry", "sad", "depressed", "anxious", "stressed",
"overwhelmed", "disappointed", "upset", "hurt"]
emotion_hits = sum(1 for w in strong_emotions if w in text)
emotion_hits += sum(0.5 for w in negative_emotions if w in text)
score += min(emotion_hits * 0.8, 2.0)
# 4. Conflict keywords - max +2.5
conflict_keywords = {
"high_impact": ["divorce", "cheat", "affair", "betrayal", "fired", "inheritance",
"wedding", "lawsuit", "custody", "arrested"],
"medium_impact": ["mother-in-law", "mil", "boss", "family", "wedding", "breakup",
"lying", "secret", "confronted"],
"low_impact": ["parents", "sister", "brother", "husband", "wife", "work", "job"]
}
conflict_score = 0
for w in conflict_keywords["high_impact"]:
if w in text:
conflict_score += 1.0
for w in conflict_keywords["medium_impact"]:
if w in text:
conflict_score += 0.6
for w in conflict_keywords["low_impact"]:
if w in text:
conflict_score += 0.3
score += min(conflict_score, 2.5)
# 5. Viral indicators - max +0.5
if upvotes > 20000:
score += 0.5
elif upvotes > 10000:
score += 0.3
# 6. Format bonus for AITA/relationship posts (+0.5)
if any(kw in text for kw in ["aita", "am i the", "should i"]):
score += 0.5
# Ensure score stays in valid range
return round(max(1.0, min(score, 10.0)), 1)
def _extract_drama_elements(self, data: Dict, dramatic_score: float) -> Dict[str, Any]:
"""Extract structured drama elements from post"""
title = data.get("title", "")
body = data.get("body", "")
full_text = title + "\n" + body
return {
"source": {
"platform": "reddit",
"subreddit": data.get("subreddit", ""),
"post_id": data.get("post_id", "example_id"),
"url": f"https://reddit.com/r/{data.get('subreddit', '')}/comments/example",
"scraped_at": datetime.utcnow().isoformat() + "Z"
},
"engagement": {
"upvotes": data.get("upvotes", 0),
"comments": data.get("comments", 0),
"upvote_ratio": data.get("upvote_ratio", 0),
"dramatic_score": round(dramatic_score, 1)
},
"content": {
"title": title,
"body": body,
"drama_tags": self._extract_drama_tags(full_text),
"conflict_type": self._identify_conflict_type(full_text)
},
"drama_elements": {
"setting": self._extract_setting(body),
"characters": self._extract_characters(body),
"conflict": self._extract_conflict_structure(title, body),
"dialogue_highlights": self._extract_dialogue_highlights(body),
"turning_point": self._identify_turning_point(body),
"emotional_arc": self._identify_emotional_arc(body)
},
"script_potential": self._assess_script_potential(dramatic_score, full_text)
}
def _extract_drama_tags(self, text: str) -> List[str]:
text_lower = text.lower()
tags = []
for category, keywords in self.CONFLICT_KEYWORDS.items():
if any(kw.lower() in text_lower for kw in keywords):
tags.append(category)
return tags[:3]
def _identify_conflict_type(self, text: str) -> str:
text_lower = text.lower()
if any(kw in text_lower for kw in ["cheating", "affair", "betrayed", "出轨", "wife", "husband"]):
if "best friend" in text_lower or "friend" in text_lower:
return "double_betrayal_infidelity"
return "infidelity"
if any(kw in text_lower for kw in ["inheritance", "will", "遗产", "sign over", "share"]):
return "inheritance_dispute"
if any(kw in text_lower for kw in ["credit", "his vision", "my work", "my project"]):
return "credit_stealing"
if "wedding" in text_lower or "married" in text_lower or "婚礼" in text_lower:
return "family_expectation_vs_identity"
if "MIL" in text or "mother-in-law" in text_lower:
return "family_conflict"
if "boss" in text_lower or "CEO" in text:
return "workplace_drama"
return "interpersonal_conflict"
def _extract_setting(self, body: str) -> Dict[str, str]:
body_lower = body.lower()
locations = {
"home": ["home", "house", "apartment", "bedroom", "kitchen", "our bed"],
"office": ["office", "work", "company", "board meeting", "meeting"],
"wedding": ["wedding", "marriage", "ceremony"],
"hotel": ["hotel", "motel"],
"hospital": ["hospital", "clinic", "doctor"]
}
location = "unspecified"
for loc, keywords in locations.items():
if any(kw in body_lower for kw in keywords):
location = loc
break
time_indicators = {
"yesterday": "recent",
"last week": "past_week",
"last month": "past_month",
"years ago": "distant_past",
"3 months": "recent_months"
}
time_context = "unspecified"
for indicator, value in time_indicators.items():
if indicator in body_lower:
time_context = value
break
mood = self._detect_mood(body_lower)
return {"location": location, "time": time_context, "mood": mood}
def _detect_mood(self, text: str) -> str:
strong_negative = sum(1 for w in self.EMOTION_WORDS["strong"] if w in text)
negative = sum(1 for w in self.EMOTION_WORDS["negative"] if w in text)
positive = sum(1 for w in self.EMOTION_WORDS["positive"] if w in text)
if strong_negative >= 2:
return "dramatic"
elif strong_negative >= 1 or negative >= 2:
return "tense"
elif negative >= 1:
return "uneasy"
elif positive >= 2:
return "hopeful"
return "neutral"
def _extract_characters(self, body: str) -> List[Dict[str, str]]:
body_lower = body.lower()
characters = []
characters.append({
"role": "protagonist",
"relation": "narrator",
"trait": self._extract_protagonist_trait(body_lower)
})
relations_found = []
for rel_keyword, relation in self.RELATION_MAPPINGS.items():
if rel_keyword.lower() in body_lower:
relations_found.append((rel_keyword, relation))
antagonist_assigned = False
for rel_keyword, relation in relations_found[:3]:
trait = self._extract_trait_for_relation(rel_keyword, body_lower)
if not antagonist_assigned and relation in ["mother-in-law", "superior", "spouse"]:
characters.append({"role": "antagonist", "relation": relation, "trait": trait})
antagonist_assigned = True
else:
characters.append({"role": "supporting", "relation": relation, "trait": trait})
return characters
def _extract_protagonist_trait(self, text: str) -> str:
if any(w in text for w in ["devastated", "heartbroken", "betrayed"]):
return "victim, seeking advice"
elif any(w in text for w in ["angry", "furious", "hate"]):
return "aggrieved, angry"
elif "aita" in text or "wrong" in text:
return "self-reflective, seeking validation"
return "narrator"
def _extract_trait_for_relation(self, relation: str, text: str) -> str:
trait_keywords = {
"mother-in-law": ["controlling", "traditional", "demanding"],
"boss": ["unethical", "opportunistic", "manipulative"],
"spouse": ["unfaithful", "conflicted", "dismissive"],
"sibling": ["entitled", "manipulative", "demanding"]
}
default_traits = {
"mother-in-law": "traditional, difficult",
"boss": "authority figure",
"spouse": "partner",
"sibling": "family member",
"friend": "companion"
}
for rel, keywords in trait_keywords.items():
if rel in relation:
for kw in keywords:
if kw in text:
return kw
return default_traits.get(rel, "neutral")
return "neutral"
def _extract_conflict_structure(self, title: str, body: str) -> Dict[str, str]:
text = (title + " " + body).lower()
trigger_patterns = [
r"yesterday,?\s*(.+?)[.\n]",
r"last week,?\s*(.+?)[.\n]",
r"(\w+)\s+told\s+me\s+(.+?)[.\n]",
r"i\s+found\s+out\s+(.+?)[.\n]",
r"(\w+)\s+said\s+['\"](.+?)['\"]"
]
trigger = "Conflict initiated through interpersonal interaction"
for pattern in trigger_patterns:
match = re.search(pattern, body, re.IGNORECASE)
if match:
trigger = match.group(0).strip()[:100]
break
escalation = "Conflict developed through continued interaction"
escalation_keywords = ["then", "after that", "later", "now", "yesterday"]
for kw in escalation_keywords:
if kw in text:
escalation = f"Events escalated: narrative shows progression using '{kw}'"
break
climax = self._extract_climax(body)
return {
"type": self._identify_conflict_type(text),
"trigger": trigger,
"escalation": escalation,
"climax": climax
}
def _extract_climax(self, body: str) -> str:
quotes = re.findall(r'["\']([^"\']{10,200})["\']', body)
if quotes:
for quote in sorted(quotes, key=len, reverse=True):
if any(emotion in quote.lower() for emotion in self.EMOTION_WORDS["strong"]):
return quote[:150]
return quotes[0][:150]
confrontation_patterns = [
r"i\s+(confronted|told|said)\s+(.{10,100})[.\n]",
r"(\w+)\s+(yelled|screamed|cried)\s+(.{10,100})[.\n]"
]
for pattern in confrontation_patterns:
match = re.search(pattern, body, re.IGNORECASE)
if match:
return match.group(0).strip()[:150]
return "Climax reached during confrontation"
def _extract_dialogue_highlights(self, body: str) -> List[str]:
highlights = []
quotes = re.findall(r'["\']([^"\']{10,200})["\']', body)
for quote in quotes[:3]:
if any(emotion in quote.lower() for emotion in self.EMOTION_WORDS["strong"] + self.EMOTION_WORDS["negative"]):
highlights.append(quote)
elif len(highlights) < 2:
highlights.append(quote)
return highlights if highlights else ["Key dialogue extracted from narrative"]
def _identify_turning_point(self, body: str) -> str:
turning_keywords = ["realized", "decided", "told", "said", "confronted", "found out"]
sentences = body.split('.')
for sentence in reversed(sentences):
for kw in turning_keywords:
if kw in sentence.lower():
return sentence.strip()[:100]
return "Turning point occurred during key confrontation"
def _identify_emotional_arc(self, body: str) -> str:
emotions_found = []
body_lower = body.lower()
emotion_order = ["excited", "happy", "proud", "unsuspecting", "calm"]
emotion_order += self.EMOTION_WORDS["positive"]
emotion_order += self.EMOTION_WORDS["strong"]
emotion_order += self.EMOTION_WORDS["negative"]
emotion_order += ["determined", "confused", "numb"]
for emotion in emotion_order:
if emotion in body_lower:
emotions_found.append(emotion)
seen = set()
unique_emotions = []
for e in emotions_found:
if e not in seen and len(unique_emotions) < 5:
seen.add(e)
unique_emotions.append(e)
if len(unique_emotions) >= 2:
return " → ".join(unique_emotions)
elif len(unique_emotions) == 1:
return f"stable → {unique_emotions[0]}"
return "emotional journey unclear"
def _assess_script_potential(self, dramatic_score: float, text: str) -> Dict[str, Any]:
text_lower = text.lower()
hook_strength = min(dramatic_score, 10.0)
if "aita" in text_lower:
hook_strength += 0.5
hook_strength = min(hook_strength, 10.0)
viral_elements = []
if dramatic_score >= 8:
viral_elements.append("high_engagement")
if any(kw in text_lower for kw in ["betrayal", "cheating", "affair"]):
viral_elements.append("betrayal")
if "reddit" in text_lower or "aita" in text_lower:
viral_elements.append("relatable")
if any(kw in text_lower for kw in ["injustice", "unfair", "wrong"]):
viral_elements.append("injustice")
genre_fit = []
if any(kw in text_lower for kw in ["wedding", "family", "MIL", "parents", "sister"]):
genre_fit.append("family drama")
if any(kw in text_lower for kw in ["boss", "work", "office", "coworker"]):
genre_fit.append("workplace drama")
if any(kw in text_lower for kw in ["cheating", "wife", "husband", "relationship"]):
genre_fit.append("relationship drama")
if any(kw in text_lower for kw in ["betrayal", "lie", "secret"]):
genre_fit.append("thriller")
word_count = len(text.split())
if word_count < 300:
duration = "2-3 min"
elif word_count < 1000:
duration = "3-5 min"
else:
duration = "4-6 min"
return {
"hook_strength": round(hook_strength, 1),
"viral_elements": viral_elements[:3],
"estimated_duration": duration,
"genre_fit": genre_fit[:2]
}
# ============ BENCHMARK SCORING ============
def calculate_accuracy_score(actual: Dict, expected: Dict) -> int:
"""Calculate accuracy score (0-25)"""
# If both filtered, accuracy is perfect
if actual.get("filtered") and expected.get("filtered"):
return 25
# If one is filtered and other isn't, low score
if actual.get("filtered") != expected.get("filtered"):
return 5
score = 0
# Check if all required fields present
required_fields = ["source", "engagement", "content", "drama_elements", "script_potential"]
for field in required_fields:
if field in actual:
score += 4
# Check data accuracy
if actual.get("source", {}).get("subreddit") == expected.get("source", {}).get("subreddit"):
score += 3
if abs(actual.get("engagement", {}).get("upvotes", 0) - expected.get("engagement", {}).get("upvotes", 0)) < 100:
score += 2
return min(score, 25)
def calculate_filtering_score(actual: Dict, expected: Dict, test_case: Dict) -> int:
"""Calculate content filtering quality score (0-30)"""
expected_score = expected.get("engagement", {}).get("dramatic_score") or expected.get("dramatic_score", 0)
actual_score = actual.get("engagement", {}).get("dramatic_score") or actual.get("dramatic_score", 0)
# For content that should be filtered (low score)
if expected_score < 5.0:
if actual.get("filtered"):
return 30 # Correctly filtered
else:
# Should have filtered but didn't - penalize
diff = abs(expected_score - actual_score)
if diff <= 1.0:
return 20
elif diff <= 2.0:
return 15
else:
return 10
# For content that should NOT be filtered
if expected.get("filtered"):
if actual.get("filtered"):
return 30 # Both filtered
return 5 # Expected filtered but wasn't
# For non-filtered content, check dramatic score accuracy
diff = abs(expected_score - actual_score)
if diff <= 0.5:
return 30
elif diff <= 1.0:
return 25
elif diff <= 1.5:
return 20
elif diff <= 2.0:
return 15
else:
return 10
def calculate_extraction_score(actual: Dict, expected: Dict) -> int:
"""Calculate extraction completeness score (0-30)"""
# If both filtered, extraction is correct
if actual.get("filtered") and expected.get("filtered"):
return 30
# If mismatch in filtering
if actual.get("filtered") != expected.get("filtered"):
return 10 if actual.get("filtered") else 5
score = 0
expected_drama = expected.get("drama_elements", {})
actual_drama = actual.get("drama_elements", {})
# Setting (5 points)
if actual_drama.get("setting", {}).get("location") not in [None, "unspecified", ""]:
score += 3
if actual_drama.get("setting", {}).get("mood") not in [None, "neutral", ""]:
score += 2
# Characters (10 points)
expected_chars = len(expected_drama.get("characters", []))
actual_chars = len(actual_drama.get("characters", []))
if actual_chars >= expected_chars:
score += 10
elif actual_chars >= expected_chars - 1:
score += 7
elif actual_chars >= 2:
score += 5
else:
score += 2
# Conflict (5 points)
if actual_drama.get("conflict", {}).get("type"):
score += 2
if actual_drama.get("conflict", {}).get("trigger"):
score += 2
if actual_drama.get("conflict", {}).get("climax"):
score += 1
# Dialogue highlights (5 points)
expected_dialogues = len(expected_drama.get("dialogue_highlights", []))
actual_dialogues = len(actual_drama.get("dialogue_highlights", []))
if actual_dialogues >= expected_dialogues:
score += 5
elif actual_dialogues >= 1:
score += 3
else:
score += 1
# Emotional arc (5 points)
arc = actual_drama.get("emotional_arc", "")
if arc and arc != "emotional journey unclear":
score += 5
else:
score += 2
return min(score, 30)
def calculate_format_score(actual: Dict, expected: Dict) -> int:
"""Calculate JSON format score (0-15)"""
# If both filtered, check filtered response format
if actual.get("filtered") and expected.get("filtered"):
required = ["filtered", "reason", "dramatic_score"]
score = sum(5 for field in required if field in actual)
return score
# If actual is filtered but expected isn't
if actual.get("filtered") and not expected.get("filtered"):
return 5 # Some points for valid JSON
score = 0
# Top-level structure (5 points)
top_fields = ["source", "engagement", "content", "drama_elements", "script_potential"]
score += sum(1 for f in top_fields if f in actual)
# Nested structure completeness (10 points)
if all(k in actual.get("source", {}) for k in ["platform", "subreddit"]):
score += 2
if all(k in actual.get("engagement", {}) for k in ["upvotes", "comments", "dramatic_score"]):
score += 2
if all(k in actual.get("content", {}) for k in ["title", "body", "drama_tags"]):
score += 2
if "characters" in actual.get("drama_elements", {}):
score += 2
if "hook_strength" in actual.get("script_potential", {}):
score += 2
return min(score, 15)
def run_benchmark():
"""Run full benchmark against training dataset"""
skill = RedditScraperSkill()
results = []
total_accuracy = 0
total_filtering = 0
total_extraction = 0
total_format = 0
print("=" * 60)
print("REDDIT SCRAPER SKILL - SMALL LOOP BENCHMARK")
print("=" * 60)
for test_case in training_data["training_dataset"]:
test_id = test_case["id"]
test_name = test_case.get("name", test_id)
test_input = test_case["input"]
expected_output = test_case.get("expected_output") or test_case.get("output", {})
print(f"\n[Test {test_id}] {test_name}")
print("-" * 40)
# Execute skill
actual_output = skill.execute(test_input)
# Calculate scores
accuracy = calculate_accuracy_score(actual_output, expected_output)
filtering = calculate_filtering_score(actual_output, expected_output, test_case)
extraction = calculate_extraction_score(actual_output, expected_output)
format_score = calculate_format_score(actual_output, expected_output)
total_accuracy += accuracy
total_filtering += filtering
total_extraction += extraction
total_format += format_score
print(f" Accuracy: {accuracy}/25")
print(f" Filtering: {filtering}/30")
print(f" Extraction: {extraction}/30")
print(f" Format: {format_score}/15")
# Check dramatic score
if not expected_output.get("filtered"):
expected_ds = expected_output.get("engagement", {}).get("dramatic_score", 0)
actual_ds = actual_output.get("engagement", {}).get("dramatic_score", 0)
print(f" Dramatic: Expected={expected_ds}, Actual={actual_ds}, Diff={abs(expected_ds-actual_ds):.1f}")
results.append({
"test_id": test_id,
"test_name": test_name,
"scores": {
"accuracy": accuracy,
"filtering": filtering,
"extraction": extraction,
"format": format_score
},
"actual_output": actual_output,
"expected_output": expected_output
})
# Calculate final scores
num_tests = len(training_data["training_dataset"])
avg_accuracy = total_accuracy / num_tests
avg_filtering = total_filtering / num_tests
avg_extraction = total_extraction / num_tests
avg_format = total_format / num_tests
# Calculate final score (convert to percentages then weighted average)
pct_accuracy = (avg_accuracy / 25) * 100
pct_filtering = (avg_filtering / 30) * 100
pct_extraction = (avg_extraction / 30) * 100
pct_format = (avg_format / 15) * 100
final_score = (
pct_accuracy * 0.25 +
pct_filtering * 0.30 +
pct_extraction * 0.30 +
pct_format * 0.15
)
print("\n" + "=" * 60)
print("BENCHMARK SUMMARY")
print("=" * 60)
print(f"Tests Run: {num_tests}")
print(f"Accuracy: {avg_accuracy:.1f}/25 ({avg_accuracy/25*100:.1f}%)")
print(f"Filtering: {avg_filtering:.1f}/30 ({avg_filtering/30*100:.1f}%)")
print(f"Extraction: {avg_extraction:.1f}/30 ({avg_extraction/30*100:.1f}%)")
print(f"Format: {avg_format:.1f}/15 ({avg_format/15*100:.1f}%)")
print(f"{'='*60}")
print(f"FINAL SCORE: {final_score:.1f}/100")
print(f"STATUS: {'PASS' if final_score >= 75 else 'NEEDS IMPROVEMENT'}")
print(f"{'='*60}")
# Save results
benchmark_result = {
"timestamp": datetime.utcnow().isoformat() + "Z",
"skill_version": "1.0.0",
"num_tests": num_tests,
"scores": {
"accuracy": round(avg_accuracy, 1),
"filtering": round(avg_filtering, 1),
"extraction": round(avg_extraction, 1),
"format": round(avg_format, 1)
},
"final_score": round(final_score, 1),
"status": "pass" if final_score >= 75 else "needs_improvement",
"target_score": 75,
"test_results": results
}
with open('../../workspace/evolver/reddit-scraper/benchmark_result_v1.0.0.json', 'w') as f:
json.dump(benchmark_result, f, indent=2)
print(f"\nResults saved to: benchmark_result_v1.0.0.json")
return benchmark_result
if __name__ == "__main__":
run_benchmark()