diff --git a/backend/politrack/sources/parlament/speeches.py b/backend/politrack/sources/parlament/speeches.py index ff26774..2d58a61 100644 --- a/backend/politrack/sources/parlament/speeches.py +++ b/backend/politrack/sources/parlament/speeches.py @@ -25,6 +25,11 @@ from politrack.text import excerpt PARAGRAPH = re.compile(r"
]*>.*?
", re.IGNORECASE | re.DOTALL) HEADING_ANCHOR = re.compile(r'... Präsident Name: ...") +CHAIR = re.compile(r"^]*>\s*(?:\s*]*>.*?)?\s*(?:Prä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 ("Abgeordneter ..." or "Präsident:"). 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 " dict[str, str]: continue start = max((s for s in headings if s <= m.start()), default=html.rfind("
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" diff --git a/backend/tests/test_parsers.py b/backend/tests/test_parsers.py index 48005fa..2575cb4 100644 --- a/backend/tests/test_parsers.py +++ b/backend/tests/test_parsers.py @@ -217,3 +217,21 @@ def test_election_preferences_eu_layouts(): (1, "Lopatka, Dr. Reinhold", 2268, 58013, None, 3, "Bernhuber, Alexander", 44641)])] assert [(c["name"], c["votes"]) for c in parse_preferences("eu", eu2019)] == [("Dr. Othmar Karas", 103035)] assert [(c["name"], c["votes"]) for c in parse_preferences("eu", eu2024)] == [("Dr. Reinhold Lopatka", 58013)] + + +def test_speech_segments_end_at_next_ru_anchor_or_chair(): + """XXIV-XXVII protocols mark speeches with "RU_" anchors only; a speech used to run to the end of the file.""" + from politrack.sources.parlament.speeches import speech_segments + html = """
+ +Abgeordnete Anna Eins (Grüne): Erste Rede.
+Danke. (Beifall bei den Grünen.)
+Präsident +Max Vorsitz: Zu Wort gemeldet ist Abgeordneter Zwei.
+ +Abgeordneter Bernd Zwei (SPÖ): Zweite Rede.
+""" + "Weitere Tagesordnung.
" * 3000 + "" + seg = speech_segments(html, ["RU_1", "RU_2"]) + assert seg["RU_1"].startswith("Abgeordnete Anna Eins (Grüne): Erste Rede.") and "Vorsitz" not in seg["RU_1"] + assert seg["RU_2"].startswith("Abgeordneter Bernd Zwei (SPÖ): Zweite Rede.") + assert "RU_2" in seg and len(seg["RU_2"]) < 200_000 # the rest of the session protocol is not one speech