-
Notifications
You must be signed in to change notification settings - Fork 787
/
util.py
59 lines (45 loc) · 1.32 KB
/
util.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
import codecs
import chardet
from config import Config
def str_q2b(s):
res = ""
for u in s:
c = ord(u)
if c == 12288:
c = 32
elif 65281 <= c <= 65374:
c -= 65248
res += chr(c)
return res
def check_file_encoding(file_name):
f = open(file_name, 'rb')
data = f.read()
detect_result = chardet.detect(data)
print(detect_result)
return detect_result
def generate_single_pairs_from_multi_turn(utterances):
pairs = []
for index in range(len(utterances) - 1):
pairs.append((utterances[index], utterances[index + 1]))
return pairs
def check_format(file_name):
file = codecs.open(file_name, encoding=Config.encoding)
for index, line in enumerate(file):
pair = line.split("\t")
if not len(pair) == 2:
print("error", file_name)
print(line, index, len(pair))
break
file.close()
def format_refine(file_name):
file = codecs.open(file_name, encoding=Config.encoding)
valid_lines = []
for index, line in enumerate(file):
pair = line.split("\t")
if len(pair) == 2:
valid_lines.append(line)
file.close()
file = codecs.open(file_name, "w", encoding=Config.encoding)
for line in valid_lines:
file.write(line)
file.close()