-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvhandler.py
More file actions
114 lines (89 loc) · 2.8 KB
/
Copy pathcsvhandler.py
File metadata and controls
114 lines (89 loc) · 2.8 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import csv
import re
INPUT_CSV = "validation.csv"
OUTPUT_TXT = "data/char/val.txt"
_ws = re.compile(r"\s+")
_apostrophe_spaces = re.compile(r"\s*'\s*")
def normalize_text(t: str) -> str:
if t is None:
return ""
t = t.strip()
# normalize “smart” quotes/apostrophes to plain ones
t = (t.replace("\u2019", "'") # ’
.replace("\u2018", "'") # ‘
.replace("\u201C", '"') # “
.replace("\u201D", '"') # ”
.replace("\u00A0", " ")) # nbsp
# fix spaced-apostrophe tokens: don ' t -> don't, that ' s -> that's, Let ' s -> Let's
t = _apostrophe_spaces.sub("'", t)
# optional: normalize weird spacing around punctuation a bit
t = t.replace(" ,", ",").replace(" .", ".").replace(" !", "!").replace(" ?", "?").replace(" ;", ";").replace(" :", ":")
# collapse whitespace
t = _ws.sub(" ", t).strip()
return t
def extract_turns(dialog_cell: str):
s = (dialog_cell or "").strip()
if not s:
return []
s = s.replace('""', '"')
l = s.find("[")
r = s.rfind("]")
if l != -1 and r != -1 and r > l:
s = s[l+1:r]
turns = []
buf = []
in_quote = False
q = None
i = 0
n = len(s)
while i < n:
ch = s[i]
if not in_quote and ch in ("'", '"'):
in_quote = True
q = ch
buf = []
i += 1
continue
if in_quote and ch == q:
if q == "'":
j = i + 1
while j < n and s[j].isspace():
j += 1
if j >= n or s[j] in ("]", "'", '"'):
text = "".join(buf).strip()
if text:
turns.append(text)
in_quote = False
q = None
buf = []
i += 1
continue
else:
buf.append("'")
i += 1
continue
else:
text = "".join(buf).strip()
if text:
turns.append(text)
in_quote = False
q = None
buf = []
i += 1
continue
if in_quote:
buf.append(ch)
i += 1
return turns
with open(INPUT_CSV, newline="", encoding="utf-8") as f_in, open(OUTPUT_TXT, "w", encoding="utf-8") as f_out:
reader = csv.DictReader(f_in)
for row in reader:
turns = extract_turns(row.get("dialog", ""))
if not turns:
continue
turns = [normalize_text(t) for t in turns]
for i, utt in enumerate(turns):
role = "User" if i % 2 == 0 else "Assistant"
f_out.write(f"{role}: {utt}\n")
f_out.write("\n")
print("done.")