-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpptparserv1.py
77 lines (66 loc) · 2.26 KB
/
pptparserv1.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from .pptparser import PPTParser
from .pt import Turn, Subnode, PT
CHANGE_ROLE = {"user": "assistant", "assistant": "user"}
SIGN_TO_SUBNODETYPE = {"+": "upvoted", "-": "downvoted", "*": "writing", "?": "unrated"}
class PPTParserV1(PPTParser):
def loads(self, text: str):
pt: PT = []
lines = text.splitlines()
def read_body() -> str:
first_line = lines.pop(0)
read_lines = [first_line]
while lines:
line = lines[0]
if line and line[0] == ":":
lines.pop(0)
read_lines.append(line[1:])
else:
break
return "\n".join(read_lines)
# First turn should be user's
body = read_body()
turn = Turn(role="user", main=body)
def push_turn(turn: Turn) -> Turn:
pt.append(turn)
new_role = CHANGE_ROLE[turn.role]
turn = Turn(role=new_role, main=body)
return turn
while lines:
body = read_body()
if body == "":
turn = push_turn(turn)
continue
sign = body[0]
content = body[1:]
if sign in SIGN_TO_SUBNODETYPE:
turn.subnodes.append(
Subnode(type=SIGN_TO_SUBNODETYPE[sign], content=content)
)
else:
turn = push_turn(turn)
pt.append(turn)
return pt
def dumps(self, pt: PT):
lines = []
def put(content: str):
content_lines = content.splitlines()
if not content_lines:
lines.append("")
return
lines.append(content_lines.pop(0))
for line in content_lines:
lines.append(":" + line)
for turn in pt:
put(turn.main)
for n in turn.subnodes:
if n.type == "upvoted":
sign = "+"
if n.type == "downvoted":
sign = "-"
if n.type == "writing":
sign = "*"
if n.type == "unrated":
sign = "?"
put(sign + n.content)
ppttext = "\n".join(lines)
return ppttext