You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PaddleSpeech/third_party/python-pinyin/pinyin-data/merge_unihan.py

124 lines
4.3 KiB

E2E/Streaming Transformer/Conformer ASR (#578) * add cmvn and label smoothing loss layer * add layer for transformer * add glu and conformer conv * add torch compatiable hack, mask funcs * not hack size since it exists * add test; attention * add attention, common utils, hack paddle * add audio utils * conformer batch padding mask bug fix #223 * fix typo, python infer fix rnn mem opt name error and batchnorm1d, will be available at 2.0.2 * fix ci * fix ci * add encoder * refactor egs * add decoder * refactor ctc, add ctc align, refactor ckpt, add warmup lr scheduler, cmvn utils * refactor docs * add fix * fix readme * fix bugs, refactor collator, add pad_sequence, fix ckpt bugs * fix docstring * refactor data feed order * add u2 model * refactor cmvn, test * add utils * add u2 config * fix bugs * fix bugs * fix autograd maybe has problem when using inplace operation * refactor data, build vocab; add format data * fix text featurizer * refactor build vocab * add fbank, refactor feature of speech * refactor audio feat * refactor data preprare * refactor data * model init from config * add u2 bins * flake8 * can train * fix bugs, add coverage, add scripts * test can run * fix data * speed perturb with sox * add spec aug * fix for train * fix train logitc * fix logger * log valid loss, time dataset process * using np for speed perturb, remove some debug log of grad clip * fix logger * fix build vocab * fix logger name * using module logger as default * fix * fix install * reorder imports * fix board logger * fix logger * kaldi fbank and mfcc * fix cmvn and print prarams * fix add_eos_sos and cmvn * fix cmvn compute * fix logger and cmvn * fix subsampling, label smoothing loss, remove useless * add notebook test * fix log * fix tb logger * multi gpu valid * fix log * fix log * fix config * fix compute cmvn, need paddle 2.1 * add cmvn notebook * fix layer tools * fix compute cmvn * add rtf * fix decoding * fix layer tools * fix log, add avg script * more avg and test info * fix dataset pickle problem; using 2.1 paddle; num_workers can > 0; ckpt save in exp dir;fix setup.sh; * add vimrc * refactor tiny script, add transformer and stream conf * spm demo; librisppech scripts and confs * fix log * add librispeech scripts * refactor data pipe; fix conf; fix u2 default params * fix bugs * refactor aishell scripts * fix test * fix cmvn * fix s0 scripts * fix ds2 scripts and bugs * fix dev & test dataset filter * fix dataset filter * filter dev * fix ckpt path * filter test, since librispeech will cause OOM, but all test wer will be worse, since mismatch train with test * add comment * add syllable doc * fix ds2 configs * add doc * add pypinyin tools * fix decoder using blank_id=0 * mmseg with pybind11 * format code
4 years ago
# -*- coding: utf-8 -*-
import collections
def code_to_hanzi(code):
hanzi = chr(int(code.replace('U+', '0x'), 16))
return hanzi
def sort_pinyin_dict(pinyin_dict):
return collections.OrderedDict(
sorted(pinyin_dict.items(),
key=lambda item: int(item[0].replace('U+', '0x'), 16))
)
def remove_dup_items(lst):
new_lst = []
for item in lst:
if item not in new_lst:
new_lst.append(item)
return new_lst
def parse_pinyins(fp):
pinyin_map = {}
for line in fp:
line = line.strip()
if line.startswith('#') or not line:
continue
code, pinyin = line.split('#')[0].split(':')
pinyin = ','.join([x.strip() for x in pinyin.split() if x.strip()])
pinyin_map[code.strip()] = pinyin.split(',')
return pinyin_map
def merge(raw_pinyin_map, adjust_pinyin_map, overwrite_pinyin_map):
new_pinyin_map = {}
for code, pinyins in raw_pinyin_map.items():
if code in overwrite_pinyin_map:
pinyins = overwrite_pinyin_map[code]
elif code in adjust_pinyin_map:
pinyins = adjust_pinyin_map[code] + pinyins
new_pinyin_map[code] = remove_dup_items(pinyins)
return new_pinyin_map
def save_data(pinyin_map, writer):
for code, pinyins in pinyin_map.items():
hanzi = code_to_hanzi(code)
line = '{code}: {pinyin} # {hanzi}\n'.format(
code=code, pinyin=','.join(pinyins), hanzi=hanzi
)
writer.write(line)
def extend_pinyins(old_map, new_map, only_no_exists=False):
for code, pinyins in new_map.items():
if only_no_exists: # 只当 code 不存在时才更新
if code not in old_map:
old_map[code] = pinyins
else:
old_map.setdefault(code, []).extend(pinyins)
if __name__ == '__main__':
raw_pinyin_map = {}
with open('kHanyuPinyin.txt') as fp:
khanyupinyin = parse_pinyins(fp)
raw_pinyin_map.update(khanyupinyin)
with open('kXHC1983.txt') as fp:
kxhc1983 = parse_pinyins(fp)
extend_pinyins(raw_pinyin_map, kxhc1983)
with open('nonCJKUI.txt') as fp:
noncjkui = parse_pinyins(fp)
extend_pinyins(raw_pinyin_map, noncjkui)
with open('kMandarin_8105.txt') as fp:
adjust_pinyin_map = parse_pinyins(fp)
extend_pinyins(raw_pinyin_map, adjust_pinyin_map)
with open('kMandarin_overwrite.txt') as fp:
_map = parse_pinyins(fp)
extend_pinyins(adjust_pinyin_map, _map)
extend_pinyins(raw_pinyin_map, adjust_pinyin_map)
with open('kMandarin.txt') as fp:
_map = parse_pinyins(fp)
extend_pinyins(adjust_pinyin_map, _map)
extend_pinyins(raw_pinyin_map, adjust_pinyin_map)
with open('kTGHZ2013.txt') as fp:
_map = parse_pinyins(fp)
extend_pinyins(adjust_pinyin_map, _map)
extend_pinyins(raw_pinyin_map, adjust_pinyin_map)
with open('kHanyuPinlu.txt') as fp:
khanyupinyinlu = parse_pinyins(fp)
extend_pinyins(adjust_pinyin_map, _map)
extend_pinyins(raw_pinyin_map, adjust_pinyin_map)
with open('GBK_PUA.txt') as fp:
pua_pinyin_map = parse_pinyins(fp)
extend_pinyins(raw_pinyin_map, pua_pinyin_map)
with open('kanji.txt') as fp:
_map = parse_pinyins(fp)
extend_pinyins(raw_pinyin_map, _map, only_no_exists=True)
with open('overwrite.txt') as fp:
overwrite_pinyin_map = parse_pinyins(fp)
extend_pinyins(raw_pinyin_map, overwrite_pinyin_map)
new_pinyin_map = merge(raw_pinyin_map, adjust_pinyin_map,
overwrite_pinyin_map)
new_pinyin_map = sort_pinyin_dict(new_pinyin_map)
assert len(new_pinyin_map) == len(raw_pinyin_map)
code_set = set(new_pinyin_map.keys())
assert set(khanyupinyin.keys()) - code_set == set()
assert set(khanyupinyinlu.keys()) - code_set == set()
assert set(kxhc1983.keys()) - code_set == set()
assert set(adjust_pinyin_map.keys()) - code_set == set()
assert set(overwrite_pinyin_map.keys()) - code_set == set()
assert set(pua_pinyin_map.keys()) - code_set == set()
with open('pinyin.txt', 'w') as fp:
fp.write('# version: 0.10.2\n')
fp.write('# source: https://github.com/mozillazg/pinyin-data\n')
save_data(new_pinyin_map, fp)