Spaces:
Running
Running
Create hoax_filter.py
Browse files- hoax_filter.py +157 -0
hoax_filter.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# hoax_filter.py
|
2 |
+
# Lightweight, stateless misinformation heuristics for language/source/scale
|
3 |
+
|
4 |
+
import re
|
5 |
+
from urllib.parse import urlparse
|
6 |
+
from dataclasses import dataclass
|
7 |
+
from typing import Dict, Any, Optional, Tuple, List
|
8 |
+
|
9 |
+
_NUMBER_UNIT = re.compile(
|
10 |
+
r'(?P<num>[\d,]+(?:\.\d+)?)\s*(?P<unit>mile|miles|km|kilometer|kilometers)',
|
11 |
+
re.I
|
12 |
+
)
|
13 |
+
|
14 |
+
LANG_RED_FLAGS = [
|
15 |
+
r'\brecently\s+declassified\b',
|
16 |
+
r'\bshocking\b',
|
17 |
+
r'\bastonishing\b',
|
18 |
+
r'\bexplosive\b',
|
19 |
+
r'\bexperts\s+say\b',
|
20 |
+
r'\breportedly\b',
|
21 |
+
r'\bmothership\b',
|
22 |
+
r'\bancient\s+alien\b',
|
23 |
+
r'\bdormant\s+(?:observational\s+)?craft\b',
|
24 |
+
r'\bangular\s+edges\b',
|
25 |
+
r'\bviral\b',
|
26 |
+
r'\bnever\s+before\s+seen\b',
|
27 |
+
r'\bshaking\s+(?:the\s+)?scientific\s+community\b',
|
28 |
+
r'\bfootage\b',
|
29 |
+
]
|
30 |
+
|
31 |
+
# Trusted primary sources (add/remove as you like)
|
32 |
+
ALLOW_DOMAINS = {
|
33 |
+
'nasa.gov', 'jpl.nasa.gov', 'pds.nasa.gov', 'science.nasa.gov', 'heasarc.gsfc.nasa.gov',
|
34 |
+
'esa.int', 'esawebservices.esa.int', 'esa-maine.esa.int',
|
35 |
+
'noirlab.edu', 'cfa.harvard.edu', 'caltech.edu', 'berkeley.edu', 'mit.edu',
|
36 |
+
'nature.com', 'science.org', 'iopscience.iop.org', 'agu.org',
|
37 |
+
'arxiv.org', 'adsabs.harvard.edu',
|
38 |
+
}
|
39 |
+
|
40 |
+
# High-virality social/video platforms: treat as high risk for scientific “scoops”
|
41 |
+
DENY_DOMAINS = {
|
42 |
+
'm.facebook.com', 'facebook.com', 'x.com', 'twitter.com', 't.co',
|
43 |
+
'tiktok.com', 'youtube.com', 'youtu.be', 'instagram.com', 'reddit.com',
|
44 |
+
}
|
45 |
+
|
46 |
+
# Medium-risk tabloid/aggregator examples (tune to preference)
|
47 |
+
MEDIUM_DOMAINS = {
|
48 |
+
'dailyMail.co.uk', 'dailymail.co.uk', 'newyorkpost.com', 'the-sun.com',
|
49 |
+
'mirror.co.uk', 'sputniknews.com', 'rt.com',
|
50 |
+
}
|
51 |
+
|
52 |
+
@dataclass
|
53 |
+
class HoaxFilterResult:
|
54 |
+
red_flag_hits: int
|
55 |
+
source_score: float
|
56 |
+
scale_score: float
|
57 |
+
combined: float
|
58 |
+
notes: Dict[str, Any]
|
59 |
+
|
60 |
+
class HoaxFilter:
|
61 |
+
"""
|
62 |
+
Scores are in [0,1]; higher means more likely hoax/misinformation.
|
63 |
+
"""
|
64 |
+
|
65 |
+
def __init__(self,
|
66 |
+
red_flag_weight: float = 0.35,
|
67 |
+
source_weight: float = 0.25,
|
68 |
+
scale_weight: float = 0.40,
|
69 |
+
extraordinary_km: float = 50.0):
|
70 |
+
"""
|
71 |
+
extraordinary_km: any single claimed length >= this is 'extraordinary'.
|
72 |
+
Adjust to tighten/loosen sensitivity (100–500 for stricter).
|
73 |
+
"""
|
74 |
+
self.red_flag_weight = red_flag_weight
|
75 |
+
self.source_weight = source_weight
|
76 |
+
self.scale_weight = scale_weight
|
77 |
+
self.extraordinary_km = extraordinary_km
|
78 |
+
self._flag_res = [re.compile(p, re.I) for p in LANG_RED_FLAGS]
|
79 |
+
|
80 |
+
@staticmethod
|
81 |
+
def _km_from_match(num: str, unit: str) -> float:
|
82 |
+
n = float(num.replace(',', ''))
|
83 |
+
if unit.lower().startswith('mile'):
|
84 |
+
return n * 1.609344
|
85 |
+
return n
|
86 |
+
|
87 |
+
def language_red_flags(self, text: str) -> Tuple[int, List[str]]:
|
88 |
+
hits = []
|
89 |
+
for rx in self._flag_res:
|
90 |
+
if rx.search(text):
|
91 |
+
hits.append(rx.pattern)
|
92 |
+
return len(hits), hits
|
93 |
+
|
94 |
+
def source_heuristic(self, url: Optional[str]) -> Tuple[float, str]:
|
95 |
+
"""
|
96 |
+
Returns (risk, note). risk in [0,1]; higher is worse.
|
97 |
+
"""
|
98 |
+
if not url:
|
99 |
+
return 0.5, "no_source"
|
100 |
+
host = urlparse(url).netloc.lower()
|
101 |
+
|
102 |
+
# Strip common subdomains to compare base domains
|
103 |
+
parts = host.split(':')[0].split('.')
|
104 |
+
base = '.'.join(parts[-2:]) if len(parts) >= 2 else host
|
105 |
+
|
106 |
+
if host in ALLOW_DOMAINS or base in ALLOW_DOMAINS:
|
107 |
+
return 0.05, f"allow:{host}"
|
108 |
+
if host in DENY_DOMAINS or base in DENY_DOMAINS:
|
109 |
+
return 0.85, f"deny:{host}"
|
110 |
+
if host in MEDIUM_DOMAINS or base in MEDIUM_DOMAINS:
|
111 |
+
return 0.7, f"medium:{host}"
|
112 |
+
return 0.6, f"unknown:{host}"
|
113 |
+
|
114 |
+
def scale_check(self, text: str, context_keywords: Optional[List[str]] = None) -> Tuple[float, Dict]:
|
115 |
+
"""
|
116 |
+
Parse lengths and judge extraordinariness, boosting risk when context
|
117 |
+
suggests planetary/astronomical claims.
|
118 |
+
"""
|
119 |
+
context_keywords = context_keywords or []
|
120 |
+
sizes_km = []
|
121 |
+
for m in _NUMBER_UNIT.finditer(text):
|
122 |
+
sizes_km.append(self._km_from_match(m.group('num'), m.group('unit')))
|
123 |
+
|
124 |
+
if not sizes_km:
|
125 |
+
return 0.0, {"sizes_km": []}
|
126 |
+
|
127 |
+
max_km = max(sizes_km)
|
128 |
+
extraordinary_context = any(k in text.lower() for k in context_keywords)
|
129 |
+
ratio = max_km / max(self.extraordinary_km, 1.0)
|
130 |
+
base = min(ratio, 1.0) # saturate at 1.0
|
131 |
+
if extraordinary_context:
|
132 |
+
base = min(1.0, base * 1.25) # slight boost in relevant context
|
133 |
+
return base, {"sizes_km": sizes_km, "max_km": max_km, "extraordinary_context": extraordinary_context}
|
134 |
+
|
135 |
+
def score(self, text: str, url: Optional[str] = None,
|
136 |
+
context_keywords: Optional[List[str]] = None) -> HoaxFilterResult:
|
137 |
+
rf_count, rf_hits = self.language_red_flags(text)
|
138 |
+
rf_score = min(rf_count / 4.0, 1.0)
|
139 |
+
|
140 |
+
src_risk, src_note = self.source_heuristic(url)
|
141 |
+
scale_risk, scale_notes = self.scale_check(text, context_keywords=context_keywords)
|
142 |
+
|
143 |
+
combined = (self.red_flag_weight * rf_score
|
144 |
+
+ self.source_weight * src_risk
|
145 |
+
+ self.scale_weight * scale_risk)
|
146 |
+
|
147 |
+
return HoaxFilterResult(
|
148 |
+
red_flag_hits=rf_count,
|
149 |
+
source_score=src_risk,
|
150 |
+
scale_score=scale_risk,
|
151 |
+
combined=min(combined, 1.0),
|
152 |
+
notes={
|
153 |
+
"red_flag_patterns": rf_hits,
|
154 |
+
"source": src_note,
|
155 |
+
**scale_notes
|
156 |
+
}
|
157 |
+
)
|