wangzhibo
2023-08-30 38198e716dea44c698b31c5abaf74dac32173edb
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
 
#!/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