aboutsummaryrefslogtreecommitdiff
path: root/mkdict-cedict
blob: f4201ec1858b2b84b5887a04e2c00624c7dd6b22 (about) (plain)
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
#!/bin/sh
# mkdict-cedict: build dictd format dictionairies from CC-CEDICT
# Copyright (C) 2024 Runciter <runciter@whispers-vpn.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

CONSONS=bcdfghjklmnpqrstwxyz

URL=https://www.mdbg.net/chinese/dictionary?page=cc-cedict
SHORT="CC-CEDICT: community maintained Chinese-English dictionary"

I=0
SPLITARRAY[$I]=zi
I=$((I+1))
SPLITARRAY[$I]=piny

I=0
FLAVOR[$I]=smpl
FLAVORINFO[$I]="simplified"
FLAVORAPPEND[$I]=-smpl
FLAVORSPLITTER[$I]=0
FLAVORSPLITINDEX[$I]=2
I=$((I+1))
FLAVOR[$I]=trad
FLAVORINFO[$I]="traditional"
FLAVORAPPEND[$I]=-trad
FLAVORSPLITTER[$I]=0
FLAVORSPLITINDEX[$I]=1
I=$((I+1))
FLAVOR[$I]=acc
FLAVORINFO[$I]=pinyin
FLAVORAPPEND[$I]=-pinyin
FLAVORSPLITTER[$I]=1
FLAVORSPLITINDEX[$I]=1
I=$((I+1))
FLAVOR[$I]=numb
FLAVORINFO[$I]="numbered"
FLAVORAPPEND[$I]=-numb
FLAVORSPLITTER[$I]=1
FLAVORSPLITINDEX[$I]=2
I=$((I+1))
FLAVOR[$I]=bare
FLAVORINFO[$I]="bare"
FLAVORAPPEND[$I]=-bare
FLAVORSPLITTER[$I]=1
FLAVORSPLITINDEX[$I]=3

PINY=": v
s/^\(.\+\)\[\([[:alnum:][:space:]·,:]*\)u:\([[:alnum:][:space:]·,:]*\)\]/\1[\2v\3]/
t v"

PINY="${PINY}
s/^\([^[]\+\)\[\([[:alnum:][:space:]·,]*\)\]/\1[\2] {\2} (\2) /"

I=0
SUBFROM[$I]=a	SUBTO[$I]=ā	TONE[$I]=1	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=a	SUBTO[$I]=á	TONE[$I]=2	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=a	SUBTO[$I]=ǎ	TONE[$I]=3	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=a	SUBTO[$I]=à	TONE[$I]=4	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=a	SUBTO[$I]=a	TONE[$I]=5	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=e	SUBTO[$I]=ē	TONE[$I]=1	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=e	SUBTO[$I]=é	TONE[$I]=2	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=e	SUBTO[$I]=ě	TONE[$I]=3	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=e	SUBTO[$I]=è	TONE[$I]=4	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=e	SUBTO[$I]=e	TONE[$I]=5	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=ou	SUBTO[$I]=ōu	TONE[$I]=1	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=ou	SUBTO[$I]=óu	TONE[$I]=2	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=ou	SUBTO[$I]=ǒu	TONE[$I]=3	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=ou	SUBTO[$I]=òu	TONE[$I]=4	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=ou	SUBTO[$I]=ou	TONE[$I]=5	LAST[$I]=no	I=$((I+1))
SUBFROM[$I]=i	SUBTO[$I]=ī	TONE[$I]=1	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=i	SUBTO[$I]=í	TONE[$I]=2	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=i	SUBTO[$I]=ǐ	TONE[$I]=3	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=i	SUBTO[$I]=ì	TONE[$I]=4	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=i	SUBTO[$I]=i	TONE[$I]=5	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=o	SUBTO[$I]=ō	TONE[$I]=1	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=o	SUBTO[$I]=ó	TONE[$I]=2	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=o	SUBTO[$I]=ǒ	TONE[$I]=3	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=o	SUBTO[$I]=ò	TONE[$I]=4	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=o	SUBTO[$I]=o	TONE[$I]=5	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=u	SUBTO[$I]=ū	TONE[$I]=1	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=u	SUBTO[$I]=ú	TONE[$I]=2	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=u	SUBTO[$I]=ǔ	TONE[$I]=3	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=u	SUBTO[$I]=ù	TONE[$I]=4	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=u	SUBTO[$I]=u	TONE[$I]=5	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=v	SUBTO[$I]=ǖ	TONE[$I]=1	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=v	SUBTO[$I]=ǘ	TONE[$I]=2	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=v	SUBTO[$I]=ǚ	TONE[$I]=3	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=v	SUBTO[$I]=ǜ	TONE[$I]=4	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=v	SUBTO[$I]=ü	TONE[$I]=5	LAST[$I]=yes	I=$((I+1))
SUBFROM[$I]=r	SUBTO[$I]="'r"	TONE[$I]=5	LAST[$I]=yes	I=$((I+1))

for IND in ${!TONE[@]}
do
	PINY="${PINY}
: ${SUBTO[$IND]}
s/^\([^[]\+\[[[:alnum:][:space:]·,]*[[:alpha:]]*\)\(${SUBFROM[$IND]}\)\([$(awk  -- 'BEGIN{print "'${LAST[$IND]}'" == "yes" ? "'$CONSONS'" : "[:alpha:]";}' /dev/null)]*\)${TONE[$IND]}\([[:alnum:][:space:]·,]*\][[:space:]]{[[:alnum:][:space:]·,]*}[[:space:]]([[:alnum:][:space:]·,]*[[:alpha:]]*${SUBFROM[$IND]}[$(awk  -- 'BEGIN{print "'${LAST[$IND]}'" == "yes" ? "'$CONSONS'" : "[:alpha:]";}' /dev/null)]*\)${TONE[$IND]}\([[:alnum:][:space:]·,]*)\)/\1${SUBTO[$IND]}\3\4\5/
s/^\([^[]\+\[.*\].*{.*}.*(.*).*\/.*\[[[:alnum:][:space:]·,]*[[:alpha:]]*\)\(${SUBFROM[$IND]}\)\([$(awk  -- 'BEGIN{print "'${LAST[$IND]}'" == "yes" ? "'$CONSONS'" : "[:alpha:]";}' /dev/null)]*\)${TONE[$IND]}\([[:alnum:][:space:]·,]*\].*$\)/\1${SUBTO[$IND]}\3\4/
t ${SUBTO[$IND]}"
done

AWK='function adddefs(entries, entries_ind, new, equivs, defs)
	{
	entries[new] = entries[new] "\n\n" equivs "\n"
	for (i in defs)
	{
		entries_ind[new]++
		entries[new] = entries[new] "\n  " entries_ind[new] ". " defs[i]
	}
}
function arrayprint(array, out)
{

	for (word in array)
	{
		print "_____\n\n" word array[word] |& out
	}
}

BEGIN {
	FS="/"'
for IND in ${!FLAVOR[@]}
do
	AWK=$AWK'
	out'${FLAVOR[$IND]}' = "dictfmt -c5 -u '"$URL"' -s \"'"${FLAVORINFO[$IND]} $SHORT"'\" --allchars --utf8 cedict'${FLAVORAPPEND[$IND]}'"'
done
AWK=$AWK'
}

/^# / {'
for IND in ${!FLAVOR[@]}
do
	AWK=$AWK'
	print substr($0, 3, length() -3) |& out'${FLAVOR[$IND]}
done
AWK=$AWK'
}

/^#! / {'
for IND in ${!FLAVOR[@]}
do
	AWK=$AWK'
	print substr($0, 4, length() -4) |& out'${FLAVOR[$IND]}
done
AWK=$AWK'
}

/^[^#]/ {
	split($1, defined, /\[/)
	split(defined[1], '${SPLITARRAY[0]}', / /)
	split (defined[2], '${SPLITARRAY[1]}', /\] \{|\} \(|\)/)'
for IND in ${!FLAVOR[@]}
do
	AWK=$AWK'
	new'${FLAVOR[$IND]}' = '${SPLITARRAY[${FLAVORSPLITTER[$IND]}]}'['${FLAVORSPLITINDEX[$IND]}']
	'
done
AWK=$AWK'equivs = '
for IND in ${!FLAVOR[@]}
do
	SEQ=$SEQ'new'${FLAVOR[$IND]}' " | " '
done
SEQ="${SEQ:0:-6}"
AWK=$AWK$SEQ
AWK=$AWK'
	delete defs
	for (i = 2 ; i < NF ; i++)
	{
		defs[i-1] = $i
	}'
for IND in ${!FLAVOR[@]}
do
	AWK=$AWK'
	gsub(/[[:space:]]|,|,/, "", new'${FLAVOR[$IND]}')
	adddefs('${FLAVOR[$IND]}', '${FLAVOR[$IND]}'_ind, new'${FLAVOR[$IND]}', equivs, defs)'
done
AWK=$AWK'
}

END {'
for IND in ${!FLAVOR[@]}
do
	AWK=$AWK'
	arrayprint('${FLAVOR[$IND]}', out'${FLAVOR[$IND]}')
	close(out'${FLAVOR[$IND]}')'
done
AWK=$AWK'
}'

cat $1 | gunzip - | sed "$PINY" | gawk -- "$AWK"