Repository navigation
Expand file tree
/
Copy pathtranscript.py
More file actions
57 lines (45 loc) · 1.94 KB
/
Copy pathtranscript.py
File metadata and controls
57 lines (45 loc) · 1.94 KB
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
"""Parse rap battle transcript files."""
import re
def parse_battle(text):
"""Parse a battle transcript into structured data.
Args:
text: Full text content of a battle .txt file.
Returns:
(model_left, model_right, rounds) where rounds is a list of
(author, text) tuples. System rounds (e.g. "Final round!") are
included with author "system".
"""
if not text or not text.strip():
raise ValueError("battle transcript is empty")
lines = text.splitlines()
match = re.fullmatch(r"#\s+(\S+/\S+)\s+v\s+(\S+/\S+)\s*", lines[0])
if not match:
raise ValueError("battle transcript must start with '# <model> v <model>'")
model_left = match.group(1).strip()
model_right = match.group(2).strip()
participants = {model_left, model_right}
rounds = []
current_author = None
current_lines = []
for line in lines[1:]:
header = re.fullmatch(r">\s*(\S+)\s*", line)
author = header.group(1) if header else None
if author == "system" or author in participants:
if current_author is not None:
round_text = "\n".join(current_lines).strip()
if not round_text:
raise ValueError(f"round by {current_author!r} is empty")
rounds.append((current_author, round_text))
elif any(previous_line.strip() for previous_line in current_lines):
raise ValueError("transcript contains text before the first author header")
current_author = author
current_lines = []
else:
current_lines.append(line)
if current_author is None:
raise ValueError("battle transcript contains no author rounds")
round_text = "\n".join(current_lines).strip()
if not round_text:
raise ValueError(f"round by {current_author!r} is empty")
rounds.append((current_author, round_text))
return model_left, model_right, rounds