|
#!/usr/bin/python
|
# -*- coding: UTF-8 -*-
|
|
import sys
|
import numpy as np
|
|
|
'''
|
为每一个汉字 选取词语 开头 结尾
|
为每一个汉字 选取成语,该汉字在成语的第1,2,3,4个字;
|
|
每个汉字输出一个txt文件;
|
|
自行修改,输出全部,还是只输出前几个'''
|
|
|
|
two_word_file = 'two_sorted.txt'
|
four_word_lfile = 'idoms_sort.txt'
|
two_word_list = []
|
four_word_list = []
|
|
|
with open(two_word_file, 'r', encoding='utf-8-sig') as f:
|
for word in f.read().splitlines():
|
if len(word) == 2:
|
two_word_list.append(word)
|
with open(four_word_lfile, 'r', encoding='utf-8-sig') as f:
|
for word in f.read().splitlines():
|
if len(word) == 4:
|
four_word_list.append(word)
|
|
|
print(len(two_word_list))
|
print(len(four_word_list))
|
|
|
def get_target_words(list, char, position):
|
word_list = []
|
for item in list:
|
if item[position] == char:
|
word_list.append(item)
|
return word_list
|
|
|
def get_target_words_all(char):
|
word_list_1 = get_target_words(two_word_list, char, 0)
|
word_list_2 = get_target_words(two_word_list, char, 1)
|
word_list_3 = get_target_words(four_word_list, char, 0)
|
word_list_4 = get_target_words(four_word_list, char, 1)
|
word_list_5 = get_target_words(four_word_list, char, 2)
|
word_list_6 = get_target_words(four_word_list, char, 3)
|
|
return word_list_1, word_list_2, word_list_3, word_list_4, word_list_5, word_list_6
|
|
|
target_words = '天地玄黄宇宙洪荒日月盈昃辰宿列张寒来暑往秋收冬藏闰余成岁律吕调阳云腾致雨露结为霜金生丽水玉出昆冈剑号巨阙珠称夜光果珍李柰菜重芥姜海咸河淡鳞潜羽翔龙师火帝鸟官人皇始制文字乃服衣裳推位让国有虞陶唐吊民伐罪'
|
|
print(len(target_words))
|
|
|
word_index = 1
|
def write_into_file(f, list, limit):
|
count = 0
|
for item in list:
|
f.write(item + ' ')
|
count = count + 1
|
if count >= limit:
|
break
|
|
for char in target_words:
|
l1, l2, l3, l4, l5, l6 = get_target_words_all(char)
|
file_name = str(word_index).zfill(4)+'_'+char+'.txt'
|
with open('target_dir/'+file_name, 'w', encoding='utf-8') as final_f:
|
final_f.write(char)
|
final_f.write('\n')
|
write_into_file(final_f, l1,3)
|
final_f.write('\n')
|
write_into_file(final_f, l2,3)
|
final_f.write('\n')
|
write_into_file(final_f, l3,1)
|
write_into_file(final_f, l4,1)
|
write_into_file(final_f, l5,1)
|
write_into_file(final_f, l6,1)
|
word_index = word_index+1
|
|
|
|
|
all_word_index = 1
|
def write_into_file(f, list):
|
for item in list:
|
f.write(item + ' ')
|
f.write('\n')
|
|
|
|
for char in target_words:
|
l1, l2, l3, l4, l5, l6 = get_target_words_all(char)
|
file_name = 'all'+str(all_word_index).zfill(4)+'_'+char+'.txt'
|
with open('target_dir_all/'+file_name, 'w', encoding='utf-8') as final_f:
|
final_f.write(char)
|
final_f.write('\n')
|
write_into_file(final_f, l1)
|
write_into_file(final_f, l2)
|
write_into_file(final_f, l3)
|
write_into_file(final_f, l4)
|
write_into_file(final_f, l5)
|
write_into_file(final_f, l6)
|
all_word_index = all_word_index+1
|