aboutsummaryrefslogtreecommitdiff
path: root/util/compile.py
blob: 1b69bd83989de485f23a24a626409113f396119f (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
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
#!/usr/bin/env python

# Copyright (C) 2016  Alex Yatskov <alex@foosoft.net>
# Author: Alex Yatskov <alex@foosoft.net>
#
# 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 <http://www.gnu.org/licenses/>.


import codecs
import json
import optparse
import os.path
import re


PARSED_TAGS = {
    'Buddh':   'Buddhist term',
    'MA':      'martial arts term',
    'X':       'rude or X-rated term',
    'abbr':    'abbreviation',
    'adj':     'former adjective classification (being removed)',
    'adj-f':   'noun or verb acting prenominally (other than the above)',
    'adj-i':   'adjective (keiyoushi)',
    'adj-na':  'adjectival nouns or quasi-adjectives (keiyodoshi)',
    'adj-no':  'nouns which may take the genitive case particle "no"',
    'adj-pn':  'pre-noun adjectival (rentaishi)',
    'adj-t':   '"taru" adjective',
    'adv':     'adverb (fukushi)',
    'adv-n':   'adverbial noun',
    'adv-to':  'adverb taking the "to" particle',
    'arch':    'archaism',
    'ateji':   'ateji (phonetic) reading',
    'aux':     'auxiliary',
    'aux-adj': 'auxiliary adjective',
    'aux-v':   'auxiliary verb',
    'c':       'company name',
    'chn':     'children\'s language',
    'col':     'colloquialism',
    'comp':    'computer terminology',
    'conj':    'conjunction',
    'ctr':     'counter',
    'derog':   'derogatory term',
    'eK':      'exclusively kanji',
    'ek':      'exclusively kana',
    'exp':     'Expressions (phrases, clauses, etc.)',
    'f':       'female given name',
    'fam':     'familiar language',
    'fem':     'female term or language',
    'food':    'food term',
    'g':       'given name, as-yet not classified by sex',
    'geom':    'geometry term',
    'gikun':   'gikun (meaning) reading',
    'gram':    'grammatical term',
    'h':       'full (usually family plus given) name of a particular person',
    'hon':     'honorific or respectful (sonkeigo) language',
    'hum':     'humble (kenjougo) language',
    'iK':      'word containing irregular kanji usage',
    'id':      'idiomatic expression',
    'ik':      'word containing irregular kana usage',
    'int':     'interjection (kandoushi)',
    'io':      'irregular okurigana usage',
    'iv':      'irregular verb',
    'ling':    'linguistics terminology',
    'm':       'male given name',
    'm-sl':    'manga slang',
    'male':    'male term or language',
    'male-sl': 'male slang',
    'math':    'mathematics',
    'mil':     'military',
    'n':       'noun (common) (futsuumeishi)',
    'n-adv':   'adverbial noun (fukushitekimeishi)',
    'n-pref':  'noun, used as a prefix',
    'n-suf':   'noun, used as a suffix',
    'n-t':     'noun (temporal) (jisoumeishi)',
    'num':     'numeric',
    'oK':      'word containing out-dated kanji',
    'obs':     'obsolete term',
    'obsc':    'obscure term',
    'ok':      'out-dated or obsolete kana usage',
    'on-mim':  'onomatopoeic or mimetic word',
    'P':       'popular term',
    'p':       'place-name',
    'physics': 'physics terminology',
    'pn':      'pronoun',
    'poet':    'poetical term',
    'pol':     'polite (teineigo) language',
    'pr':      'product name',
    'pref':    'prefix',
    'prt':     'particle',
    'rare':    'rare (now replaced by "obsc")',
    's':       'surname',
    'sens':    'sensitive word',
    'sl':      'slang',
    'st':      'stations',
    'suf':     'suffix',
    'u':       'person name, either given or surname, as-yet unclassified',
    'uK':      'word usually written using kanji alone',
    'uk':      'word usually written using kana alone',
    'v1':      'Ichidan verb',
    'v2a-s':   'Nidan verb with "u" ending (archaic)',
    'v4h':     'Yodan verb with "hu/fu" ending (archaic)',
    'v4r':     'Yodan verb with "ru" ending (archaic)',
    'v5':      'Godan verb (not completely classified)',
    'v5aru':   'Godan verb - -aru special class',
    'v5b':     'Godan verb with "bu" ending',
    'v5g':     'Godan verb with "gu" ending',
    'v5k':     'Godan verb with "ku" ending',
    'v5k-s':   'Godan verb - iku/yuku special class',
    'v5m':     'Godan verb with "mu" ending',
    'v5n':     'Godan verb with "nu" ending',
    'v5r':     'Godan verb with "ru" ending',
    'v5r-i':   'Godan verb with "ru" ending (irregular verb)',
    'v5s':     'Godan verb with "su" ending',
    'v5t':     'Godan verb with "tsu" ending',
    'v5u':     'Godan verb with "u" ending',
    'v5u-s':   'Godan verb with "u" ending (special class)',
    'v5uru':   'Godan verb - uru old class verb (old form of Eru)',
    'v5z':     'Godan verb with "zu" ending',
    'vi':      'intransitive verb',
    'vk':      'kuru verb - special class',
    'vn':      'irregular nu verb',
    'vs':      'noun or participle which takes the aux. verb suru',
    'vs-c':    'su verb - precursor to the modern suru',
    'vs-i':    'suru verb - irregular',
    'vs-s':    'suru verb - special class',
    'vt':      'transitive ver',
    'vulg':    'vulgar expression or word',
    'vz':      'Ichidan verb - zuru verb - (alternative form of -jiru verbs)',
}


def is_hiragana(c):
    return 0x3040 <= ord(c) < 0x30a0


def is_katakana(c):
    return 0x30a0 <= ord(c) < 0x3100


def load_definitions(path):
    print('Parsing "{0}"...'.format(path))
    with codecs.open(path, encoding='euc-jp') as fp:
        return filter(lambda x: x and x[0] != '#', fp.read().splitlines())


def parse_kanji_dic(path):
    results = {}
    for line in load_definitions(path):
        segments = line.split()
        character = segments[0]
        kunyomi = ' '.join(filter(lambda x: list(filter(is_hiragana, x)), segments[1:]))
        onyomi = ' '.join(filter(lambda x: list(filter(is_katakana, x)), segments[1:]))
        glossary = '; '.join(re.findall('\{([^\}]+)\}', line))
        results[character] = (kunyomi or None, onyomi or None, glossary)

    return results


def parse_edict(path):
    results = []
    for line in load_definitions(path):
        segments = line.split('/')

        exp_parts = segments[0].split(' ')
        expression = exp_parts[0]
        reading_match = re.search('\[([^\]]+)\]', exp_parts[1])
        reading = None if reading_match is None else reading_match.group(1)

        defs = []
        tags = set()

        for index, dfn in enumerate(filter(None, segments[1:])):
            dfn_match = re.search(r'^((?:\((?:[\w\-\,\:]*)*\)\s*)*)(.*)$', dfn)

            tags_raw = set(filter(None, re.split(r'[\s\(\),]', dfn_match.group(1))))
            tags_raw = tags_raw.intersection(set(PARSED_TAGS.keys()))
            tags = tags.union(tags_raw)

            gloss = dfn_match.group(2).strip()
            if len(gloss) == 0:
                continue

            if index == 0 or len(dfn_match.group(1)) > 0:
                defs.append([gloss])
            else:
                defs[-1].append(gloss)

        result = [expression, reading, ' '.join(tags)]
        result += map(lambda x: '; '.join(x), defs)

        results.append(result)

    indices = {}
    for i, d in enumerate(results):
        for key in d[:2]:
            if key is not None:
                values = indices.get(key, [])
                values.append(i)
                indices[key] = values

    return {'defs': results, 'indices': indices}


def build_dict(output_dir, input_file, parser):
    if input_file is not None:
        base = os.path.splitext(os.path.basename(input_file))[0]
        with open(os.path.join(output_dir, base) + '.json', 'w') as fp:
             # json.dump(parser(input_file), fp, sort_keys=True, indent=4, separators=(',', ': '))
             json.dump(parser(input_file), fp, separators=(',', ':'))


def build(dict_dir, kanjidic, edict, enamdict):
    build_dict(dict_dir, kanjidic, parse_kanji_dic)
    build_dict(dict_dir, edict, parse_edict)
    build_dict(dict_dir, enamdict, parse_edict)


def main():
    parser = optparse.OptionParser()
    parser.add_option('--kanjidic', dest='kanjidic')
    parser.add_option('--edict', dest='edict')
    parser.add_option('--enamdict', dest='enamdict')

    options, args = parser.parse_args()

    if len(args) == 0:
        parser.print_help()
    else:
        build(args[0], options.kanjidic, options.edict, options.enamdict)


if __name__ == '__main__':
    main()