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 | def process_article(aid: str, raw_html: str, meta: dict):
soup = BeautifulSoup(ftfy.fix_text(raw_html), "html.parser")
article = soup.select_one("article")
if not article:
return None
first_div = article.find("div", recursive=False)
container = first_div if first_div else article
h1 = soup.select_one("h1")
title = h1.get_text(" ", strip=True) if h1 else meta.get("title", "")
sentences, html_parts, labels, title_labels = [], [], [], []
first_elem = True
for el in collect_elements(container):
if is_trailing_element(el):
break
# For figures: strip "Image source,X" attribution, keep only the caption
if el.name == "figure":
figcap = el.find("figcaption")
if not figcap:
continue
# Remove attribution spans (visually-hidden "Image source,..." nodes)
import copy as _copy
cap = _copy.deepcopy(figcap)
for span in cap.find_all(True):
if "image source" in span.get_text(" ", strip=True).lower()[:20]:
span.decompose()
text = cap.get_text(" ", strip=True)
else:
text = el.get_text(" ", strip=True)
if not text:
continue
# Skip promotional headings and continue collecting content.
if el.name in ("h2", "h3", "h4") and is_noise_heading(text):
continue
# Link-only boilerplate paragraphs (e.g. "Read more here")
if el.name == "p" and text.lower().rstrip(".") in SKIP_PARA_TEXT:
continue
# Mid-article promo paragraphs (e.g. "The documentary is available…")
if el.name == "p" and any(text.lower().startswith(p)
for p in SKIP_PARA_PREFIXES):
continue
# BBC series and brand promos
if any(text.lower().startswith(p) for p in SKIP_ANY_PREFIXES):
continue
is_heading = (el.name in ("h2", "h3", "h4") and len(text) <= 200)
sentences.append(text)
html_parts.append({"tag": el.name, "html": clean_inner_html(el)})
if is_heading:
labels.append(0 if first_elem else 1)
title_labels.append(1)
else:
labels.append(0)
title_labels.append(0)
first_elem = False
# Remove trailing headings without following content.
while sentences and title_labels[-1] == 1:
sentences.pop()
html_parts.pop()
labels.pop()
title_labels.pop()
if not sentences:
return None
return {
"articleId": aid,
"outlet": "BBC",
"title": title,
"url": meta.get("url", ""),
"sentences": sentences,
"html_parts": html_parts,
"labels": labels,
"title_labels": title_labels,
"num_sections": labels.count(1) + 1,
}
|