Updating dictionary format again

This commit is contained in:
Alex Yatskov 2016-04-12 20:17:40 -07:00
parent bf7c476a72
commit cfab4c31ec

View File

@ -18,80 +18,114 @@
import codecs import codecs
import json
import optparse import optparse
import os.path import os.path
import re import re
PARSED_TAGS = { PARSED_TAGS = {
'P', # common word 'Buddh': 'Buddhist term',
'adj', # former adjective classification (being removed) 'MA': 'martial arts term',
'adj-f', # noun or verb acting prenominally (other than the above) 'X': 'rude or X-rated term',
'adj-i', # adjective (keiyoushi) 'abbr': 'abbreviation',
'adj-na', # adjectival nouns or quasi-adjectives (keiyodoshi) 'adj': 'former adjective classification (being removed)',
'adj-no', # nouns which may take the genitive case particle `no' 'adj-f': 'noun or verb acting prenominally (other than the above)',
'adj-pn', # pre-noun adjectival (rentaishi) 'adj-i': 'adjective (keiyoushi)',
'adj-t', # `taru' adjective 'adj-na': 'adjectival nouns or quasi-adjectives (keiyodoshi)',
'adv', # adverb (fukushi) 'adj-no': 'nouns which may take the genitive case particle "no"',
'adv-n', # adverbial noun 'adj-pn': 'pre-noun adjectival (rentaishi)',
'adv-to', # adverb taking the `to' particle 'adj-t': '"taru" adjective',
'aux', # auxiliary 'adv': 'adverb (fukushi)',
'aux-adj', # auxiliary adjective 'adv-n': 'adverbial noun',
'aux-v', # auxiliary verb 'adv-to': 'adverb taking the "to" particle',
'c', # company name 'arch': 'archaism',
'conj', # conjunction 'ateji': 'ateji (phonetic) reading',
'ctr', # counter 'aux': 'auxiliary',
'exp', # Expressions (phrases, clauses, etc.) 'aux-adj': 'auxiliary adjective',
'f', # female given name 'aux-v': 'auxiliary verb',
'g', # given name, as-yet not classified by sex 'chn': 'children\'s language',
'h', # full (usually family plus given) name of a particular person 'col': 'colloquialism',
'int', # interjection (kandoushi) 'comp': 'computer terminology',
'iv', # irregular verb 'conj': 'conjunction',
'm', # male given name 'ctr': 'counter',
'n', # noun (common) (futsuumeishi) 'derog': 'derogatory term',
'n-adv', # adverbial noun (fukushitekimeishi) 'eK': 'exclusively kanji',
'n-pref', # noun, used as a prefix 'ek': 'exclusively kana',
'n-suf', # noun, used as a suffix 'exp': 'Expressions (phrases, clauses, etc.)',
'n-t', # noun (temporal) (jisoumeishi) 'fam': 'familiar language',
'num', # numeric 'fem': 'female term or language',
'p', # place-name 'food': 'food term',
'pn', # pronoun 'geom': 'geometry term',
'pr', # product name 'gikun': 'gikun (meaning) reading',
'pref' , # prefix 'gram': 'grammatical term',
'prt', # particle 'hon': 'honorific or respectful (sonkeigo) language',
's', # surname 'hum': 'humble (kenjougo) language',
'st', # stations 'iK': 'word containing irregular kanji usage',
'suf', # suffix 'id': 'idiomatic expression',
'u', # person name, either given or surname, as-yet unclassified 'ik': 'word containing irregular kana usage',
'v1', # Ichidan verb 'int': 'interjection (kandoushi)',
'v2a-s', # Nidan verb with 'u' ending (archaic) 'io': 'irregular okurigana usage',
'v4h', # Yodan verb with `hu/fu' ending (archaic) 'iv': 'irregular verb',
'v4r', # Yodan verb with `ru' ending (archaic) 'ling': 'linguistics terminology',
'v5', # Godan verb (not completely classified) 'm-sl': 'manga slang',
'v5aru', # Godan verb - -aru special class 'male': 'male term or language',
'v5b', # Godan verb with `bu' ending 'male-sl': 'male slang',
'v5g', # Godan verb with `gu' ending 'math': 'mathematics',
'v5k', # Godan verb with `ku' ending 'mil': 'military',
'v5k-s', # Godan verb - iku/yuku special class 'n': 'noun (common) (futsuumeishi)',
'v5m', # Godan verb with `mu' ending 'n-adv': 'adverbial noun (fukushitekimeishi)',
'v5n', # Godan verb with `nu' ending 'n-pref': 'noun, used as a prefix',
'v5r', # Godan verb with `ru' ending 'n-suf': 'noun, used as a suffix',
'v5r-i', # Godan verb with `ru' ending (irregular verb) 'n-t': 'noun (temporal) (jisoumeishi)',
'v5s', # Godan verb with `su' ending 'num': 'numeric',
'v5t', # Godan verb with `tsu' ending 'oK': 'word containing out-dated kanji',
'v5u', # Godan verb with `u' ending 'obs': 'obsolete term',
'v5u-s', # Godan verb with `u' ending (special class) 'obsc': 'obscure term',
'v5uru', # Godan verb - uru old class verb (old form of Eru) 'ok': 'out-dated or obsolete kana usage',
'v5z', # Godan verb with `zu' ending 'on-mim': 'onomatopoeic or mimetic word',
'vi', # intransitive verb 'physics': 'physics terminology',
'vk', # kuru verb - special class 'pn': 'pronoun',
'vn', # irregular nu verb 'poet': 'poetical term',
'vs', # noun or participle which takes the aux. verb suru 'pol': 'polite (teineigo) language',
'vs-c', # su verb - precursor to the modern suru 'pref': 'prefix',
'vs-i', # suru verb - irregular 'prt': 'particle',
'vs-s', # suru verb - special class 'rare': 'rare (now replaced by "obsc")',
'vt', # transitive verb 'sens': 'sensitive word',
'vz', # Ichidan verb - zuru verb - (alternative form of -jiru verbs) 'sl': 'slang',
'suf': 'suffix',
'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)',
} }
@ -128,32 +162,49 @@ def parse_edict(path):
for line in load_definitions(path): for line in load_definitions(path):
segments = line.split('/') segments = line.split('/')
expression = segments[0].split(' ') exp_parts = segments[0].split(' ')
term = expression[0] expression = exp_parts[0]
match = re.search('\[([^\]]+)\]', expression[1]) reading_match = re.search('\[([^\]]+)\]', exp_parts[1])
reading = '' if match is None else match.group(1) reading = None if reading_match is None else reading_match.group(1)
glossary = '; '.join(filter(lambda x: len(x) > 0, segments[1:]))
glossary = re.sub('\(\d+\)\s*', '', glossary)
defs = []
tags = [] tags = []
for group in re.findall('\(([^\)\]]+)\)', glossary):
tags.extend(group.split(','))
tags = set(tags).intersection(PARSED_TAGS) for index, dfn in enumerate(filter(None, segments[1:])):
tags = ' '.join(tags) dfn_match = re.search(r'^((?:\((?:[\w\-\,\:]*)*\)\s+)*)(.*)$', dfn)
gloss = dfn_match.group(2)
results.append((term, reading, glossary, tags)) if index == 0:
tags_raw = set(filter(None, re.split(r'[\s\(\),]', dfn_match.group(1))))
tags = tags_raw.intersection(set(PARSED_TAGS.keys()))
return results[1:] 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): def build_dict(output_dir, input_file, parser):
if input_file is not None: if input_file is not None:
base = os.path.splitext(os.path.basename(input_file))[0] base = os.path.splitext(os.path.basename(input_file))[0]
with codecs.open(os.path.join(output_dir, base) + '.csv', 'wb', encoding='utf-8') as fp: with open(os.path.join(output_dir, base) + '.json', 'w') as fp:
for d in parser(input_file): # json.dump(parser(input_file), fp, sort_keys=True, indent=4, separators=(',', ': '))
fp.write('\t'.join(d) + '\n') json.dump(parser(input_file), fp, separators=(',', ':'))
def build(dict_dir, kanjidic, edict, enamdict): def build(dict_dir, kanjidic, edict, enamdict):