speeches: end XXIV-XXVII speeches at the next RU_ anchor or the chair

Protocols of these periods mark speeches with RU_ anchors only, so no heading was found and
every speech ran to the end of its session protocol (~57k documents of up to 2.8 MB;
document table 11 GB, chunks 5.6 GB, nightly dump 0.4 -> 6+ GB). Segments longer than any
real speech (200k chars) are now dropped instead of stored. parser_version 2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
simonandClaude Opus 5.5 committed 2026-11-19 12:20:10 +01:00
1 parent 10c677ca70
commit 95355305d4
2 files changed
+30 -2

No files matched your search

@@ -25,6 +25,11 @@ from politrack.text import excerpt
PARAGRAPH = re.compile(r"<p\b[^>]*>.*?</p>", re.IGNORECASE | re.DOTALL)
HEADING_ANCHOR = re.compile(r'<a\s+name="T_[^"]+"', re.IGNORECASE)
# XXIV-XXVII: every speech starts with a time-stamp paragraph carrying an "RU_" anchor
SPEECH_ANCHOR = re.compile(r'<a\s+name="RU_[^"]+"', re.IGNORECASE)
# The presiding officer's interventions end a speech ("<p class=MsoNormal><b>... Präsident <a>Name</a>:</b> ...")
CHAIR = re.compile(r"^<p[^>]*>\s*<b>(?:\s*<span[^>]*>.*?</span>)?\s*(?:Pr&auml;sident|Präsident)", re.IGNORECASE | re.DOTALL)
MAX_SPEECH_CHARS = 200_000 # no plenary speech is longer; a longer segment means its end wasn't found
INTERJECTION = re.compile(r"\(([^()]{3,800}?)\)")
INTERJECTION_WORDS = re.compile(r"\b(Beifall|Ruf|Rufe|Zwischenruf|Heiterkeit|Abg\.|Abgeordnete[rn]?|Präsident|"
r"Unruhe|Widerspruch|Lachen|Bravo|Pfui|Zustimmung)\b")
@@ -60,7 +65,9 @@ def speech_segments(html: str, anchors: list[str]) -> dict[str, str]:
# Session protocols: every speech begins with a heading paragraph - the speaker's name in bold, with "T_"
# anchors before or after it ("<a name=T_...></a><b>Abgeordneter ...</b>" or "<b>Präsident<a ...>:</b>").
paragraphs = [(m.start(), m.end(), m.group(0)) for m in PARAGRAPH.finditer(html)]
headings = [start for start, _, p in paragraphs if HEADING_ANCHOR.search(p) and "<b" in p[:600].lower()]
headings = [start for start, _, p in paragraphs
if (HEADING_ANCHOR.search(p) and "<b" in p[:600].lower()) or SPEECH_ANCHOR.search(p)
or CHAIR.match(p)]
out = {}
for anchor in anchors:
m = re.search(r'(?:name|id)="' + re.escape(anchor) + '"', html)
@@ -68,7 +75,9 @@ def speech_segments(html: str, anchors: list[str]) -> dict[str, str]:
continue
start = max((s for s in headings if s <= m.start()), default=html.rfind("<p", 0, m.start()))
end = min((s for s in headings if s > m.start()), default=len(html))
out[anchor] = html_text(html[start:end])
text = html_text(html[start:end])
if len(text) <= MAX_SPEECH_CHARS:
out[anchor] = text
return out
@@ -76,6 +85,7 @@ def speech_segments(html: str, anchors: list[str]) -> dict[str, str]:
class SpeechTexts(Adapter):
spec = SPEC
name = "parlament.speeches"
parser_version = 2 # 2: speeches of XXIV-XXVII end at the next "RU_" speech or the chair (were whole protocols)
description = "Full texts and interjections of plenary speeches (stenographic protocols)"
schedule = "20 4 * * *"
queue = "bulk"