forked from NanBox/PiPiName
-
Notifications
You must be signed in to change notification settings - Fork 0
/
name_set.py
245 lines (215 loc) · 8.47 KB
/
name_set.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import json
import re
import opencc
from name import Name
from stroke_number import get_stroke_number
# 简体转繁体
s2tConverter = opencc.OpenCC('s2t.json')
# 繁体转简体
t2sConverter = opencc.OpenCC('t2s.json')
def get_source(source, validate, stroke_list):
exist_name = dict()
if validate:
print('>>加载名字库...')
get_name_valid('Chinese_Names', exist_name)
names = set()
# 默认
if source == 0:
get_name_dat('Chinese_Names', names, stroke_list)
# 诗经
elif source == 1:
print('>>加载诗经...')
get_name_json('诗经', names, 'content', stroke_list)
# 楚辞
elif source == 2:
print('>>加载楚辞...')
get_name_txt('楚辞', names, stroke_list)
# 论语
elif source == 3:
print('>>加载论语...')
get_name_json('论语', names, 'paragraphs', stroke_list)
# 周易
elif source == 4:
print('>>加载周易...')
get_name_txt('周易', names, stroke_list)
# 唐诗
elif source == 5:
print('>>加载唐诗...')
for i in range(0, 58000, 1000):
get_name_json('唐诗/poet.tang.' + str(i), names, 'paragraphs', stroke_list)
# 宋诗
elif source == 6:
print('>>加载宋诗...')
for i in range(0, 255000, 1000):
get_name_json('宋诗/poet.song.' + str(i), names, 'paragraphs', stroke_list)
# 宋词
elif source == 7:
print('>>加载宋词...')
for i in range(0, 22000, 1000):
get_name_json('宋词/ci.song.' + str(i), names, 'paragraphs', stroke_list)
else:
print('词库号输入错误')
print('>>筛选名字...')
# 检查名字是否存在并添加性别
if validate:
if source != 0:
names = get_intersect(names, exist_name)
return names
def get_intersect(names, exist_name):
result = set()
for i in names:
if i.first_name in exist_name.keys():
i.gender = exist_name[i.first_name]
result.add(i)
return result
# 加载名字库
def get_name_valid(path, exist_names):
with open('data/' + path + '.dat', encoding='utf-8') as f:
for line in f:
data = line.split(',')
name = data[0][1:]
gender = data[1].replace('\n', '')
if name in exist_names:
if gender != exist_names.get(name) or gender == '未知':
exist_names[name] = '双'
else:
exist_names[name] = gender
def get_name_dat(path, names, stroke_list):
with open('data/' + path + '.dat', encoding='utf-8') as f:
line_list = f.readlines()
size = len(line_list)
progress = 0
for i in range(0, size):
# 生成进度
if (i + 1) * 100 / size - progress >= 5:
progress += 5
print('>>正在生成名字...' + str(progress) + '%')
data = line_list[i].split(',')
if len(data[0]) == 2:
name = data[0]
else:
name = data[0][1:]
# 转繁体
name = s2tConverter.convert(name)
gender = data[1].replace('\n', '')
if len(name) == 2:
# 转换笔画数
strokes = list()
strokes.append(get_stroke_number(name[0]))
strokes.append(get_stroke_number(name[1]))
# 判断是否包含指定笔画数
for stroke in stroke_list:
if stroke[0] == strokes[0] and stroke[1] == strokes[1]:
names.add(Name(name, '', gender))
def get_name_txt(path, names, stroke_list):
with open('data/' + path + '.txt', encoding='utf-8') as f:
line_list = f.readlines()
size = len(line_list)
progress = 0
for i in range(0, size):
# 生成进度
if (i + 1) * 100 / size - progress >= 10:
progress += 10
print('>>正在生成名字...' + str(progress) + '%')
# 转繁体
string = s2tConverter.convert(line_list[i])
if re.search(r'\w', string) is None:
continue
string_list = re.split('!?,。,.?! \n', string)
check_and_add_names(names, string_list, stroke_list)
def get_name_json(path, names, column, stroke_list):
with open('data/' + path + '.json', encoding='utf-8') as f:
data = json.loads(f.read())
size = len(data)
progress = 0
for j in range(0, size):
# 生成进度
if (j + 1) * 100 / size - progress >= 10:
progress += 10
print('>>正在生成名字...' + str(progress) + '%')
for string in data[j][column]:
# 转繁体
string = s2tConverter.convert(string)
string_list = re.split('!?,。,.?! \n', string)
check_and_add_names(names, string_list, stroke_list)
def check_and_add_names(names, string_list, stroke_list):
for sentence in string_list:
sentence = sentence.strip()
# 转换笔画数
strokes = list()
for ch in sentence:
if is_chinese(ch):
strokes.append(get_stroke_number(ch))
else:
strokes.append(0)
# 判断是否包含指定笔画数
for stroke in stroke_list:
if stroke[0] in strokes and stroke[1] in strokes:
index0 = strokes.index(stroke[0])
index1 = strokes.index(stroke[1])
if index0 < index1:
name0 = sentence[index0]
name1 = sentence[index1]
names.add(Name(name0 + name1, sentence, ''))
# 判断是否为汉字
def is_chinese(uchar):
if u'\u4e00' <= uchar <= u'\u9fa5':
return True
else:
return False
def check_resource(name):
if len(name) != 3:
return
print('正在生成名字来源...\n')
check_name_json('诗经', name, 'content')
check_name_txt('楚辞', name)
check_name_json('论语', name, 'paragraphs')
check_name_txt('周易', name)
for i in range(0, 58000, 1000):
check_name_json('唐诗/poet.tang.' + str(i), name, 'paragraphs')
for i in range(0, 255000, 1000):
check_name_json('宋诗/poet.song.' + str(i), name, 'paragraphs')
for i in range(0, 22000, 1000):
check_name_json('宋词/ci.song.' + str(i), name, 'paragraphs')
def check_name_json(path, name, column):
with open('data/' + path + '.json', encoding='utf-8') as f:
data = json.loads(f.read())
size = len(data)
for i in range(0, size):
poem = data[i]
for string in poem[column]:
string_list = re.split('!?,。,.?! \n', string)
title = path
if path == '诗经':
title = '诗经 ' + poem['title'] + ' ' + poem['chapter'] + ' ' + poem['section']
elif path == '论语':
title = '论语 ' + poem['chapter']
elif path.startswith('唐诗'):
title = '唐诗 ' + poem['title'] + ' ' + poem['author']
elif path.startswith('宋诗'):
title = '宋诗 ' + poem['title'] + ' ' + poem['author']
elif path.startswith('宋词'):
title = '宋词 ' + poem['rhythmic'] + ' ' + poem['author']
check_name_resource(title, name, string_list)
def check_name_txt(path, name):
with open('data/' + path + '.txt', encoding='utf-8') as f:
line_list = f.readlines()
size = len(line_list)
for i in range(0, size):
if re.search(r'\w', line_list[i]) is None:
continue
string_list = re.split('!?,。,.?! \n', line_list[i])
check_name_resource(path, name, string_list)
def check_name_resource(title, name, string_list):
for sentence in string_list:
if title.startswith('唐诗') or title.startswith('宋诗'):
# 转简体
title = t2sConverter.convert(title)
sentence = t2sConverter.convert(sentence)
if name[1] in sentence and name[2] in sentence:
index0 = sentence.index(name[1])
index1 = sentence.index(name[2])
if index0 < index1:
print(title)
print(sentence.strip().replace(name[1], '「' + name[1] + '」') \
.replace(name[2], '「' + name[2] + '」') + '\n')