-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructurizer.rb
315 lines (269 loc) · 9.25 KB
/
structurizer.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
##
## MIT License
##
## Copyright (c) 2016 SilentByte <https://silentbyte.com/>
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in all
## copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
##
require 'date'
require 'json'
require 'nokogiri'
class PinyinConverter
# Structure *must* be ordered by length.
DiacriticPositions = [
['iao', 'ia^o'],
['IAO', 'IA^O'],
['uai', 'ua^i'],
['UAI', 'UA^I'],
['ai', 'a^i'],
['ao', 'a^o'],
['ei', 'e^i'],
['ia', 'ia^'],
['ie', 'ie^'],
['io', 'io^'],
['iu', 'iu^'],
['AI', 'A^I'],
['AO', 'A^O'],
['EI', 'E^I'],
['IA', 'IA^'],
['IE', 'IE^'],
['IO', 'IO^'],
['IU', 'IU^'],
['ou', 'o^u'],
['ua', 'ua^'],
['ue', 'ue^'],
['ui', 'ui^'],
['uo', 'uo^'],
['üe', 'üe^'],
['OU', 'O^U'],
['UA', 'UA^'],
['UE', 'UE^'],
['UI', 'UI^'],
['UO', 'UO^'],
['ÜE', 'ÜE^'],
['a', 'a^'],
['e', 'e^'],
['i', 'i^'],
['o', 'o^'],
['u', 'u^'],
['ü', 'ü^'],
['m', 'm^'],
['r', 'r^'],
['A', 'A^'],
['E', 'E^'],
['I', 'I^'],
['O', 'O^'],
['U', 'U^'],
['Ü', 'Ü^'],
['M', 'M^'],
['R', 'R^']
]
DiacriticCandidates = [
'a^', 'e^', 'i^' ,'o^' ,'u^', 'ü^', 'm^', 'r^', 'A^', 'E^', 'I^', 'O^', 'U^', 'Ü^', 'M^', 'R^'
]
DiacriticCharacters = [
['ā', 'ē', 'ī', 'ō', 'ū', 'ǖ', 'm̄', 'r', 'Ā', 'Ē', 'Ī', 'Ō', 'Ū', 'Ǖ', 'M̄', 'R'],
['á', 'é', 'í', 'ó', 'ú', 'ǘ', 'ḿ', 'r', 'Á', 'É', 'Í', 'Ó', 'Ú', 'Ǘ', 'Ḿ', 'R'],
['ǎ', 'ě', 'ǐ', 'ǒ', 'ǔ', 'ǚ', 'm̌', 'r', 'Ǎ', 'Ě', 'Ǐ', 'Ǒ', 'Ǔ', 'Ǚ', 'M̌', 'R'],
['à', 'è', 'ì', 'ò', 'ù', 'ǜ', 'm̀', 'r', 'À', 'È', 'Ì', 'Ò', 'Ù', 'Ǜ', 'M̀', 'R'],
['a', 'e', 'i', 'o', 'u', 'ü', 'm', 'r', 'A', 'E', 'I', 'O', 'U', 'Ü', 'M', 'R']
]
def parse_match(word, tone)
DiacriticPositions.each do |p|
break if word.gsub!(p[0], p[1])
end
DiacriticCandidates.each_with_index do |c, i|
break if word.gsub!(c, DiacriticCharacters[tone.to_i - 1][i])
end
return word
end
def numeric_to_diacritic(pinyin)
# Only consider words ending with a tone indicator.
return pinyin.gsub(/([a-zA-ZüÜ]+)(\d)/) { self.parse_match($1, $2) }
end
end
class CedictParser
def initialize
@pinyinConverter = PinyinConverter.new
end
def get_fields(line)
regex = /^(?<traditional>.+?) (?<simplified>.+?) \[(?<pinyin>.+?)\] (?<definitions>\/.+\/)/
return regex.match(line)
end
def get_characters(characters)
return characters.scan(/\p{Han}/).collect do |c|
{:cp => c.ord.to_s(16), :c => c}
end
end
def get_numeric_pinyin(pinyin)
return pinyin.sub('u:', 'ü').sub('U:', 'Ü')
end
def get_diacritic_pinyin(pinyin)
return @pinyinConverter.numeric_to_diacritic(pinyin)
end
def get_definitions(definitions)
return definitions.split('/').reject { |m| m.empty? }
end
def get_diacritic_definitions(definitions)
return self.get_definitions(definitions).map! { |d| d.gsub(/(\[.+?\])/) { self.get_diacritic_pinyin($1) } }
end
def parse_line(line)
if not line[0] or line[0] == '#'
return nil
end
fields = self.get_fields(line)
if not fields
raise 'Dictionary entry is invalid.'
return nil
end
return {
:traditional => fields[:traditional],
:simplified => fields[:simplified],
:referencedTraditional => self.get_characters(fields[:traditional]),
:referencedSimplified => self.get_characters(fields[:simplified]),
:pinyinNumeric => self.get_numeric_pinyin(fields[:pinyin]),
:pinyinDiacritic => self.get_diacritic_pinyin(fields[:pinyin]),
:definitions => self.get_definitions(fields[:definitions]),
:definitionsDiacritic => self.get_diacritic_definitions(fields[:definitions])
}
end
end
class GenericStructurizer
def structurize(definition_file, output_file)
File.open(output_file, 'w') do |output|
line_counter = 0
parser = CedictParser.new
output.write(self.document_preamble)
IO.foreach(definition_file) do |line|
line_counter += 1
begin
object = parser.parse_line(line)
rescue => e
STDERR.puts "Invalid dictionary entry on line #{line_counter}.",
" -> #{e}"
end
if not object
next
end
output.write(self.object_prefix(object))
output.write(self.object_data(object))
output.write(self.object_postfix(object))
end
output.write(self.document_postamble)
end
end
end
class JsonStructurizer < GenericStructurizer
def initialize
@is_first = true
end
def document_preamble
return "[\n"
end
def document_postamble
return "\n]"
end
def object_prefix(object)
if @is_first
@is_first = false
return ' '
else
return ",\n "
end
end
def object_postfix(object)
return ''
end
def object_data(object)
return object.to_json
end
end
class XmlStructurizer < GenericStructurizer
def document_preamble
date = DateTime.now.iso8601
return '<?xml version="1.0" encoding="utf-8" ?>' + "\n" +
"<cc-cedict created=\"#{date}\">\n"
end
def document_postamble
return '</cc-cedict>'
end
def object_prefix(object)
return ''
end
def object_postfix(object)
return "\n"
end
def object_data(object)
builder = Nokogiri::XML::Builder.new do |xml|
xml.entry {
xml.traditional object[:traditional]
xml.simplified object[:simplified]
xml.send('referenced-traditional') {
object[:referencedTraditional].each { |c| xml.character c }
}
xml.send('referenced-simplified') {
object[:referencedSimplified].each { |c| xml.character c }
}
xml.send('pinyin-numeric', object[:pinyinNumeric])
xml.send('pinyin-diacritic', object[:pinyinDiacritic])
xml.definitions {
object[:definitions].each { |c| xml.definition c }
}
xml.send('definitions-diacritic') {
object[:definitionsDiacritic].each { |c| xml.definition c }
}
}
end
return builder.doc.root.to_xml
end
end
class CsvStructurizer < GenericStructurizer
def document_preamble
return "traditional\tsimplified\treferenced-traditional\treferenced-simplified\tpinyin-numeric\tpinyin-diacritic\tdefinitions\tdefinitions-diacritic\n"
end
def document_postamble
return ''
end
def object_prefix(object)
return ''
end
def object_postfix(object)
return "\n"
end
def object_data(object)
return object[:traditional] + "\t" +
object[:simplified] + "\t" +
object[:referencedTraditional].collect {|c| c[:c]}.join('/') + "\t" +
object[:referencedSimplified].collect {|c| c[:c]}.join('/') + "\t" +
object[:pinyinNumeric] + "\t" +
object[:pinyinDiacritic] + "\t" +
object[:definitions].join('/') + "\t" +
object[:definitionsDiacritic].join('/')
end
end
def main(format, definition_file, output_file)
structurizer = case format
when 'json' then JsonStructurizer.new
when 'xml' then XmlStructurizer.new
when 'csv' then CsvStructurizer.new
else raise "Format '#{format}' is unknown."
end
structurizer.structurize(definition_file, output_file)
end
main(ARGV[0], ARGV[1], ARGV[2])