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:
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)
|
PARAGRAPH = re.compile(r"<p\b[^>]*>.*?</p>", re.IGNORECASE | re.DOTALL)
|
||||||
HEADING_ANCHOR = re.compile(r'<a\s+name="T_[^"]+"', re.IGNORECASE)
|
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ä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 = re.compile(r"\(([^()]{3,800}?)\)")
|
||||||
INTERJECTION_WORDS = re.compile(r"\b(Beifall|Ruf|Rufe|Zwischenruf|Heiterkeit|Abg\.|Abgeordnete[rn]?|Präsident|"
|
INTERJECTION_WORDS = re.compile(r"\b(Beifall|Ruf|Rufe|Zwischenruf|Heiterkeit|Abg\.|Abgeordnete[rn]?|Präsident|"
|
||||||
r"Unruhe|Widerspruch|Lachen|Bravo|Pfui|Zustimmung)\b")
|
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_"
|
# 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>").
|
# 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)]
|
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 = {}
|
out = {}
|
||||||
for anchor in anchors:
|
for anchor in anchors:
|
||||||
m = re.search(r'(?:name|id)="' + re.escape(anchor) + '"', html)
|
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
|
continue
|
||||||
start = max((s for s in headings if s <= m.start()), default=html.rfind("<p", 0, m.start()))
|
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))
|
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
|
return out
|
||||||
|
|
||||||
|
|
||||||
@@ -76,6 +85,7 @@ def speech_segments(html: str, anchors: list[str]) -> dict[str, str]:
|
|||||||
class SpeechTexts(Adapter):
|
class SpeechTexts(Adapter):
|
||||||
spec = SPEC
|
spec = SPEC
|
||||||
name = "parlament.speeches"
|
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)"
|
description = "Full texts and interjections of plenary speeches (stenographic protocols)"
|
||||||
schedule = "20 4 * * *"
|
schedule = "20 4 * * *"
|
||||||
queue = "bulk"
|
queue = "bulk"
|
||||||
|
|||||||
@@ -217,3 +217,21 @@ def test_election_preferences_eu_layouts():
|
|||||||
(1, "Lopatka, Dr. Reinhold", 2268, 58013, None, 3, "Bernhuber, Alexander", 44641)])]
|
(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", eu2019)] == [("Dr. Othmar Karas", 103035)]
|
||||||
assert [(c["name"], c["votes"]) for c in parse_preferences("eu", eu2024)] == [("Dr. Reinhold Lopatka", 58013)]
|
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 = """<html><body>
|
||||||
|
<p class=RB><a name="RU_1">9.50</a></p>
|
||||||
|
<p class=StandardRB><a name="R_1_9"><b>Abgeordnete <A HREF="/WWER/PAD_1/index.shtml">Anna Eins</A></b> (Grüne)</a>: Erste Rede.</p>
|
||||||
|
<p class=StandardRE>Danke. <i>(Beifall bei den Grünen.)</i></p>
|
||||||
|
<p class=MsoNormal><b><span lang=DE style='display:none'><!--x--></span>Präsident
|
||||||
|
<A HREF="/WWER/PAD_2/index.shtml">Max Vorsitz</A>:</b> Zu Wort gemeldet ist Abgeordneter Zwei.</p>
|
||||||
|
<p class=RB><a name="RU_2">9.55</a></p>
|
||||||
|
<p class=StandardRB><b>Abgeordneter Bernd Zwei</b> (SPÖ): Zweite Rede.</p>
|
||||||
|
""" + "<p>Weitere Tagesordnung.</p>" * 3000 + "</body></html>"
|
||||||
|
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
|
||||||
Reference in new issue
Block a user