-
Notifications
You must be signed in to change notification settings - Fork 4
/
make-dict.rb
executable file
·296 lines (266 loc) · 9.7 KB
/
make-dict.rb
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/ruby -w
# This creates dictionary files
require "sqlite3"
require 'optparse'
require_relative "pingyam-rb/lib_pingyam.rb"
@options = {:romanization => :yale, :incomplete => true, :vulgar => true, :format => :kobo}
OptionParser.new do |opts|
opts.banner = "Usage: #{$0}"
opts.on("-j", "--jyutping", "jyutping romanizaiton [default is yale]") { |n| @options[:romanization] = :jyutping }
opts.on("-c", "--complete-only", "skip incomplete entries") { |n| @options[:incomplete] = false }
opts.on("-n", "--nice", "no vulgar words") { |n| @options[:vulgar] = false }
opts.on("-a", "--kindle", "make kindle dictionary format [default kobo df]") { |n| @options[:format] = :kindle }
opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end.parse!
def make_dictname
name = "cantodict-#{@options[:romanization]}"
name += '-no-incomplete' unless @options[:incomplete]
name += '-no-vulgar' unless @options[:vulgar]
return name
end
CoverHtml = <<HEREDOC
<html>
<head>
<meta content="text/html" http-equiv="content-type">
</head>
<body>
<h1>Cantodict (v. %s)</h1>
<h2>A Collaborative Dictionary of Cantonese from <a href="https://www.cantonese.sheik.co.uk/">https://www.cantonese.sheik.co.uk/</a></h2>
<h3>Created by Cantodict editors over the years</h3>
</body>
</html>
HEREDOC
CopyrightHtml = <<HEREDOC
<html>
<head>
<meta content="text/html" http-equiv="content-type">
</head>
<body>
<h1>Copyright</h1>
<h3>Cantodict editors from <a href="https://www.cantonese.sheik.co.uk/">https://www.cantonese.sheik.co.uk/</a></h3>
<ul>
<li>bybell -- 36397</li>
<li>tym -- 7297</li>
<li>sheik -- 5327</li>
<li>Michael 忠仔 -- 4747</li>
<li>Steph_fr -- 3631</li>
<li>C Chiu -- 1922</li>
<li>WANNABEAFREAK -- 1399</li>
<li>claw -- 1256</li>
<li>bernard -- 778</li>
<li>yuetwoh -- 616</li>
<li>radagasty -- 527</li>
<li>Eldon -- 493</li>
<li>nanimo -- 463</li>
<li>Yeegs -- 453</li>
<li>aaron -- 335</li>
<li>穆斯林 -- 244</li>
<li>rathpy -- 202</li>
<li>tuan -- 196</li>
<li>kath -- 154</li>
<li>riko -- 137</li>
<li>XieChengnuo -- 133</li>
<li>yckillua -- 108</li>
<li>spacemonkey -- 93</li>
<li>wai ming -- 82</li>
<li>duaaagiii -- 72</li>
<li>goofygreg -- 65</li>
<li>AliceWong -- 61</li>
<li>SophusNg -- 60</li>
<li>MrBug -- 59</li>
<li>Emerald -- 53</li>
<li>beLIEve -- 47</li>
<li>Kobo-Daishi -- 45</li>
<li>jsrwang -- 43</li>
<li>Cryme -- 40</li>
<li>theABC -- 38</li>
<li>SaNgUi -- 37</li>
<li>ray_siu -- 34</li>
<li>changeup -- 30</li>
<li>dr.slump -- 29</li>
<li>platinumangel -- 26</li>
<li>14K Guy -- 23</li>
<li>azurenights -- 18</li>
<li>BenoniH -- 17</li>
<li>atyh1985 -- 16</li>
<li>desmond -- 15</li>
<li>s.m. -- 13</li>
<li>Honna -- 13</li>
<li>Antje -- 13</li>
<li>supermidget -- 12</li>
<li>hkl8324 -- 12</li>
<li>Wonton -- 12</li>
<li>Shusaku -- 11</li>
<li>rschmitt -- 10</li>
<li>anthony -- 10</li>
<li>alanl -- 10</li>
<li>carson -- 9</li>
<li>a-dat -- 7</li>
<li>asiancajun -- 6</li>
<li>ChaakMing -- 6</li>
<li>wilson -- 4</li>
<li>registered99 -- 4</li>
<li>makiaea -- 4</li>
<li>RichardSharpe -- 4</li>
<li>Zac2333 -- 3</li>
<li>ProsperousBridge -- 3</li>
<li>velshin -- 2</li>
<li>takeru -- 1</li>
<li>romano -- 1</li>
</ul>
</body>
</html>
HEREDOC
CantodictOpf = <<HEREDOC
<?xml version="1.0"?>
<package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookId">
<metadata>
<dc:title>Cantodict (v. %s)</dc:title>
<dc:creator opf:role="aut">Cantodict editors from https://www.cantonese.sheik.co.uk/</dc:creator>
<dc:language>zh</dc:language>
<meta name="cover" content="my-cover-image" />
<x-metadata>
<DictionaryInLanguage>zh</DictionaryInLanguage>
<DictionaryOutLanguage>en</DictionaryOutLanguage>
<DefaultLookupIndex>cantodict</DefaultLookupIndex>
</x-metadata>
</metadata>
<manifest>
<!-- <item href="cover-image.jpg" id="my-cover-image" media-type="image/jpg" /> -->
<item id="cover"
href="cover.html"
media-type="application/xhtml+xml" />
<item id="copyright"
href="copyright.html"
media-type="application/xhtml+xml" />
<item id="content"
href="content.html"
media-type="application/xhtml+xml" />
</manifest>
<spine>
<itemref idref="cover" />
<itemref idref="copyright"/>
<itemref idref="content"/>
</spine>
<guide>
<reference type="index" title="IndexName" href="content.html"/>
</guide>
</package>
HEREDOC
DictionaryStart =<<HEREDOC
<html xmlns:math="http://exslt.org/math" xmlns:svg="http://www.w3.org/2000/svg" xmlns:tl="https://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf"
xmlns:saxon="http://saxon.sf.net/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cx="https://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:mbp="https://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf" xmlns:mmc="https://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf" xmlns:idx="https://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<mbp:frameset>
HEREDOC
DictionaryEnd = <<HEREDOC
</mbp:frameset>
</body>
</html>
HEREDOC
EntryStart =<<HEREDOC
<idx:entry name="cantodict" scriptable="yes" spell="yes">
<idx:short><a id="%{id}"></a>
<idx:orth value="%{headword}">%{headword} - %{short_description}
HEREDOC
EntryEnd =<<HEREDOC
</idx:orth>
<p>%{definition}</p>
</idx:short>
</idx:entry>
HEREDOC
@conv = Converter.new(6)
def get_dict_data()
begin
dbfile = ARGV[0]
dbfile = 'output/cantodict.sqlite' unless dbfile
db = SQLite3::Database.new dbfile
db.transaction
entries = []
target_romanization = 1
target_romanization = 6 if @options[:romanization] == :jyutping
db.execute("SELECT chinese, cantodict_id, definition, notes, pos, flag, jyutping, pinyin, dialect, incomplete, radical, stroke_count, variants, similar FROM Entries where entry_type in ('character', 'compound')") do |row|
chinese, cantodict_id, definition, notes, pos, flag, jyutping, pinyin, dialect, incomplete, radical, stroke_count, variants, similar = row
next if (not @options[:vulgar]) && flag.split(',').include?("Vulgar")
next if (not @options[:incomplete]) && incomplete == 1
romanization = @conv.convert_line(jyutping, target_romanization)
short_description = "<i>#{romanization}</i>"
short_description += " [拼 <i>#{pinyin}</i>]" unless pinyin.empty?
short_description += " 部:#{radical}" unless (radical.nil? or radical.empty?)
short_description += " [粵]" if dialect == 'cantonese-only'
short_description += " [國]" if dialect == 'mandarin-or-written-only'
short_description += " INCOMPLETE" if incomplete == 1
definition_lines = []
definition_lines << definition.gsub("\r\n","<br />").strip.gsub("\n", "<br />")
definition_lines << notes.gsub("\r\n","<br />").strip.gsub("\n", "<br />") if notes
definition_body = definition_lines.join("<br />")
attributes = []
attributes << "<b>Pos:</b> #{pos.strip} " unless pos.strip.empty?
attributes << "<b>Strokes:</b> #{stroke_count} " if stroke_count
attributes << "<b>Variants:</b> #{variants} " unless variants.empty?
attributes << "<b>Similar:</b> #{similar} " unless similar.empty?
definition_body += "<br />" + attributes.join("<br />") unless attributes.empty?
entries << {
headword: chinese,
id: cantodict_id,
variants: variants.split(','),
short_description: short_description,
definition: definition_body }
end
return entries
ensure
if db
db.rollback if db.transaction_active?
db.close
end
end
end
def make_kindle_dir(entries)
dictname = make_dictname()
directory_name = "output/#{dictname}/kindle-#{dictname}"
Dir.mkdir(directory_name) unless File.exist?(directory_name)
File.open(directory_name + "/copyright.html", "w") { |f| f.write(CopyrightHtml) }
File.open(directory_name + "/cover.html", "w") { |f| f.write(CoverHtml % dictname.sub("cantodict-","")) }
File.open(directory_name + "/cantodict.opf", "w") { |f| f.write(CantodictOpf % dictname.sub("cantodict-","")) }
File.open(directory_name + "/content.html", "w") do |f|
f.print DictionaryStart
entries.each do |entry|
f.print EntryStart % entry
variants = entry[:variants]
if variants and variants.size > 0
f.puts "<idx:infl>"
variants.each {|v| f.puts %(<idx:iform value="#{v}"></idx:iform>)}
f.puts "</idx:infl>"
end
f.print EntryEnd % entry
end
f.print DictionaryEnd
end
end
def make_kobo_dictfile(entries)
dictname = make_dictname()
filename = "output/#{dictname}/kobo-#{dictname}.df"
File.open(filename, "w") do |f|
entries.each do |entry|
f.puts("@#{entry[:headword]}")
f.puts(": - #{entry[:short_description]}")
variants = entry[:variants]
variants.each { |v| f.puts("&#{v}") } if variants and variants.size > 0
f.print("<html>")
f.puts(entry[:definition])
f.puts("<br /><b>Cantodict id:</b> #{entry[:id]}")
end
end
end
entries = get_dict_data
if @options[:format] == :kobo
make_kobo_dictfile(entries)
elsif @options[:format] == :kindle
make_kindle_dir(entries)
end