-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
82 lines (72 loc) · 1.91 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# @File : util.py
# @Author: Cuiqingyao
# @Date : 18-11-29
# @Desc :
# @Contact: [email protected]
import json
import re
def write_to_json(json_data, data_file):
'''
序列化json数据,易读格式
:param json_data:
:param data_file:
:return:
'''
with open(data_file, 'w', encoding='utf8') as f:
json.dump(obj=json_data, fp=f, ensure_ascii=False)
def write_to_txt(txt_data, data_file):
'''
将数据写入txt文件,分行
:param txt_data:
:param data_file:
:return:
'''
with open(data_file, 'w', encoding='utf8') as f:
for data in txt_data:
f.write(' '.join(data) + '\n')
def write_sentences(txt_data, data_file):
'''
将数据写入txt文件,分行
:param txt_data:
:param data_file:
:return:
'''
with open(data_file, 'w', encoding='utf8') as f:
for data in txt_data:
f.write(data + '\n')
def writer_answer_to_txt(results, file):
'''
分词结果写入文件
:param data:
:param file:
:return:
'''
with open(file, 'w', encoding='utf8') as f:
if isinstance(results[0], list):
for result in results:
f.write(' '.join(result[1:-1]) + '\n')
elif isinstance(results[0], str):
for result in results:
f.write(result + '\n')
else:
raise Exception("结果格式错误!")
def read_from_json(data_file):
'''
读取json数据
:param data_file:
:return:
'''
with open(data_file, 'r', encoding='utf8') as f:
json_data = json.load(f)
return json_data
def read_from_txt(data_file):
'''
读取语料数据
:param data_file: 语料
:return: list, 每个元素对用一行分词数据
'''
data = []
with open(data_file, 'r', encoding='utf8') as f:
for line in f:
data.append(re.split(r'\s+',line.strip()))
return data