Spaces:
Sleeping
Sleeping
File size: 4,847 Bytes
f078461 |
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 |
import tldextract
import re
from multiprocessing import Pool, cpu_count
from tqdm import tqdm
# Trusted domains
TRUSTED_DOMAINS = {
# π International Mainstream News
"abcnews.go.com",
"aljazeera.com",
"apnews.com",
"bbc.com",
"bloomberg.com",
"cbc.ca",
"cbsnews.com",
"cnn.com",
"dw.com",
"economist.com",
"euronews.com",
"forbes.com",
"ft.com",
"indiatimes.com",
"japantimes.co.jp",
"latimes.com",
"npr.org",
"nytimes.com",
"reuters.com",
"smh.com.au",
"theguardian.com",
"usatoday.com",
"washingtonpost.com",
"wsj.com",
"france24.com",
# π° Ghana-Specific News
"3news.com",
"adomonline.com",
"citinewsroom.com",
"ghanaweb.com",
"ghanaiantimes.com.gh",
"ghananewsagency.org",
"graphic.com.gh",
"modernghana.com",
"myjoyonline.com",
"peacefmonline.com",
"pulse.com.gh",
"starrfm.com.gh",
"thebftonline.com",
"yen.com.gh",
"nsmq.com.gh",
# β½ Sports News
"cbssports.com",
"espn.com",
"eurosport.com",
"fifa.com",
"footballghana.com",
"foxsports.com",
"ghanasoccernet.com",
"goal.com",
"nba.com",
"nbcsports.com",
"onefootball.com",
"skysports.com",
"sportinglife.com",
"supersport.com",
"tntsports.co.uk",
"theathletic.com",
"olympics.com",
# π¬ Entertainment & Pop Culture
"billboard.com",
"deadline.com",
"entertainment.com",
"eonline.com",
"ew.com",
"hollywoodreporter.com",
"indiewire.com",
"people.com",
"rollingstone.com",
"thewrap.com",
"variety.com",
# π§ͺ Science & Research
"eurekalert.org",
"medpagetoday.com",
"nasa.gov",
"nature.com",
"sciencealert.com",
"sciencenews.org",
"statnews.com",
# π Fact-Checking & Watchdogs
"africacheck.org",
"factcheck.org",
"fullfact.org",
"politifact.com",
"snopes.com",
# π Global & General Niche News
"asia.nikkei.com",
"globalissues.org",
"ipsnews.net",
"oecdobserver.org",
"rferl.org",
# π° African Regional News (non-Ghana)
"dailynation.africa",
"enca.com",
"ewn.co.za",
"monitor.co.ug",
"thecitizen.co.tz",
"businessinsider.com",
"africanews.com",
# π Academic & Policy Think Tanks
"brookings.edu",
"carnegieendowment.org",
"cfr.org",
"foreignpolicy.com",
"theconversation.com",
}
# Suspicious domains that often spread misinformation
SUSPICIOUS_DOMAINS = {
"beforeitsnews.com",
"naturalnews.com",
"infowars.com",
"breitbart.com",
"dailystormer.com",
"zerohedge.com",
"activistpost.com",
"realfarmacy.com",
"healthnutnews.com",
}
def extract_domain(url):
"""Extract domain from URL"""
ext = tldextract.extract(url)
return f"{ext.domain}.{ext.suffix}"
_PATTERNS = [
(re.compile(r"\b[A-Z]+\s*\(Reuters\)\s*[-ββ]?\s*", re.IGNORECASE), ""),
(re.compile(r"\(Reuters\)", re.IGNORECASE), ""),
(re.compile(r"Reuters", re.IGNORECASE), ""),
(
re.compile(
r"\b(?:WASHINGTON|NEW YORK|LONDON|PARIS|BERLIN|TOKYO|MOSCOW|BEIJING|DELHI)\s*[-ββ]?\s*",
re.IGNORECASE,
),
"",
),
(re.compile(r"\b(?:AP|CNN|BBC|Fox News|NBC|CBS|ABC News)\b", re.IGNORECASE), ""),
(re.compile(r"\bBy\s+[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*\b", re.IGNORECASE), ""),
(re.compile(r"\S+@\S+\.\S+"), ""),
(re.compile(r"http[s]?://\S+"), ""),
(re.compile(r"[^a-zA-Z\s]"), " "),
(re.compile(r"\s+"), " "),
]
def remove_source_artifacts_fast(text):
"""Optimized version of source artifact removal"""
if not isinstance(text, str) or len(text) < 10:
return ""
for pattern, replacement in _PATTERNS:
text = pattern.sub(replacement, text)
return text.strip().lower()
def _process_text_chunk(text_chunk):
"""Internal helper to process a chunk of texts in parallel"""
return [remove_source_artifacts_fast(text) for text in text_chunk]
def parallel_preprocess(texts, n_jobs=None):
"""Parallel preprocessing of texts using multiprocessing"""
if n_jobs is None:
n_jobs = min(cpu_count(), 8)
chunk_size = max(1, len(texts) // n_jobs)
chunks = [texts[i : i + chunk_size] for i in range(0, len(texts), chunk_size)]
print(
f"Processing {len(texts)} texts in {len(chunks)} chunks using {n_jobs} processes..."
)
with Pool(n_jobs) as pool:
results = list(
tqdm(
pool.imap(_process_text_chunk, chunks),
total=len(chunks),
desc="Preprocessing chunks",
)
)
processed_texts = []
for chunk_result in results:
processed_texts.extend(chunk_result)
return processed_texts
|