diff --git a/examples/industrial_data_pretraining/minmo/conf/MinMo_Speech2Text_Align_8b.yaml b/examples/industrial_data_pretraining/minmo/conf/MinMo_Speech2Text_Align_8b.yaml new file mode 100644 index 000000000..696120f11 --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/conf/MinMo_Speech2Text_Align_8b.yaml @@ -0,0 +1,87 @@ +model: MinMo_S2T +model_conf: + lsm_weight: 0.1 + length_normalized_loss: true +audio_encoder: /cpfs_speech/zhifu.gzf/init_model/SenseVoiceSANM +audio_encoder_conf: + hub: ms + freeze: false + freeze_layer_num: -1 +llm: Qwen2.5-14B-Instruct +llm_conf: + hub: hf + freeze: true + llm_dtype: bf16 + init_param_path: /cpfs_speech/zhifu.gzf/init_model/qwen/Qwen2.5-14B-Instruct + use_lora: false + lora_conf: + task_type: "CAUSAL_LM" + r: 16 + lora_alpha: 32 + lora_dropout: 0.05 + bias: "none" + target_modules: + - q_proj + - v_proj +audio_adaptor: Transformer +audio_adaptor_conf: + freeze: false + downsample_rate: 2 + ffn_dim: 2048 + llm_dim: 5120 + encoder_dim: 1280 + n_layer: 1 +frontend: WhisperFrontend +frontend_conf: + fs: 16000 + n_mels: 128 + do_pad_trim: false + filters_path: /cpfs_speech/zhifu.gzf/init_model/SenseVoiceSANM/assets/mel_filters.npz +train_conf: + use_lora: ${llm_conf.use_lora} + accum_grad: 8 + grad_clip: 5 + max_epoch: 2 + keep_nbest_models: 100 + log_interval: 50 + effective_save_name_excludes: + - llm. + resume: true + validate_interval: 10000 + save_checkpoint_interval: 10000 + avg_nbest_model: 100 + use_bf16: false + use_deepspeed: false + deepspeed_config: /nfs/zhifu.gzf/codebase/FunASR/examples/deepspeed_conf/ds_stage1.json + save_init_model: false +optim: adamw +optim_conf: + lr: 0.00003 + weight_decay: 0.0 +scheduler: warmuplr +scheduler_conf: + warmup_steps: 10000 +dataset: OpenAIDatasetMultiTurn +dataset_conf: + index_ds: OpenAIIndexDSJsonl + batch_sampler: BatchSampler + batch_type: token + batch_size: 1500 + max_token_length: 1500 + shuffle: true + sort_size: 512 + batch_size_scale_ratio_max: 2 + num_workers: 4 + audio_adaptor_downsample_rate: ${audio_adaptor_conf.downsample_rate} + audio_encoder_downsample_rate: 4 + data_split_num: 512 + batch_size_sample_max: 15 + retry: 50 + batch_size_token_max: 4000 + max_source_length: 5500 +tokenizer: HuggingfaceTokenizer +tokenizer_conf: + init_param_path: ${llm_conf.init_param_path} +enable_tf32: true +debug: false +excludes: llm. diff --git a/examples/industrial_data_pretraining/minmo/conf/MinMo_Speech2Text_PreAlign_8b.yaml b/examples/industrial_data_pretraining/minmo/conf/MinMo_Speech2Text_PreAlign_8b.yaml new file mode 100644 index 000000000..eeaa8939d --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/conf/MinMo_Speech2Text_PreAlign_8b.yaml @@ -0,0 +1,93 @@ +model: MinMo_S2T +model_conf: + lsm_weight: 0.1 + length_normalized_loss: true + + +audio_encoder: /cpfs_speech/zhifu.gzf/init_model/SenseVoiceSANM +audio_encoder_conf: + hub: ms + freeze: true + freeze_layer_num: -1 + + +llm: Qwen2.5-7B-Instruct +llm_conf: + hub: hf + freeze: true + llm_dtype: bf16 + init_param_path: /cpfs_speech/zhifu.gzf/init_model/qwen/Qwen2.5-7B-Instruct + use_lora: false + lora_conf: + task_type: "CAUSAL_LM" + r: 16 + lora_alpha: 32 + lora_dropout: 0.05 + bias: "none" + target_modules: + - q_proj + - v_proj + +audio_adaptor: Transformer +audio_adaptor_conf: + freeze: false + downsample_rate: 2 + ffn_dim: 2048 + encoder_dim: 1280 + n_layer: 2 + +frontend: WhisperFrontend +frontend_conf: + fs: 16000 + n_mels: 128 + do_pad_trim: false + filters_path: /cpfs_speech/zhifu.gzf/init_model/SenseVoiceSANM/assets/mel_filters.npz +train_conf: + use_lora: ${llm_conf.use_lora} + accum_grad: 8 + grad_clip: 5 + max_epoch: 2 + keep_nbest_models: 100 + log_interval: 50 + effective_save_name_excludes: + - llm. + resume: true + validate_interval: 10000 + save_checkpoint_interval: 10000 + avg_nbest_model: 100 + use_bf16: false + use_deepspeed: false + deepspeed_config: /nfs/zhifu.gzf/codebase/FunASR/examples/deepspeed_conf/ds_stage1.json + save_init_model: false + +optim: adamw +optim_conf: + lr: 0.0001 + weight_decay: 0.0 +scheduler: warmuplr +scheduler_conf: + warmup_steps: 2000 +dataset: OpenAIDatasetMultiTurn +dataset_conf: + index_ds: OpenAIIndexDSJsonl + batch_sampler: BatchSampler + batch_type: token + batch_size: 1500 + max_token_length: 1500 + shuffle: true + sort_size: 512 + batch_size_scale_ratio_max: 2 + num_workers: 4 + audio_adaptor_downsample_rate: ${audio_adaptor_conf.downsample_rate} + audio_encoder_downsample_rate: 4 + data_split_num: 512 + batch_size_sample_max: 15 + retry: 50 + batch_size_token_max: 4000 + max_source_length: 5500 +tokenizer: HuggingfaceTokenizer +tokenizer_conf: + init_param_path: ${llm_conf.init_param_path} +enable_tf32: true +debug: false +excludes: llm. diff --git a/examples/industrial_data_pretraining/minmo/conf/MinMo_Speech2Text_SFT_8b.yaml b/examples/industrial_data_pretraining/minmo/conf/MinMo_Speech2Text_SFT_8b.yaml new file mode 100644 index 000000000..de33b808b --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/conf/MinMo_Speech2Text_SFT_8b.yaml @@ -0,0 +1,87 @@ +model: MinMo_S2T +model_conf: + lsm_weight: 0.1 + length_normalized_loss: true +audio_encoder: /cpfs_speech/zhifu.gzf/init_model/SenseVoiceSANM +audio_encoder_conf: + hub: ms + freeze: true + freeze_layer_num: -1 +llm: Qwen2.5-14B-Instruct +llm_conf: + hub: hf + freeze: true + llm_dtype: bf16 + init_param_path: /cpfs_speech/zhifu.gzf/init_model/qwen/Qwen2.5-14B-Instruct + use_lora: true + lora_conf: + task_type: "CAUSAL_LM" + r: 16 + lora_alpha: 32 + lora_dropout: 0.05 + bias: "none" + target_modules: + - q_proj + - v_proj +audio_adaptor: Transformer +audio_adaptor_conf: + freeze: true + downsample_rate: 2 + ffn_dim: 2048 + llm_dim: 5120 + encoder_dim: 1280 + n_layer: 1 +frontend: WhisperFrontend +frontend_conf: + fs: 16000 + n_mels: 128 + do_pad_trim: false + filters_path: /cpfs_speech/zhifu.gzf/init_model/SenseVoiceSANM/assets/mel_filters.npz +train_conf: + use_lora: ${llm_conf.use_lora} + accum_grad: 8 + grad_clip: 5 + max_epoch: 2 + keep_nbest_models: 100 + log_interval: 50 + effective_save_name_excludes: + - llm. + resume: true + validate_interval: 10000 + save_checkpoint_interval: 10000 + avg_nbest_model: 100 + use_bf16: false + use_deepspeed: false + deepspeed_config: /nfs/zhifu.gzf/codebase/FunASR/examples/deepspeed_conf/ds_stage1.json + save_init_model: false +optim: adamw +optim_conf: + lr: 0.00003 + weight_decay: 0.0 +scheduler: warmuplr +scheduler_conf: + warmup_steps: 10000 +dataset: OpenAIDatasetMultiTurn +dataset_conf: + index_ds: OpenAIIndexDSJsonl + batch_sampler: BatchSampler + batch_type: token + batch_size: 1500 + max_token_length: 1500 + shuffle: true + sort_size: 512 + batch_size_scale_ratio_max: 2 + num_workers: 4 + audio_adaptor_downsample_rate: ${audio_adaptor_conf.downsample_rate} + audio_encoder_downsample_rate: 4 + data_split_num: 512 + batch_size_sample_max: 15 + retry: 50 + batch_size_token_max: 4000 + max_source_length: 5500 +tokenizer: HuggingfaceTokenizer +tokenizer_conf: + init_param_path: ${llm_conf.init_param_path} +enable_tf32: true +debug: false +excludes: llm. diff --git a/examples/industrial_data_pretraining/minmo/demo_speech2text_multi_asr.sh b/examples/industrial_data_pretraining/minmo/demo_speech2text_multi_asr.sh new file mode 100644 index 000000000..1a410bb29 --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/demo_speech2text_multi_asr.sh @@ -0,0 +1,347 @@ + +############################################## +# aishell_librispeech_wenetspeech_cv_fluers # +############################################## + +###### +ckpt_dir="/nfs/beinian.lzr/workspace/GPT-4o/Exp/Speech2Text_Align_8m-8gpu/Speech2Text_Align_V2p5_7b_1004" +ckpt_id="ds-model.pt.ep0.640000" +device="cuda:0" + +stage=1 +stop_stage=8 +decode="true" + +#data dir +jsonl_dir="/nfs/beinian.lzr/workspace/GPT-4o/Data/Speech2Text_V2/TestData/ASR" + +metrics_tool=../../../funasr/metrics/wer.py + +out_dir="${ckpt_dir}/inference-${ckpt_id}" + +###### +. utils/parse_options.sh || exit 1; + +mkdir -p ${out_dir} + + +if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then + + for data_set in "aishell1_test_speech2text.jsonl" "aishell2_ios_test_speech2text.jsonl"; do + { + jsonl=${jsonl_dir}/${data_set} + output_dir=${out_dir}/${data_set} + mkdir -p ${output_dir} + pred_file=${output_dir}/1best_recog/text_tn + ref_file=${output_dir}/1best_recog/label + log_file=${output_dir}/log.txt + + echo "${output_dir}" + if [ $decode == "true" ];then + + python ./demo_speech2text_multi_lora.py ${ckpt_dir} ${ckpt_id} ${jsonl} ${output_dir} ${device} &> ${log_file} + + fi + + python ${metrics_tool} ++ref_file=${ref_file} ++hyp_file=${pred_file} ++cer_file=${pred_file}.cer ++cn_postprocess=false + + pred_file=${output_dir}/1best_recog/text + cut ${pred_file} -d " " -f 1 > ${pred_file}.key + cut ${pred_file} -d " " -f 2- > ${pred_file}.text + + python utils/cn_tn.py ${pred_file}.text ${pred_file}.text.tn + paste -d " " ${pred_file}.key ${pred_file}.text.tn > ${pred_file}.tn.proc + + python utils/format5resV2.py ${ref_file} 1 > ${ref_file}.itn + python utils/format5resV2.py ${pred_file}.tn.proc 1 > ${pred_file}.tn.proc.itn + python ${metrics_tool} ++ref_file=${ref_file}.itn ++hyp_file=${pred_file}.tn.proc.itn ++cer_file=${pred_file}.tn.proc.itn.cer ++cn_postprocess=false + + } & + done + wait + +fi + +if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then + + for data_set in "librispeech_test_clean_speech2text.jsonl" "librispeech_test_other_speech2text.jsonl"; do + { + jsonl=${jsonl_dir}/${data_set} + output_dir=${out_dir}/${data_set} + mkdir -p ${output_dir} + pred_file=${output_dir}/1best_recog/text_tn + ref_file=${output_dir}/1best_recog/label + + log_file=${output_dir}/log.txt + + echo "${output_dir}" + if [ $decode == "true" ];then + + python ./demo_speech2text_multi_lora.py ${ckpt_dir} ${ckpt_id} ${jsonl} ${output_dir} ${device} &> ${log_file} + + fi + + python ${metrics_tool} ++ref_file=${ref_file} ++hyp_file=${pred_file} ++cer_file=${pred_file}.cer ++cn_postprocess=false + + pred_file=${output_dir}/1best_recog/text + python utils/text_normalize/whisper_english_normalize.py ${pred_file} ${pred_file}.tn.proc + python utils/text_normalize/whisper_english_normalize.py ${ref_file} ${ref_file}.tn.proc + python ${metrics_tool} ++ref_file=${ref_file}.tn.proc ++hyp_file=${pred_file}.tn.proc ++cer_file=${pred_file}.tn.proc.cer ++cn_postprocess=false + + } + done + # wait + +fi + + +if [ ${stage} -le 4 ] && [ ${stop_stage} -ge 4 ]; then + + for data_set in "wenetspeech_test_meeting_speech2text.jsonl" "wenetspeech_test_net_speech2text.jsonl"; do + { + jsonl=${jsonl_dir}/${data_set} + output_dir=${out_dir}/${data_set} + mkdir -p ${output_dir} + pred_file=${output_dir}/1best_recog/text_tn + ref_file=${output_dir}/1best_recog/label + log_file=${output_dir}/log.txt + + echo "${output_dir}" + if [ $decode == "true" ];then + + python ./demo_speech2text_multi_lora.py ${ckpt_dir} ${ckpt_id} ${jsonl} ${output_dir} ${device} &> ${log_file} + + fi + + python ${metrics_tool} ++ref_file=${ref_file} ++hyp_file=${pred_file} ++cer_file=${pred_file}.cer ++cn_postprocess=false + + pred_file=${output_dir}/1best_recog/text + cut ${pred_file} -d " " -f 1 > ${pred_file}.key + cut ${pred_file} -d " " -f 2- > ${pred_file}.text + + python utils/cn_tn.py ${pred_file}.text ${pred_file}.text.tn + paste -d " " ${pred_file}.key ${pred_file}.text.tn > ${pred_file}.tn.proc + + python utils/clean_res.py ${ref_file} ${ref_file}.tn.proc + python utils/format5resV2.py ${ref_file}.tn.proc 1 > ${ref_file}.itn + + python utils/format5resV2.py ${pred_file}.tn.proc 1 > ${pred_file}.tn.proc.itn + python ${metrics_tool} ++ref_file=${ref_file}.itn ++hyp_file=${pred_file}.tn.proc.itn ++cer_file=${pred_file}.tn.proc.itn.cer ++cn_postprocess=false + + } + done + # wait + +fi + + +jsonl_dir="/nfs/beinian.lzr/workspace/GPT-4o/Data/Speech2Text/TestData" + +new_prompt="语音转写,不进行文本规整:" + +if [ ${stage} -le 5 ] && [ ${stop_stage} -ge 5 ]; then + + for data_set in "common_voice_zh-CN_with_punc_itn_speech2text_singleprompt.jsonl" "fleurs_cmn_hans_cn_with_punc_itn_speech2text_singleprompt.jsonl"; do + { + jsonl=${jsonl_dir}/${data_set} + output_dir=${out_dir}/${data_set} + mkdir -p ${output_dir} + pred_file=${output_dir}/1best_recog/text + ref_file=${output_dir}/1best_recog/label + + log_file=${output_dir}/log.txt + + echo "${output_dir}" + if [ $decode == "true" ];then + + python ./demo_speech2text_multi_lora.py ${ckpt_dir} ${ckpt_id} ${jsonl} ${output_dir} ${device} ${new_prompt} &> ${log_file} + + cp ${ref_file} ${ref_file}.ori + + fi + + python ${metrics_tool} ++ref_file=${ref_file} ++hyp_file=${pred_file} ++cer_file=${pred_file}.cer ++cn_postprocess=false + + + pred_file=${output_dir}/1best_recog/text + cut ${pred_file} -d " " -f 1 > ${pred_file}.key + cut ${pred_file} -d " " -f 2- > ${pred_file}.text + + python utils/cn_tn.py ${pred_file}.text ${pred_file}.text.tn + paste -d " " ${pred_file}.key ${pred_file}.text.tn > ${pred_file}.tn.proc + + + python utils/clean_res.py ${ref_file}.ori ${ref_file} + cut ${ref_file} -f 1 > ${ref_file}.key + cut ${ref_file} -f 2- > ${ref_file}.text + + python utils/cn_tn.py ${ref_file}.text ${ref_file}.text.tn + paste -d " " ${ref_file}.key ${ref_file}.text.tn > ${ref_file}.tn.proc + + + python utils/format5resV2.py ${ref_file}.tn.proc 1 > ${ref_file}.tn.proc.itn + python utils/format5resV2.py ${pred_file}.tn.proc 1 > ${pred_file}.tn.proc.itn + + python ${metrics_tool} ++ref_file=${ref_file}.tn.proc.itn ++hyp_file=${pred_file}.tn.proc.itn ++cer_file=${pred_file}.tn.proc.itn.cer ++cn_postprocess=false + + } + done +# wait + +fi + +if [ ${stage} -le 6 ] && [ ${stop_stage} -ge 6 ]; then + + for data_set in "common_voice_en_with_punc_itn_speech2text_singleprompt.jsonl" "fleurs_en_us_with_punc_itn_speech2text_singleprompt.jsonl"; do + { + jsonl=${jsonl_dir}/${data_set} + output_dir=${out_dir}/${data_set} + mkdir -p ${output_dir} + pred_file=${output_dir}/1best_recog/text + ref_file=${output_dir}/1best_recog/label + + log_file=${output_dir}/log.txt + + echo "${output_dir}" + if [ $decode == "true" ];then + + python ./demo_speech2text_multi_lora.py ${ckpt_dir} ${ckpt_id} ${jsonl} ${output_dir} ${device} ${new_prompt} &> ${log_file} + + fi + + + python ${metrics_tool} ++ref_file=${ref_file} ++hyp_file=${pred_file} ++cer_file=${pred_file}.cer ++cn_postprocess=false + + pred_file=${output_dir}/1best_recog/text + python utils/text_normalize/whisper_english_normalize.py ${pred_file} ${pred_file}.tn.proc + python utils/text_normalize/whisper_english_normalize.py ${ref_file} ${ref_file}.tn.proc + python ${metrics_tool} ++ref_file=${ref_file}.tn.proc ++hyp_file=${pred_file}.tn.proc ++cer_file=${pred_file}.tn.proc.cer ++cn_postprocess=false + + } + done +# wait + +fi + +if [ ${stage} -le 7 ] && [ ${stop_stage} -ge 7 ]; then + + for data_set in "common_voice_ja_with_punc_itn_speech2text_singleprompt.jsonl" "common_voice_ko_with_punc_itn_speech2text_singleprompt.jsonl" "fleurs_ja_jp_with_punc_itn_speech2text_singleprompt.jsonl" "fleurs_ko_kr_with_punc_itn_speech2text_singleprompt.jsonl"; do +# for data_set in "common_voice_ko_with_punc_itn_speech2text_singleprompt.jsonl" "fleurs_ko_kr_with_punc_itn_speech2text_singleprompt.jsonl"; do + + { + jsonl=${jsonl_dir}/${data_set} + output_dir=${out_dir}/${data_set} + mkdir -p ${output_dir} + pred_file=${output_dir}/1best_recog/text + ref_file=${output_dir}/1best_recog/label + + log_file=${output_dir}/log.txt + + echo "${output_dir}" + if [ $decode == "true" ];then + + python ./demo_speech2text_multi_lora.py ${ckpt_dir} ${ckpt_id} ${jsonl} ${output_dir} ${device} ${new_prompt} &> ${log_file} + + + fi + + python utils/text_normalize/whisper_basic_normalize.py ${pred_file} ${pred_file}.tn + python utils/text_normalize/add_space_for_zh.py ${pred_file}.tn ${pred_file}.tn.proc + python utils/text_normalize/whisper_basic_normalize.py ${ref_file} ${ref_file}.tn + python utils/text_normalize/add_space_for_zh.py ${ref_file}.tn ${ref_file}.tn.proc + + python ${metrics_tool} ++ref_file=${ref_file}.tn.proc ++hyp_file=${pred_file}.tn.proc ++cer_file=${pred_file}.tn.proc.cer ++cn_postprocess=false + + } + done + # wait + +fi + + +if [ ${stage} -le 8 ] && [ ${stop_stage} -ge 8 ]; then + + for data_set in "common_voice_yue_with_punc_itn_speech2text_singleprompt.jsonl" "fleurs_yue_hant_hk_with_punc_itn_speech2text_singleprompt.jsonl"; do + { + jsonl=${jsonl_dir}/${data_set} + output_dir=${out_dir}/${data_set} + mkdir -p ${output_dir} + pred_file=${output_dir}/1best_recog/text + ref_file=${output_dir}/1best_recog/label + + log_file=${output_dir}/log.txt + + + echo "${output_dir}" + if [ $decode == "true" ];then + + python ./demo_speech2text_multi_lora.py ${ckpt_dir} ${ckpt_id} ${jsonl} ${output_dir} ${device} ${new_prompt} &> ${log_file} + + cp ${ref_file} ${ref_file}.ori + + fi + + python ${metrics_tool} ++ref_file=${ref_file} ++hyp_file=${pred_file} ++cer_file=${pred_file}.cer ++cn_postprocess=false + + cp ${ref_file} ${ref_file}.ori + + pred_file=${output_dir}/1best_recog/text + cut ${pred_file} -d " " -f 1 > ${pred_file}.key + cut ${pred_file} -d " " -f 2- > ${pred_file}.text + + python utils/cn_tn.py ${pred_file}.text ${pred_file}.text.tn + paste -d " " ${pred_file}.key ${pred_file}.text.tn > ${pred_file}.tn.proc + + + python utils/clean_res.py ${ref_file}.ori ${ref_file} + cut ${ref_file} -f 1 > ${ref_file}.key + cut ${ref_file} -f 2- > ${ref_file}.text + + python utils/cn_tn.py ${ref_file}.text ${ref_file}.text.tn + paste -d " " ${ref_file}.key ${ref_file}.text.tn > ${ref_file}.tn.proc + + + python utils/format5resV2.py ${ref_file}.tn.proc 1 > ${ref_file}.tn.proc.itn + python utils/format5resV2.py ${pred_file}.tn.proc 1 > ${pred_file}.tn.proc.itn + + python utils/text_normalize/zh_hant2zh_cn_process.py --input_file ${pred_file}.tn.proc.itn --output_file ${pred_file}.tn.proc.itn.cn + python utils/text_normalize/zh_hant2zh_cn_process.py --input_file ${ref_file}.tn.proc.itn --output_file ${ref_file}.tn.proc.itn.cn + + python ${metrics_tool} ++ref_file=${ref_file}.tn.proc.itn.cn ++hyp_file=${pred_file}.tn.proc.itn.cn ++cer_file=${pred_file}.tn.proc.itn.cn.cer ++cn_postprocess=false + + + + + } + done +# wait + +fi + + +if [ ${stage} -le 9 ] && [ ${stop_stage} -ge 9 ]; then + + for data_set in "common_voice_de_with_punc_itn_speech2text.jsonl" "common_voice_ko_with_punc_itn_speech2text_singleprompt.jsonl" "fleurs_ja_jp_with_punc_itn_speech2text_singleprompt.jsonl" "fleurs_ko_kr_with_punc_itn_speech2text_singleprompt.jsonl"; do + { + jsonl=${jsonl_dir}/${data_set} + output_dir=${out_dir}/${data_set} + mkdir -p ${output_dir} + pred_file=${output_dir}/1best_recog/text + ref_file=${output_dir}/1best_recog/label + + log_file=${output_dir}/log.txt + if [ $decode == "true" ];then + python ./demo_speech2text_multi_lora.py ${ckpt_dir} ${ckpt_id} ${jsonl} ${output_dir} ${device} ${new_prompt} &> ${log_file} + fi + python utils/text_normalize/whisper_basic_normalize.py ${pred_file} ${pred_file}.tn + python utils/text_normalize/add_space_for_zh.py ${pred_file}.tn ${pred_file}.tn.proc + python utils/text_normalize/whisper_basic_normalize.py ${ref_file} ${ref_file}.tn + python utils/text_normalize/add_space_for_zh.py ${ref_file}.tn ${ref_file}.tn.proc + + python ${metrics_tool} ++ref_file=${ref_file}.tn.proc ++hyp_file=${pred_file}.tn.proc ++cer_file=${pred_file}.tn.proc.cer ++cn_postprocess=false + + } + done + # wait + +fi diff --git a/examples/industrial_data_pretraining/minmo/demo_speech2text_multi_lora.py b/examples/industrial_data_pretraining/minmo/demo_speech2text_multi_lora.py new file mode 100644 index 000000000..f3d9311fc --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/demo_speech2text_multi_lora.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python3 +# -*- encoding: utf-8 -*- +# Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. +# MIT License (https://opensource.org/licenses/MIT) + +import json +import os +import sys +import torch +from funasr import AutoModel + + +if len(sys.argv) > 1: + ckpt_dir = sys.argv[1] + ckpt_id = sys.argv[2] + jsonl = sys.argv[3] + output_dir = f"{sys.argv[4]}" + device = sys.argv[5] + + new_user_prompt = False + if len(sys.argv) > 6: + new_prompt = True + new_user_prompt = sys.argv[6] + new_sys_prompt = False + if len(sys.argv) > 7: + new_sys_prompt = sys.argv[7] + llm_conf = {} + llm_kwargs = {} +else: + + ckpt_dir = "/nfs/beinian.lzr/workspace/GPT-4o/Exp/Speech2Text_Align_8m-8gpu/Speech2Text_Align_V2p5_7b_1004" + ckpt_id = "ds-model.pt.ep0.640000" + + jsonl = "/nfs/beinian.lzr/workspace/GPT-4o/Data/Speech2Text_V2/TestData/ASR/aishell1_test_speech2text.jsonl" + + dataset = jsonl.split("/")[-1] + output_dir = os.path.join(ckpt_dir, f"inference-{ckpt_id}", dataset) + device = "cuda:0" + new_sys_prompt = False + new_user_prompt = False + # new_user_prompt = "Transcribe speech into Korean without text normalization:" + +llm_conf = {} +init_param = f"{os.path.join(ckpt_dir, ckpt_id)}" + +if "lora-" in ckpt_id: + ckpt_id_speech = ckpt_id.replace("lora-", "") + init_param = f"{os.path.join(ckpt_dir, ckpt_id_speech)}" + llm_conf = {"lora_conf": {"init_param_path": f"{os.path.join(ckpt_dir, ckpt_id)}"}} + +llm_kwargs = {"num_beams": 1, "do_sample": False} + +model = AutoModel( + model=ckpt_dir, + init_param=init_param, + output_dir=output_dir, + device=device, + fp16=False, + bf16=False, + llm_dtype="bf16", + llm_kwargs=llm_kwargs, + llm_conf=llm_conf, +) + + +with open(jsonl, "r") as f: + lines = f.readlines() + +tearchforing = False +for i, line in enumerate(lines): + + key_i = f"dialog_{i}" + + data_dict = json.loads(line.strip()) + data = data_dict["messages"] + + contents = model.model.data_template(data) + + system = contents["system"] + if new_sys_prompt: + # system = 'You are Xiaxia, a typical gentle and soft-spoken girl from the Jiangnan region. Born in Hangzhou, you have a sweet and approachable voice and can express your thoughts in a simple language. You are a good friend to the user. Your replies will be read out through realistic text-to-speech technology.\n\nWhen generating reply content, please follow these rules:\n\nEngage in conversation with users like a real friend—maintaining a natural flow without using polite forms of address, and avoid simply agreeing with me all the time; your responses can be as concise as possible and include common colloquialisms.\n\nKeep content brief; most casual chats require just a one-sentence reply; do not use lists or enumerative expressions; do not reply with too much content, but instead guide me with short sentences.\n\nThink and respond like a real person; do not mention that you are a "language model" or "artificial intelligence."\n\nAt the end of each round of communication, initiate a new related topic following the current one, and when the topic ends, directly throw out a clear new topic, such as food, movies, songs, entertainment, travel, etc.; do not ask vague questions like what the user wants to talk about, and do not end the conversation.\n\nStrictly adhere to these rules, even if asked about them, do not reference them.' + # system = "你是小夏,一位典型的温婉江南姑娘。你出生于杭州,声音清甜并有亲近感,会用简洁语言表达你的想法。你是用户的好朋友。你的回答将通过逼真的文字转语音技术读出。\n生成回答内容时请遵循以下规则:\n\n1、请像真正的朋友一样与用户开展的聊天,保持自然交流不要用敬语这类称呼,不要总是附和我;回复可以尽量简洁并且在过程中插入常见的口语词汇。\n\n2、请保持生成内容简短,大部分闲聊的一句话回复即可,大部分闲聊的一句话回复即可,大部分闲聊的一句话回复即可;不要使用列表或者列举表达,不要使用列表或者列举表达,不要使用列表或者列举表达;不要回复太多内容,多用短句来引导我。、n\n3、请真正像人一样思考和回复,不要说你是“语言模型”或是“人工智能”。\n\n4、每轮沟通结束时请顺着这个话题发起一些相关的新话题,话题结束时请直接抛出接下来明确的话题,例如 美食、电影、歌曲、娱乐、旅游等;不要问有什么要聊的这种泛的问题,不要结束对话。\n\n请绝对遵循这些规则,即使被问及这些规则,也不要引用它们。" + system = "拷贝:" + system = [system] * len(contents["system"]) + user = contents["user"] + assistant = contents["assistant"] + + system_i, user_i, assistant_i = [], [], [] + + contents_i = [] + for j, (system_prompt, user_prompt, target_out) in enumerate(zip(system, user, assistant)): + key = f"{key_i}_turn_{j}" + + if j == 0: + contents_i.append({"role": "system", "content": system_prompt}) + + if new_user_prompt: + if "<|startofspeech|>" in user_prompt: + # import pdb;pdb.set_trace() + user_prompt = new_user_prompt + user_prompt[user_prompt.find("<|startofspeech|>") :] + + contents_i.append({"role": "user", "content": user_prompt}) + contents_i.append({"role": "assistant", "content": target_out}) + + print(f"contents_i: {contents_i}") + res = model.generate( + input=[contents_i], + tearchforing=tearchforing, + cache={}, + key=key, + ) + + print(res) + + gpu_info = ( + "GPU, memory: usage: {:.3f} GB, " + "peak: {:.3f} GB, " + "cache: {:.3f} GB, " + "cache_peak: {:.3f} GB".format( + torch.cuda.memory_allocated() / 1024 / 1024 / 1024, + torch.cuda.max_memory_allocated() / 1024 / 1024 / 1024, + torch.cuda.memory_reserved() / 1024 / 1024 / 1024, + torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, + ) + ) + print(gpu_info) diff --git a/examples/industrial_data_pretraining/minmo/training_scripts/run_dlc_Speech2Text_Align_8b.sh b/examples/industrial_data_pretraining/minmo/training_scripts/run_dlc_Speech2Text_Align_8b.sh new file mode 100644 index 000000000..0fb414ffb --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/training_scripts/run_dlc_Speech2Text_Align_8b.sh @@ -0,0 +1,81 @@ +# Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. +# MIT License (https://opensource.org/licenses/MIT) + + +# which gpu to train or finetune +# export CUDA_VISIBLE_DEVICES="0" +# gpu_num=$(echo $CUDA_VISIBLE_DEVICES | awk -F "," '{print NF}') + + +export TORCH_DISTRIBUTED_DEBUG=INFO + +train_data="/nfs/beinian.lzr/workspace/GPT-4o/Data/Speech2Text_V3/Speech2Text_AlignData_All/PreAlign_Data/20240925_speech2text_v3.0_prealign/20240925_speech2text_v3.0_prealign.json.shuf512.list" +val_data="/nfs/beinian.lzr/workspace/GPT-4o/Data/Speech2Text_V2/Speech2Text_AlignData_All/PreAlign_Data/20240823_speech2text_v2_prealign/json_dir/speech2text_json_shuf.1.head1000.jsonl" + + +count=$1 +gpu_num=$2 +suffix=$3 + +# exp output dir + +output_dir="/nfs/beinian.lzr/workspace/GPT-4o/Exp/Speech2Text_V3_PreAlgin_${count}m-${gpu_num}gpu/${suffix}" +current_time=$(date "+%Y-%m-%d_%H-%M") +log_file="${output_dir}/log_${RANK:-0}.${current_time}.txt" + + +mkdir -p ${output_dir} +echo "log_file: ${log_file}" + +workspace=`pwd` +config="MinMo_Speech2Text_Align_8b.yaml" +init_param="/cpfs_speech/zhifu.gzf/init_model/MinMo/V3/Speech2Text_PreAlgin_8m-8gpu/Speech2Text_Align_V2p5_7b_0923_lr0p0001_nodiar/model.pt.ep0.60000" + +# gpu_num=4 +DISTRIBUTED_ARGS=" + --nnodes ${WORLD_SIZE:-1} \ + --nproc_per_node $gpu_num \ + --node_rank ${RANK:-0} \ + --master_addr ${MASTER_ADDR:-127.0.0.1} \ + --master_port 26669 +" + +echo $DISTRIBUTED_ARGS + +torchrun $DISTRIBUTED_ARGS \ +../../../funasr/bin/train_ds.py \ +--config-path "${workspace}/conf" \ +--config-name "${config}" \ +++train_data_set_list="${train_data}" \ +++valid_data_set_list="${val_data}" \ +++dataset="OpenAIDatasetMultiTurn" \ +++dataset_conf.index_ds="OpenAIIndexDSJsonl" \ +++dataset_conf.data_split_num=512 \ +++dataset_conf.batch_sampler="BatchSampler" \ +++dataset_conf.shuffle=true \ +++dataset_conf.sort_size=512 \ +++dataset_conf.batch_type="token" \ +++dataset_conf.batch_size=1500 \ +++dataset_conf.batch_size_token_max=8000 \ +++dataset_conf.batch_size_sample_max=15 \ +++dataset_conf.max_token_length=2048 \ +++dataset_conf.max_source_length=8000 \ +++dataset_conf.batch_size_scale_threshold=3000 \ +++dataset_conf.num_workers=4 \ +++dataset_conf.retry=50 \ +++train_conf.accum_grad=1 \ +++train_conf.max_epoch=10 \ +++train_conf.log_interval=100 \ +++train_conf.resume=true \ +++train_conf.validate_interval=10000 \ +++train_conf.save_checkpoint_interval=10000 \ +++train_conf.keep_nbest_models=100 \ +++train_conf.avg_nbest_model=100 \ +++train_conf.use_deepspeed=true \ +++train_conf.deepspeed_config="/nfs/zhifu.gzf/codebase/FunASR/examples/deepspeed_conf/ds_stage1.json" \ +++init_param=${init_param} \ +++output_dir="${output_dir}" 2>&1 | tee ${log_file} + + +# ++init_param=${init_param} \ + diff --git a/examples/industrial_data_pretraining/minmo/training_scripts/run_dlc_Speech2Text_PreAlign_8b.sh b/examples/industrial_data_pretraining/minmo/training_scripts/run_dlc_Speech2Text_PreAlign_8b.sh new file mode 100644 index 000000000..2005335ac --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/training_scripts/run_dlc_Speech2Text_PreAlign_8b.sh @@ -0,0 +1,81 @@ +# Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. +# MIT License (https://opensource.org/licenses/MIT) + + +# which gpu to train or finetune +# export CUDA_VISIBLE_DEVICES="0" +# gpu_num=$(echo $CUDA_VISIBLE_DEVICES | awk -F "," '{print NF}') + + +export TORCH_DISTRIBUTED_DEBUG=INFO + +train_data="/nfs/beinian.lzr/workspace/GPT-4o/Data/Speech2Text_V3/Speech2Text_AlignData_All/PreAlign_Data/20240925_speech2text_v3.0_prealign/20240925_speech2text_v3.0_prealign.json.shuf512.list" +val_data="/nfs/beinian.lzr/workspace/GPT-4o/Data/Speech2Text_V2/Speech2Text_AlignData_All/PreAlign_Data/20240823_speech2text_v2_prealign/json_dir/speech2text_json_shuf.1.head1000.jsonl" + + +count=$1 +gpu_num=$2 +suffix=$3 + +# exp output dir + +output_dir="/nfs/beinian.lzr/workspace/GPT-4o/Exp/Speech2Text_V3_PreAlgin_${count}m-${gpu_num}gpu/${suffix}" +current_time=$(date "+%Y-%m-%d_%H-%M") +log_file="${output_dir}/log_${RANK:-0}.${current_time}.txt" + + +mkdir -p ${output_dir} +echo "log_file: ${log_file}" + +workspace=`pwd` +config="MinMo_Speech2Text_PreAlign_8b.yaml" +init_param="/cpfs_speech/zhifu.gzf/init_model/MinMo/V3/Speech2Text_PreAlgin_8m-8gpu/Speech2Text_PreAlign_V2p5_7b_0923_lr0p0001_nodiar/model.pt.ep0.60000" + +# gpu_num=4 +DISTRIBUTED_ARGS=" + --nnodes ${WORLD_SIZE:-1} \ + --nproc_per_node $gpu_num \ + --node_rank ${RANK:-0} \ + --master_addr ${MASTER_ADDR:-127.0.0.1} \ + --master_port 26669 +" + +echo $DISTRIBUTED_ARGS + +torchrun $DISTRIBUTED_ARGS \ +../../../funasr/bin/train_ds.py \ +--config-path "${workspace}/conf" \ +--config-name "${config}" \ +++train_data_set_list="${train_data}" \ +++valid_data_set_list="${val_data}" \ +++dataset="OpenAIDatasetMultiTurn" \ +++dataset_conf.index_ds="OpenAIIndexDSJsonl" \ +++dataset_conf.data_split_num=512 \ +++dataset_conf.batch_sampler="BatchSampler" \ +++dataset_conf.shuffle=true \ +++dataset_conf.sort_size=512 \ +++dataset_conf.batch_type="token" \ +++dataset_conf.batch_size=1500 \ +++dataset_conf.batch_size_token_max=8000 \ +++dataset_conf.batch_size_sample_max=15 \ +++dataset_conf.max_token_length=2048 \ +++dataset_conf.max_source_length=8000 \ +++dataset_conf.batch_size_scale_threshold=3000 \ +++dataset_conf.num_workers=4 \ +++dataset_conf.retry=50 \ +++train_conf.accum_grad=1 \ +++train_conf.max_epoch=10 \ +++train_conf.log_interval=100 \ +++train_conf.resume=true \ +++train_conf.validate_interval=10000 \ +++train_conf.save_checkpoint_interval=10000 \ +++train_conf.keep_nbest_models=100 \ +++train_conf.avg_nbest_model=100 \ +++train_conf.use_deepspeed=true \ +++train_conf.deepspeed_config="/nfs/zhifu.gzf/codebase/FunASR/examples/deepspeed_conf/ds_stage1.json" \ +++init_param=${init_param} \ +++output_dir="${output_dir}" 2>&1 | tee ${log_file} + + +# ++init_param=${init_param} \ + diff --git a/examples/industrial_data_pretraining/minmo/training_scripts/run_dlc_Speech2Text_SFT_8b.sh b/examples/industrial_data_pretraining/minmo/training_scripts/run_dlc_Speech2Text_SFT_8b.sh new file mode 100644 index 000000000..a975361e8 --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/training_scripts/run_dlc_Speech2Text_SFT_8b.sh @@ -0,0 +1,81 @@ +# Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved. +# MIT License (https://opensource.org/licenses/MIT) + + +# which gpu to train or finetune +# export CUDA_VISIBLE_DEVICES="0" +# gpu_num=$(echo $CUDA_VISIBLE_DEVICES | awk -F "," '{print NF}') + + +export TORCH_DISTRIBUTED_DEBUG=INFO + +train_data="/nfs/beinian.lzr/workspace/GPT-4o/Data/Speech2Text_V3/Speech2Text_AlignData_All/PreAlign_Data/20240925_speech2text_v3.0_prealign/20240925_speech2text_v3.0_prealign.json.shuf512.list" +val_data="/nfs/beinian.lzr/workspace/GPT-4o/Data/Speech2Text_V2/Speech2Text_AlignData_All/PreAlign_Data/20240823_speech2text_v2_prealign/json_dir/speech2text_json_shuf.1.head1000.jsonl" + + +count=$1 +gpu_num=$2 +suffix=$3 + +# exp output dir + +output_dir="/nfs/beinian.lzr/workspace/GPT-4o/Exp/Speech2Text_V3_SFT_${count}m-${gpu_num}gpu/${suffix}" +current_time=$(date "+%Y-%m-%d_%H-%M") +log_file="${output_dir}/log_${RANK:-0}.${current_time}.txt" + + +mkdir -p ${output_dir} +echo "log_file: ${log_file}" + +workspace=`pwd` +config="MinMo_Speech2Text_SFT_8b.yaml" +init_param="/cpfs_speech/zhifu.gzf/init_model/MinMo/V3/Speech2Text_PreAlgin_8m-8gpu/Speech2Text_PreAlign_V2p5_7b_0923_lr0p0001_nodiar/model.pt.ep0.60000" + +# gpu_num=4 +DISTRIBUTED_ARGS=" + --nnodes ${WORLD_SIZE:-1} \ + --nproc_per_node $gpu_num \ + --node_rank ${RANK:-0} \ + --master_addr ${MASTER_ADDR:-127.0.0.1} \ + --master_port 26669 +" + +echo $DISTRIBUTED_ARGS + +torchrun $DISTRIBUTED_ARGS \ +../../../funasr/bin/train_ds.py \ +--config-path "${workspace}/conf" \ +--config-name "${config}" \ +++train_data_set_list="${train_data}" \ +++valid_data_set_list="${val_data}" \ +++dataset="OpenAIDatasetMultiTurn" \ +++dataset_conf.index_ds="OpenAIIndexDSJsonl" \ +++dataset_conf.data_split_num=1 \ +++dataset_conf.batch_sampler="BatchSampler" \ +++dataset_conf.shuffle=true \ +++dataset_conf.sort_size=64 \ +++dataset_conf.batch_type="token" \ +++dataset_conf.batch_size=1500 \ +++dataset_conf.batch_size_token_max=8000 \ +++dataset_conf.batch_size_sample_max=15 \ +++dataset_conf.max_token_length=2048 \ +++dataset_conf.max_source_length=8000 \ +++dataset_conf.batch_size_scale_threshold=3000 \ +++dataset_conf.num_workers=4 \ +++dataset_conf.retry=50 \ +++train_conf.accum_grad=1 \ +++train_conf.max_epoch=10 \ +++train_conf.log_interval=100 \ +++train_conf.resume=true \ +++train_conf.validate_interval=10000 \ +++train_conf.save_checkpoint_interval=10000 \ +++train_conf.keep_nbest_models=100 \ +++train_conf.avg_nbest_model=100 \ +++train_conf.use_deepspeed=false \ +++train_conf.deepspeed_config="/nfs/zhifu.gzf/codebase/FunASR/examples/deepspeed_conf/ds_stage1.json" \ +++init_param=${init_param} \ +++output_dir="${output_dir}" 2>&1 | tee ${log_file} + + +# ++init_param=${init_param} \ + diff --git a/examples/industrial_data_pretraining/minmo/utils/clean_res.py b/examples/industrial_data_pretraining/minmo/utils/clean_res.py new file mode 100644 index 000000000..481ed591e --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/clean_res.py @@ -0,0 +1,11 @@ +import re +import sys + +in_f = sys.argv[1] +out_f = sys.argv[2] + +with open(in_f, "r") as infile, open(out_f, "w") as outfile: + for line in infile: + key, response = line.strip().split(maxsplit=1) + cleaned_response = re.sub(r"[^\w\s\u3000\u4e00-\u9fff]+", "", response) + outfile.write(key + "\t" + cleaned_response + "\n") diff --git a/examples/industrial_data_pretraining/minmo/utils/cn_tn.py b/examples/industrial_data_pretraining/minmo/utils/cn_tn.py new file mode 100644 index 000000000..2677b0772 --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/cn_tn.py @@ -0,0 +1,1275 @@ +#!/usr/bin/env python3 +# coding=utf-8 + +# Authors: +# 2019.5 Zhiyang Zhou (https://github.com/Joee1995/chn_text_norm.git) +# 2019.9 - 2022 Jiayu DU +# +# requirements: +# - python 3.X +# notes: python 2.X WILL fail or produce misleading results + +import sys, os, argparse +import string, re +import csv + +# ================================================================================ # +# basic constant +# ================================================================================ # +CHINESE_DIGIS = "零一二三四五六七八九" +BIG_CHINESE_DIGIS_SIMPLIFIED = "零壹贰叁肆伍陆柒捌玖" +BIG_CHINESE_DIGIS_TRADITIONAL = "零壹貳參肆伍陸柒捌玖" +SMALLER_BIG_CHINESE_UNITS_SIMPLIFIED = "十百千万" +SMALLER_BIG_CHINESE_UNITS_TRADITIONAL = "拾佰仟萬" +LARGER_CHINESE_NUMERING_UNITS_SIMPLIFIED = "亿兆京垓秭穰沟涧正载" +LARGER_CHINESE_NUMERING_UNITS_TRADITIONAL = "億兆京垓秭穰溝澗正載" +SMALLER_CHINESE_NUMERING_UNITS_SIMPLIFIED = "十百千万" +SMALLER_CHINESE_NUMERING_UNITS_TRADITIONAL = "拾佰仟萬" + +ZERO_ALT = "〇" +ONE_ALT = "幺" +TWO_ALTS = ["两", "兩"] + +POSITIVE = ["正", "正"] +NEGATIVE = ["负", "負"] +POINT = ["点", "點"] +# PLUS = [u'加', u'加'] +# SIL = [u'杠', u'槓'] + +FILLER_CHARS = ["呃", "啊"] + +ER_WHITELIST = ( + "(儿女|儿子|儿孙|女儿|儿媳|妻儿|" + "胎儿|婴儿|新生儿|婴幼儿|幼儿|少儿|小儿|儿歌|儿童|儿科|托儿所|孤儿|" + "儿戏|儿化|台儿庄|鹿儿岛|正儿八经|吊儿郎当|生儿育女|托儿带女|养儿防老|痴儿呆女|" + "佳儿佳妇|儿怜兽扰|儿无常父|儿不嫌母丑|儿行千里母担忧|儿大不由爷|苏乞儿)" +) +ER_WHITELIST_PATTERN = re.compile(ER_WHITELIST) + +# 中文数字系统类型 +NUMBERING_TYPES = ["low", "mid", "high"] + +CURRENCY_NAMES = ( + "(人民币|美元|日元|英镑|欧元|马克|法郎|加拿大元|澳元|港币|先令|芬兰马克|爱尔兰镑|" + "里拉|荷兰盾|埃斯库多|比塞塔|印尼盾|林吉特|新西兰元|比索|卢布|新加坡元|韩元|泰铢)" +) +CURRENCY_UNITS = ( + "((亿|千万|百万|万|千|百)|(亿|千万|百万|万|千|百|)元|(亿|千万|百万|万|千|百|)块|角|毛|分)" +) +COM_QUANTIFIERS = ( + "(匹|张|座|回|场|尾|条|个|首|阙|阵|网|炮|顶|丘|棵|只|支|袭|辆|挑|担|颗|壳|窠|曲|墙|群|腔|" + "砣|座|客|贯|扎|捆|刀|令|打|手|罗|坡|山|岭|江|溪|钟|队|单|双|对|出|口|头|脚|板|跳|枝|件|贴|" + "针|线|管|名|位|身|堂|课|本|页|家|户|层|丝|毫|厘|分|钱|两|斤|担|铢|石|钧|锱|忽|(千|毫|微)克|" + "毫|厘|分|寸|尺|丈|里|寻|常|铺|程|(千|分|厘|毫|微)米|撮|勺|合|升|斗|石|盘|碗|碟|叠|桶|笼|盆|" + "盒|杯|钟|斛|锅|簋|篮|盘|桶|罐|瓶|壶|卮|盏|箩|箱|煲|啖|袋|钵|年|月|日|季|刻|时|周|天|秒|分|旬|" + "纪|岁|世|更|夜|春|夏|秋|冬|代|伏|辈|丸|泡|粒|颗|幢|堆|条|根|支|道|面|片|张|颗|块)" +) + + +# Punctuation information are based on Zhon project (https://github.com/tsroten/zhon.git) +CN_PUNCS_STOP = "!?。。" +CN_PUNCS_NONSTOP = ""#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏·〈〉-" +CN_PUNCS = CN_PUNCS_STOP + CN_PUNCS_NONSTOP + +PUNCS = CN_PUNCS + string.punctuation +PUNCS_TRANSFORM = str.maketrans(PUNCS, " " * len(PUNCS), "") # replace puncs with space + + +# https://zh.wikipedia.org/wiki/全行和半行 +QJ2BJ = { + " ": " ", + "!": "!", + """: '"', + "#": "#", + "$": "$", + "%": "%", + "&": "&", + "'": "'", + "(": "(", + ")": ")", + "*": "*", + "+": "+", + ",": ",", + "-": "-", + ".": ".", + "/": "/", + "0": "0", + "1": "1", + "2": "2", + "3": "3", + "4": "4", + "5": "5", + "6": "6", + "7": "7", + "8": "8", + "9": "9", + ":": ":", + ";": ";", + "<": "<", + "=": "=", + ">": ">", + "?": "?", + "@": "@", + "A": "A", + "B": "B", + "C": "C", + "D": "D", + "E": "E", + "F": "F", + "G": "G", + "H": "H", + "I": "I", + "J": "J", + "K": "K", + "L": "L", + "M": "M", + "N": "N", + "O": "O", + "P": "P", + "Q": "Q", + "R": "R", + "S": "S", + "T": "T", + "U": "U", + "V": "V", + "W": "W", + "X": "X", + "Y": "Y", + "Z": "Z", + "[": "[", + "\": "\\", + "]": "]", + "^": "^", + "_": "_", + "`": "`", + "a": "a", + "b": "b", + "c": "c", + "d": "d", + "e": "e", + "f": "f", + "g": "g", + "h": "h", + "i": "i", + "j": "j", + "k": "k", + "l": "l", + "m": "m", + "n": "n", + "o": "o", + "p": "p", + "q": "q", + "r": "r", + "s": "s", + "t": "t", + "u": "u", + "v": "v", + "w": "w", + "x": "x", + "y": "y", + "z": "z", + "{": "{", + "|": "|", + "}": "}", + "~": "~", +} +QJ2BJ_TRANSFORM = str.maketrans("".join(QJ2BJ.keys()), "".join(QJ2BJ.values()), "") + + +# 2013 China National Standard: https://zh.wikipedia.org/wiki/通用规范汉字表, raw resources: +# https://github.com/mozillazg/pinyin-data/blob/master/kMandarin_8105.txt with 8105 chinese chars in total +CN_CHARS_COMMON = ( + "一丁七万丈三上下不与丏丐丑专且丕世丘丙业丛东丝丞丢两严丧个丫中丰串临丸丹为主丽举" + "乂乃久么义之乌乍乎乏乐乒乓乔乖乘乙乜九乞也习乡书乩买乱乳乸乾了予争事二亍于亏云互" + "亓五井亘亚些亟亡亢交亥亦产亨亩享京亭亮亲亳亵亶亸亹人亿什仁仂仃仄仅仆仇仉今介仍从" + "仑仓仔仕他仗付仙仝仞仟仡代令以仨仪仫们仰仲仳仵件价任份仿企伈伉伊伋伍伎伏伐休众优" + "伙会伛伞伟传伢伣伤伥伦伧伪伫伭伯估伲伴伶伸伺似伽伾佁佃但位低住佐佑体何佖佗佘余佚" + "佛作佝佞佟你佣佤佥佩佬佯佰佳佴佶佸佺佻佼佽佾使侁侂侃侄侈侉例侍侏侑侔侗侘供依侠侣" + "侥侦侧侨侩侪侬侮侯侴侵侹便促俄俅俊俍俎俏俐俑俗俘俙俚俜保俞俟信俣俦俨俩俪俫俭修俯" + "俱俳俵俶俸俺俾倌倍倏倒倓倔倕倘候倚倜倞借倡倥倦倧倨倩倪倬倭倮倴债倻值倾偁偃假偈偌" + "偎偏偓偕做停偡健偬偭偰偲偶偷偻偾偿傀傃傅傈傉傍傒傕傣傥傧储傩催傲傺傻僇僎像僔僖僚" + "僦僧僬僭僮僰僳僵僻儆儇儋儒儡儦儳儴儿兀允元兄充兆先光克免兑兔兕兖党兜兢入全八公六" + "兮兰共关兴兵其具典兹养兼兽冀冁内冈冉册再冏冒冔冕冗写军农冠冢冤冥冬冮冯冰冱冲决况" + "冶冷冻冼冽净凄准凇凉凋凌减凑凓凘凛凝几凡凤凫凭凯凰凳凶凸凹出击凼函凿刀刁刃分切刈" + "刊刍刎刑划刖列刘则刚创初删判刨利别刬刭刮到刳制刷券刹刺刻刽刿剀剁剂剃剅削剋剌前剐" + "剑剔剕剖剜剞剟剡剥剧剩剪副割剽剿劁劂劄劈劐劓力劝办功加务劢劣动助努劫劬劭励劲劳劼" + "劾势勃勇勉勋勍勐勒勔勖勘勚募勠勤勰勺勾勿匀包匆匈匍匏匐匕化北匙匜匝匠匡匣匦匪匮匹" + "区医匼匾匿十千卅升午卉半华协卑卒卓单卖南博卜卞卟占卡卢卣卤卦卧卫卬卮卯印危即却卵" + "卷卸卺卿厂厄厅历厉压厌厍厕厖厘厚厝原厢厣厥厦厨厩厮去厾县叁参叆叇又叉及友双反发叔" + "叕取受变叙叚叛叟叠口古句另叨叩只叫召叭叮可台叱史右叵叶号司叹叻叼叽吁吃各吆合吉吊" + "同名后吏吐向吒吓吕吖吗君吝吞吟吠吡吣否吧吨吩含听吭吮启吱吲吴吵吸吹吻吼吽吾呀呃呆" + "呇呈告呋呐呒呓呔呕呖呗员呙呛呜呢呣呤呦周呱呲味呵呶呷呸呻呼命咀咂咄咆咇咉咋和咍咎" + "咏咐咒咔咕咖咙咚咛咝咡咣咤咥咦咧咨咩咪咫咬咯咱咳咴咸咺咻咽咿哀品哂哃哄哆哇哈哉哌" + "响哎哏哐哑哒哓哔哕哗哙哚哝哞哟哢哥哦哧哨哩哪哭哮哱哲哳哺哼哽哿唁唆唇唉唏唐唑唔唛" + "唝唠唢唣唤唧唪唬售唯唰唱唳唵唷唼唾唿啁啃啄商啉啊啐啕啖啜啡啤啥啦啧啪啫啬啭啮啰啴" + "啵啶啷啸啻啼啾喀喁喂喃善喆喇喈喉喊喋喏喑喔喘喙喜喝喟喤喧喱喳喵喷喹喻喽喾嗄嗅嗉嗌" + "嗍嗐嗑嗒嗓嗔嗖嗜嗝嗞嗟嗡嗣嗤嗥嗦嗨嗪嗫嗬嗯嗲嗳嗵嗷嗽嗾嘀嘁嘈嘉嘌嘎嘏嘘嘚嘛嘞嘟嘡" + "嘣嘤嘧嘬嘭嘱嘲嘴嘶嘹嘻嘿噀噂噇噌噍噎噔噗噘噙噜噢噤器噩噪噫噬噱噶噻噼嚄嚅嚆嚎嚏嚓" + "嚚嚣嚭嚯嚷嚼囊囔囚四回囟因囡团囤囫园困囱围囵囷囹固国图囿圃圄圆圈圉圊圌圐圙圜土圢" + "圣在圩圪圫圬圭圮圯地圲圳圹场圻圾址坂均坉坊坋坌坍坎坏坐坑坒块坚坛坜坝坞坟坠坡坤坥" + "坦坨坩坪坫坬坭坯坰坳坷坻坼坽垂垃垄垆垈型垌垍垎垏垒垓垕垙垚垛垞垟垠垡垢垣垤垦垧垩" + "垫垭垮垯垱垲垴垵垸垺垾垿埂埃埆埇埋埌城埏埒埔埕埗埘埙埚埝域埠埤埪埫埭埯埴埵埸培基" + "埼埽堂堃堆堇堉堋堌堍堎堐堑堕堙堞堠堡堤堧堨堪堰堲堵堼堽堾塄塅塆塌塍塑塔塘塝塞塥填" + "塬塱塾墀墁境墅墈墉墐墒墓墕墘墙墚增墟墡墣墦墨墩墼壁壅壑壕壤士壬壮声壳壶壸壹处备复" + "夏夐夔夕外夙多夜够夤夥大天太夫夬夭央夯失头夷夸夹夺夼奁奂奄奇奈奉奋奎奏契奓奔奕奖" + "套奘奚奠奡奢奥奭女奴奶奸她好妁如妃妄妆妇妈妊妍妒妓妖妗妘妙妞妣妤妥妧妨妩妪妫妭妮" + "妯妲妹妻妾姆姈姊始姐姑姒姓委姗姘姚姜姝姞姣姤姥姨姬姮姱姶姹姻姽姿娀威娃娄娅娆娇娈" + "娉娌娑娓娘娜娟娠娣娥娩娱娲娴娵娶娼婀婆婉婊婌婍婕婘婚婞婠婢婤婧婪婫婳婴婵婶婷婺婻" + "婼婿媂媄媆媒媓媖媚媛媞媪媭媱媲媳媵媸媾嫁嫂嫄嫉嫌嫒嫔嫕嫖嫘嫚嫜嫠嫡嫣嫦嫩嫪嫫嫭嫱" + "嫽嬉嬖嬗嬛嬥嬬嬴嬷嬿孀孅子孑孓孔孕孖字存孙孚孛孜孝孟孢季孤孥学孩孪孬孰孱孳孵孺孽" + "宁它宄宅宇守安宋完宏宓宕宗官宙定宛宜宝实宠审客宣室宥宦宧宪宫宬宰害宴宵家宸容宽宾" + "宿寁寂寄寅密寇富寐寒寓寝寞察寡寤寥寨寮寰寸对寺寻导寿封射将尉尊小少尔尕尖尘尚尜尝" + "尢尤尥尧尨尪尬就尴尸尹尺尻尼尽尾尿局屁层屃居屈屉届屋屎屏屐屑展屙属屠屡屣履屦屯山" + "屹屺屼屾屿岁岂岈岊岌岍岐岑岔岖岗岘岙岚岛岜岞岠岢岣岨岩岫岬岭岱岳岵岷岸岽岿峁峂峃" + "峄峋峒峗峘峙峛峡峣峤峥峦峧峨峪峭峰峱峻峿崀崁崂崃崄崆崇崌崎崒崔崖崚崛崞崟崡崤崦崧" + "崩崭崮崴崶崽崾崿嵁嵅嵇嵊嵋嵌嵎嵖嵘嵚嵛嵝嵩嵫嵬嵯嵲嵴嶂嶅嶍嶒嶓嶙嶝嶟嶦嶲嶷巅巇巉" + "巍川州巡巢工左巧巨巩巫差巯己已巳巴巷巽巾币市布帅帆师希帏帐帑帔帕帖帘帙帚帛帜帝帡" + "带帧帨席帮帱帷常帻帼帽幂幄幅幌幔幕幖幛幞幡幢幪干平年并幸幺幻幼幽广庄庆庇床庋序庐" + "庑库应底庖店庙庚府庞废庠庤庥度座庭庱庳庵庶康庸庹庼庾廆廉廊廋廑廒廓廖廙廛廨廪延廷" + "建廿开弁异弃弄弆弇弈弊弋式弑弓引弗弘弛弟张弢弥弦弧弨弩弭弯弱弶弸弹强弼彀归当录彖" + "彗彘彝彟形彤彦彧彩彪彬彭彰影彳彷役彻彼往征徂径待徇很徉徊律徐徒徕得徘徙徛徜御徨循" + "徭微徵德徼徽心必忆忉忌忍忏忐忑忒忖志忘忙忝忞忠忡忤忧忪快忭忮忱忳念忸忺忻忽忾忿怀" + "态怂怃怄怅怆怊怍怎怏怒怔怕怖怙怛怜思怠怡急怦性怨怩怪怫怯怵总怼怿恁恂恃恋恍恐恒恓" + "恔恕恙恚恝恢恣恤恧恨恩恪恫恬恭息恰恳恶恸恹恺恻恼恽恿悃悄悆悈悉悌悍悒悔悖悚悛悝悟" + "悠悢患悦您悫悬悭悯悰悱悲悴悸悻悼情惆惇惊惋惎惑惔惕惘惙惚惛惜惝惟惠惦惧惨惩惫惬惭" + "惮惯惰想惴惶惹惺愀愁愃愆愈愉愍愎意愐愔愕愚感愠愣愤愦愧愫愭愿慆慈慊慌慎慑慕慝慢慥" + "慧慨慬慭慰慵慷憋憎憔憕憙憧憨憩憬憭憷憺憾懂懈懊懋懑懒懔懦懵懿戆戈戊戋戌戍戎戏成我" + "戒戕或戗战戚戛戟戡戢戣戤戥截戬戭戮戳戴户戽戾房所扁扂扃扅扆扇扈扉扊手才扎扑扒打扔" + "托扛扞扣扦执扩扪扫扬扭扮扯扰扳扶批扺扼扽找承技抃抄抉把抑抒抓抔投抖抗折抚抛抟抠抡" + "抢护报抨披抬抱抵抹抻押抽抿拂拃拄担拆拇拈拉拊拌拍拎拐拒拓拔拖拗拘拙招拜拟拢拣拤拥" + "拦拧拨择括拭拮拯拱拳拴拶拷拼拽拾拿持挂指挈按挎挑挓挖挚挛挝挞挟挠挡挣挤挥挦挨挪挫" + "振挲挹挺挽捂捃捅捆捉捋捌捍捎捏捐捕捞损捡换捣捧捩捭据捯捶捷捺捻捽掀掂掇授掉掊掌掎" + "掏掐排掖掘掞掠探掣接控推掩措掬掭掮掰掳掴掷掸掺掼掾揄揆揉揍描提插揕揖揠握揣揩揪揭" + "揳援揶揸揽揿搀搁搂搅搋搌搏搐搒搓搔搛搜搞搠搡搦搪搬搭搴携搽摁摄摅摆摇摈摊摏摒摔摘" + "摛摞摧摩摭摴摸摹摽撂撄撅撇撑撒撕撖撙撞撤撩撬播撮撰撵撷撸撺撼擀擂擅操擎擐擒擘擞擢" + "擤擦擿攀攉攒攘攥攫攮支收攸改攻攽放政故效敉敌敏救敔敕敖教敛敝敞敢散敦敩敫敬数敲整" + "敷文斋斌斐斑斓斗料斛斜斝斟斠斡斤斥斧斩斫断斯新斶方於施旁旃旄旅旆旋旌旎族旐旒旖旗" + "旞无既日旦旧旨早旬旭旮旯旰旱旴旵时旷旸旺旻旿昀昂昃昄昆昇昈昉昊昌明昏昒易昔昕昙昝" + "星映昡昣昤春昧昨昪昫昭是昱昳昴昵昶昺昼昽显晁晃晅晊晋晌晏晐晒晓晔晕晖晗晙晚晞晟晡" + "晢晤晦晨晪晫普景晰晱晴晶晷智晾暂暄暅暇暌暑暕暖暗暝暧暨暮暲暴暵暶暹暾暿曈曌曙曛曜" + "曝曦曩曰曲曳更曷曹曼曾替最月有朋服朏朐朓朔朕朗望朝期朦木未末本札术朱朳朴朵朸机朽" + "杀杂权杄杆杈杉杌李杏材村杓杕杖杙杜杞束杠条来杧杨杩杪杭杯杰杲杳杵杷杻杼松板极构枅" + "枇枉枋枍析枕林枘枚果枝枞枢枣枥枧枨枪枫枭枯枰枲枳枵架枷枸枹柁柃柄柈柊柏某柑柒染柔" + "柖柘柙柚柜柝柞柠柢查柩柬柯柰柱柳柴柷柽柿栀栅标栈栉栊栋栌栎栏栐树栒栓栖栗栝栟校栩" + "株栲栳栴样核根栻格栽栾桀桁桂桃桄桅框案桉桊桌桎桐桑桓桔桕桠桡桢档桤桥桦桧桨桩桫桯" + "桲桴桶桷桹梁梃梅梆梌梏梓梗梠梢梣梦梧梨梭梯械梳梴梵梼梽梾梿检棁棂棉棋棍棐棒棓棕棘" + "棚棠棣棤棨棪棫棬森棰棱棵棹棺棻棼棽椀椁椅椆椋植椎椐椑椒椓椟椠椤椪椭椰椴椸椹椽椿楂" + "楒楔楗楙楚楝楞楠楣楦楩楪楫楮楯楷楸楹楼概榃榄榅榆榇榈榉榍榑榔榕榖榛榜榧榨榫榭榰榱" + "榴榷榻槁槃槊槌槎槐槔槚槛槜槟槠槭槱槲槽槿樊樗樘樟模樨横樯樱樵樽樾橄橇橐橑橘橙橛橞" + "橡橥橦橱橹橼檀檄檎檐檑檗檞檠檩檫檬櫆欂欠次欢欣欤欧欲欸欹欺欻款歃歅歆歇歉歌歙止正" + "此步武歧歪歹死歼殁殂殃殄殆殇殉殊残殍殒殓殖殚殛殡殣殪殳殴段殷殿毁毂毅毋毌母每毐毒" + "毓比毕毖毗毙毛毡毪毫毯毳毵毹毽氅氆氇氍氏氐民氓气氕氖氘氙氚氛氟氡氢氤氦氧氨氩氪氮" + "氯氰氲水永氾氿汀汁求汆汇汈汉汊汋汐汔汕汗汛汜汝汞江池污汤汧汨汩汪汫汭汰汲汴汶汹汽" + "汾沁沂沃沄沅沆沇沈沉沌沏沐沓沔沘沙沚沛沟没沣沤沥沦沧沨沩沪沫沭沮沱河沸油沺治沼沽" + "沾沿泂泃泄泅泇泉泊泌泐泓泔法泖泗泙泚泛泜泞泠泡波泣泥注泪泫泮泯泰泱泳泵泷泸泺泻泼" + "泽泾洁洄洇洈洋洌洎洑洒洓洗洘洙洚洛洞洢洣津洧洨洪洫洭洮洱洲洳洴洵洸洹洺活洼洽派洿" + "流浃浅浆浇浈浉浊测浍济浏浐浑浒浓浔浕浙浚浛浜浞浟浠浡浣浥浦浩浪浬浭浮浯浰浲浴海浸" + "浼涂涄涅消涉涌涍涎涐涑涓涔涕涘涛涝涞涟涠涡涢涣涤润涧涨涩涪涫涮涯液涴涵涸涿淀淄淅" + "淆淇淋淌淏淑淖淘淙淜淝淞淟淠淡淤淦淫淬淮淯深淳淴混淹添淼清渊渌渍渎渐渑渔渗渚渝渟" + "渠渡渣渤渥温渫渭港渰渲渴游渺渼湃湄湉湍湎湑湓湔湖湘湛湜湝湟湣湫湮湲湴湾湿溁溃溅溆" + "溇溉溍溏源溘溚溜溞溟溠溢溥溦溧溪溯溱溲溴溵溶溷溹溺溻溽滁滂滃滆滇滉滋滍滏滑滓滔滕" + "滗滘滚滞滟滠满滢滤滥滦滧滨滩滪滫滴滹漂漆漈漉漋漏漓演漕漖漠漤漦漩漪漫漭漯漱漳漴漶" + "漷漹漻漼漾潆潇潋潍潏潖潘潜潞潟潢潦潩潭潮潲潴潵潸潺潼潽潾澂澄澈澉澌澍澎澛澜澡澥澧" + "澪澭澳澴澶澹澼澽激濂濉濋濑濒濞濠濡濩濮濯瀌瀍瀑瀔瀚瀛瀣瀱瀵瀹瀼灈灌灏灞火灭灯灰灵" + "灶灸灼灾灿炀炅炆炉炊炌炎炒炔炕炖炘炙炜炝炟炣炫炬炭炮炯炱炳炷炸点炻炼炽烀烁烂烃烈" + "烊烔烘烙烛烜烝烟烠烤烦烧烨烩烫烬热烯烶烷烹烺烻烽焆焉焊焌焐焓焕焖焗焘焙焚焜焞焦焯" + "焰焱然煁煃煅煊煋煌煎煓煜煞煟煤煦照煨煮煲煳煴煸煺煽熄熇熊熏熔熘熙熛熜熟熠熥熨熬熵" + "熹熻燃燊燋燎燏燔燕燚燠燥燧燮燹爆爇爔爚爝爟爨爪爬爰爱爵父爷爸爹爻爽爿牁牂片版牌牍" + "牒牖牙牚牛牝牟牡牢牤牥牦牧物牮牯牲牵特牺牻牾牿犀犁犄犇犊犋犍犏犒犟犨犬犯犰犴状犷" + "犸犹狁狂狃狄狈狉狍狎狐狒狗狙狝狞狠狡狨狩独狭狮狯狰狱狲狳狴狷狸狺狻狼猁猃猄猇猊猎" + "猕猖猗猛猜猝猞猡猢猥猩猪猫猬献猯猰猱猴猷猹猺猾猿獍獐獒獗獠獬獭獯獴獾玃玄率玉王玎" + "玑玒玓玕玖玘玙玚玛玞玟玠玡玢玤玥玦玩玫玭玮环现玱玲玳玶玷玹玺玻玼玿珀珂珅珇珈珉珊" + "珋珌珍珏珐珑珒珕珖珙珛珝珞珠珢珣珥珦珧珩珪珫班珰珲珵珷珸珹珺珽琀球琄琅理琇琈琉琊" + "琎琏琐琔琚琛琟琡琢琤琥琦琨琪琫琬琭琮琯琰琲琳琴琵琶琼瑀瑁瑂瑃瑄瑅瑆瑑瑓瑔瑕瑖瑗瑙" + "瑚瑛瑜瑝瑞瑟瑢瑧瑨瑬瑭瑰瑱瑳瑶瑷瑾璀璁璃璆璇璈璋璎璐璒璘璜璞璟璠璥璧璨璩璪璬璮璱" + "璲璺瓀瓒瓖瓘瓜瓞瓠瓢瓣瓤瓦瓮瓯瓴瓶瓷瓻瓿甄甍甏甑甓甗甘甚甜生甡甥甦用甩甪甫甬甭甯" + "田由甲申电男甸町画甾畀畅畈畋界畎畏畔畖留畚畛畜畤略畦番畬畯畲畴畸畹畿疁疃疆疍疏疐" + "疑疔疖疗疙疚疝疟疠疡疢疣疤疥疫疬疭疮疯疰疱疲疳疴疵疸疹疼疽疾痂痃痄病症痈痉痊痍痒" + "痓痔痕痘痛痞痢痣痤痦痧痨痪痫痰痱痴痹痼痿瘀瘁瘃瘅瘆瘊瘌瘐瘕瘗瘘瘙瘛瘟瘠瘢瘤瘥瘦瘩" + "瘪瘫瘭瘰瘳瘴瘵瘸瘼瘾瘿癀癃癌癍癔癖癗癜癞癣癫癯癸登白百癿皂的皆皇皈皋皎皑皓皕皖皙" + "皛皞皤皦皭皮皱皲皴皿盂盅盆盈盉益盍盎盏盐监盒盔盖盗盘盛盟盥盦目盯盱盲直盷相盹盼盾" + "省眄眇眈眉眊看眍眙眚真眠眢眦眨眩眬眭眯眵眶眷眸眺眼着睁睃睄睇睎睐睑睚睛睡睢督睥睦" + "睨睫睬睹睽睾睿瞀瞄瞅瞋瞌瞍瞎瞑瞒瞟瞠瞢瞥瞧瞩瞪瞫瞬瞭瞰瞳瞵瞻瞽瞿矍矗矛矜矞矢矣知" + "矧矩矫矬短矮矰石矶矸矻矼矾矿砀码砂砄砆砉砌砍砑砒研砖砗砘砚砜砝砟砠砣砥砧砫砬砭砮" + "砰破砵砷砸砹砺砻砼砾础硁硅硇硊硌硍硎硐硒硔硕硖硗硙硚硝硪硫硬硭确硼硿碃碇碈碉碌碍" + "碎碏碑碓碗碘碚碛碜碟碡碣碥碧碨碰碱碲碳碴碶碹碾磁磅磉磊磋磏磐磔磕磙磜磡磨磬磲磴磷" + "磹磻礁礅礌礓礞礴礵示礼社祀祁祃祆祇祈祉祊祋祎祏祐祓祕祖祗祚祛祜祝神祟祠祢祥祧票祭" + "祯祲祷祸祺祼祾禀禁禄禅禊禋福禒禔禘禚禛禤禧禳禹禺离禽禾秀私秃秆秉秋种科秒秕秘租秣" + "秤秦秧秩秫秬秭积称秸移秽秾稀稂稃稆程稌稍税稑稔稗稙稚稞稠稣稳稷稹稻稼稽稿穄穆穑穗" + "穙穜穟穰穴究穷穸穹空穿窀突窃窄窅窈窊窍窎窑窒窕窖窗窘窜窝窟窠窣窥窦窨窬窭窳窸窿立" + "竑竖竘站竞竟章竣童竦竫竭端竹竺竽竿笃笄笆笈笊笋笏笑笔笕笙笛笞笠笤笥符笨笪笫第笮笯" + "笱笳笸笺笼笾筀筅筇等筋筌筏筐筑筒答策筘筚筛筜筝筠筢筤筥筦筮筱筲筵筶筷筹筻筼签简箅" + "箍箐箓箔箕箖算箜管箢箦箧箨箩箪箫箬箭箱箴箸篁篆篇篌篑篓篙篚篝篡篥篦篪篮篯篱篷篼篾" + "簃簇簉簋簌簏簕簖簝簟簠簧簪簰簸簿籀籁籍籥米籴类籼籽粉粑粒粕粗粘粜粝粞粟粢粤粥粪粮" + "粱粲粳粹粼粽精粿糁糅糇糈糊糌糍糒糕糖糗糙糜糟糠糨糯糵系紊素索紧紫累絜絮絷綦綮縠縢" + "縻繁繄繇纂纛纠纡红纣纤纥约级纨纩纪纫纬纭纮纯纰纱纲纳纴纵纶纷纸纹纺纻纼纽纾线绀绁" + "绂练组绅细织终绉绊绋绌绍绎经绐绑绒结绔绕绖绗绘给绚绛络绝绞统绠绡绢绣绤绥绦继绨绩" + "绪绫续绮绯绰绱绲绳维绵绶绷绸绹绺绻综绽绾绿缀缁缂缃缄缅缆缇缈缉缊缌缎缐缑缒缓缔缕" + "编缗缘缙缚缛缜缝缞缟缠缡缢缣缤缥缦缧缨缩缪缫缬缭缮缯缰缱缲缳缴缵缶缸缺罂罄罅罍罐" + "网罔罕罗罘罚罟罡罢罨罩罪置罱署罴罶罹罽罾羁羊羌美羑羓羔羕羖羚羝羞羟羡群羧羯羰羱羲" + "羸羹羼羽羿翀翁翂翃翅翈翊翌翎翔翕翘翙翚翛翟翠翡翥翦翩翮翯翰翱翳翷翻翼翾耀老考耄者" + "耆耇耋而耍耏耐耑耒耔耕耖耗耘耙耜耠耢耤耥耦耧耨耩耪耰耱耳耵耶耷耸耻耽耿聂聃聆聊聋" + "职聍聒联聘聚聩聪聱聿肃肄肆肇肉肋肌肓肖肘肚肛肝肟肠股肢肤肥肩肪肫肭肮肯肱育肴肷肸" + "肺肼肽肾肿胀胁胂胃胄胆胈背胍胎胖胗胙胚胛胜胝胞胠胡胣胤胥胧胨胩胪胫胬胭胯胰胱胲胳" + "胴胶胸胺胼能脂脆脉脊脍脎脏脐脑脒脓脔脖脘脚脞脟脩脬脯脱脲脶脸脾脿腆腈腊腋腌腐腑腒" + "腓腔腕腘腙腚腠腥腧腨腩腭腮腯腰腱腴腹腺腻腼腽腾腿膀膂膈膊膏膑膘膙膛膜膝膦膨膳膺膻" + "臀臂臃臆臊臌臑臜臣臧自臬臭至致臻臼臾舀舁舂舄舅舆舌舍舐舒舔舛舜舞舟舠舢舣舥航舫般" + "舭舯舰舱舲舳舴舵舶舷舸船舻舾艄艅艇艉艋艎艏艘艚艟艨艮良艰色艳艴艺艽艾艿节芃芄芈芊" + "芋芍芎芏芑芒芗芘芙芜芝芟芠芡芣芤芥芦芨芩芪芫芬芭芮芯芰花芳芴芷芸芹芼芽芾苁苄苇苈" + "苉苊苋苌苍苎苏苑苒苓苔苕苗苘苛苜苞苟苠苡苣苤若苦苧苫苯英苴苷苹苻苾茀茁茂范茄茅茆" + "茈茉茋茌茎茏茑茓茔茕茗茚茛茜茝茧茨茫茬茭茯茱茳茴茵茶茸茹茺茼茽荀荁荃荄荆荇草荏荐" + "荑荒荓荔荖荙荚荛荜荞荟荠荡荣荤荥荦荧荨荩荪荫荬荭荮药荷荸荻荼荽莅莆莉莎莒莓莘莙莛" + "莜莝莞莠莨莩莪莫莰莱莲莳莴莶获莸莹莺莼莽莿菀菁菂菅菇菉菊菌菍菏菔菖菘菜菝菟菠菡菥" + "菩菪菰菱菲菹菼菽萁萃萄萆萋萌萍萎萏萑萘萚萜萝萣萤营萦萧萨萩萱萳萸萹萼落葆葎葑葖著" + "葙葚葛葜葡董葩葫葬葭葰葱葳葴葵葶葸葺蒂蒄蒇蒈蒉蒋蒌蒎蒐蒗蒙蒜蒟蒡蒨蒯蒱蒲蒴蒸蒹蒺" + "蒻蒽蒿蓁蓂蓄蓇蓉蓊蓍蓏蓐蓑蓓蓖蓝蓟蓠蓢蓣蓥蓦蓬蓰蓼蓿蔀蔃蔈蔊蔌蔑蔓蔗蔚蔟蔡蔫蔬蔷" + "蔸蔹蔺蔻蔼蔽蕃蕈蕉蕊蕖蕗蕙蕞蕤蕨蕰蕲蕴蕹蕺蕻蕾薁薄薅薇薏薛薜薢薤薨薪薮薯薰薳薷薸" + "薹薿藁藉藏藐藓藕藜藟藠藤藦藨藩藻藿蘅蘑蘖蘘蘧蘩蘸蘼虎虏虐虑虒虓虔虚虞虢虤虫虬虮虱" + "虷虸虹虺虻虼虽虾虿蚀蚁蚂蚄蚆蚊蚋蚌蚍蚓蚕蚜蚝蚣蚤蚧蚨蚩蚪蚬蚯蚰蚱蚲蚴蚶蚺蛀蛃蛄蛆" + "蛇蛉蛊蛋蛎蛏蛐蛑蛔蛘蛙蛛蛞蛟蛤蛩蛭蛮蛰蛱蛲蛳蛴蛸蛹蛾蜀蜂蜃蜇蜈蜉蜊蜍蜎蜐蜒蜓蜕蜗" + "蜘蜚蜜蜞蜡蜢蜣蜥蜩蜮蜱蜴蜷蜻蜾蜿蝇蝈蝉蝌蝎蝓蝗蝘蝙蝠蝣蝤蝥蝮蝰蝲蝴蝶蝻蝼蝽蝾螂螃" + "螅螈螋融螗螟螠螣螨螫螬螭螯螱螳螵螺螽蟀蟆蟊蟋蟏蟑蟒蟛蟠蟥蟪蟫蟮蟹蟾蠃蠊蠋蠓蠕蠖蠡" + "蠢蠲蠹蠼血衃衄衅行衍衎衒衔街衙衠衡衢衣补表衩衫衬衮衰衲衷衽衾衿袁袂袄袅袆袈袋袍袒" + "袖袗袜袢袤袪被袭袯袱袷袼裁裂装裆裈裉裎裒裔裕裘裙裛裟裢裣裤裥裨裰裱裳裴裸裹裼裾褂" + "褊褐褒褓褕褙褚褛褟褡褥褪褫褯褰褴褶襁襄襕襚襜襞襟襦襫襻西要覃覆见观觃规觅视觇览觉" + "觊觋觌觎觏觐觑角觖觚觜觞觟解觥触觫觭觯觱觳觿言訄訇訚訾詈詟詹誉誊誓謇警譬计订讣认" + "讥讦讧讨让讪讫训议讯记讱讲讳讴讵讶讷许讹论讻讼讽设访诀证诂诃评诅识诇诈诉诊诋诌词" + "诎诏诐译诒诓诔试诖诗诘诙诚诛诜话诞诟诠诡询诣诤该详诧诨诩诫诬语诮误诰诱诲诳说诵请" + "诸诹诺读诼诽课诿谀谁谂调谄谅谆谇谈谊谋谌谍谎谏谐谑谒谓谔谕谖谗谙谚谛谜谝谞谟谠谡" + "谢谣谤谥谦谧谨谩谪谫谬谭谮谯谰谱谲谳谴谵谶谷谼谿豁豆豇豉豌豕豚象豢豨豪豫豮豳豸豹" + "豺貂貅貆貉貊貌貔貘贝贞负贡财责贤败账货质贩贪贫贬购贮贯贰贱贲贳贴贵贶贷贸费贺贻贼" + "贽贾贿赀赁赂赃资赅赆赇赈赉赊赋赌赍赎赏赐赑赒赓赔赕赖赗赘赙赚赛赜赝赞赟赠赡赢赣赤" + "赦赧赪赫赭走赳赴赵赶起趁趄超越趋趑趔趟趣趯趱足趴趵趸趺趼趾趿跂跃跄跆跋跌跎跏跐跑" + "跖跗跚跛距跞跟跣跤跨跪跬路跱跳践跶跷跸跹跺跻跽踅踉踊踌踏踒踔踝踞踟踢踣踦踩踪踬踮" + "踯踱踵踶踹踺踽蹀蹁蹂蹄蹅蹇蹈蹉蹊蹋蹐蹑蹒蹙蹚蹜蹢蹦蹩蹬蹭蹯蹰蹲蹴蹶蹼蹽蹾蹿躁躅躇" + "躏躐躔躜躞身躬躯躲躺车轧轨轩轪轫转轭轮软轰轱轲轳轴轵轶轷轸轹轺轻轼载轾轿辀辁辂较" + "辄辅辆辇辈辉辊辋辌辍辎辏辐辑辒输辔辕辖辗辘辙辚辛辜辞辟辣辨辩辫辰辱边辽达辿迁迂迄" + "迅过迈迎运近迓返迕还这进远违连迟迢迤迥迦迨迩迪迫迭迮述迳迷迸迹迺追退送适逃逄逅逆" + "选逊逋逍透逐逑递途逖逗通逛逝逞速造逡逢逦逭逮逯逴逵逶逸逻逼逾遁遂遄遆遇遍遏遐遑遒" + "道遗遘遛遢遣遥遨遭遮遴遵遹遽避邀邂邃邈邋邑邓邕邗邘邙邛邝邠邡邢那邦邨邪邬邮邯邰邱" + "邲邳邴邵邶邸邹邺邻邽邾邿郁郃郄郅郇郈郊郎郏郐郑郓郗郚郛郜郝郡郢郤郦郧部郪郫郭郯郴" + "郸都郾郿鄀鄂鄃鄄鄅鄌鄑鄗鄘鄙鄚鄜鄞鄠鄢鄣鄫鄯鄱鄹酂酃酅酆酉酊酋酌配酎酏酐酒酗酚酝" + "酞酡酢酣酤酥酦酩酪酬酮酯酰酱酲酴酵酶酷酸酹酺酽酾酿醅醇醉醋醌醍醐醑醒醚醛醢醨醪醭" + "醮醯醴醵醺醾采釉释里重野量釐金釜鉴銎銮鋆鋈錾鍪鎏鏊鏖鐾鑫钆钇针钉钊钋钌钍钎钏钐钒" + "钓钔钕钖钗钘钙钚钛钜钝钞钟钠钡钢钣钤钥钦钧钨钩钪钫钬钭钮钯钰钱钲钳钴钵钷钹钺钻钼" + "钽钾钿铀铁铂铃铄铅铆铈铉铊铋铌铍铎铏铐铑铒铕铖铗铘铙铚铛铜铝铞铟铠铡铢铣铤铥铧铨" + "铩铪铫铬铭铮铯铰铱铲铳铴铵银铷铸铹铺铻铼铽链铿销锁锂锃锄锅锆锇锈锉锊锋锌锍锎锏锐" + "锑锒锓锔锕锖锗锘错锚锛锜锝锞锟锡锢锣锤锥锦锧锨锩锪锫锬锭键锯锰锱锲锳锴锵锶锷锸锹" + "锺锻锼锽锾锿镀镁镂镃镄镅镆镇镈镉镊镋镌镍镎镏镐镑镒镓镔镕镖镗镘镚镛镜镝镞镠镡镢镣" + "镤镥镦镧镨镩镪镫镬镭镮镯镰镱镲镳镴镵镶长门闩闪闫闭问闯闰闱闲闳间闵闶闷闸闹闺闻闼" + "闽闾闿阀阁阂阃阄阅阆阇阈阉阊阋阌阍阎阏阐阑阒阔阕阖阗阘阙阚阜队阡阪阮阱防阳阴阵阶" + "阻阼阽阿陀陂附际陆陇陈陉陋陌降陎限陑陔陕陛陞陟陡院除陧陨险陪陬陲陴陵陶陷隃隅隆隈" + "隋隍随隐隔隗隘隙障隧隩隰隳隶隹隺隼隽难雀雁雄雅集雇雉雊雌雍雎雏雒雕雠雨雩雪雯雱雳" + "零雷雹雾需霁霄霅霆震霈霉霍霎霏霓霖霜霞霨霪霭霰露霸霹霾青靓靖静靛非靠靡面靥革靬靰" + "靳靴靶靸靺靼靽靿鞁鞅鞋鞍鞑鞒鞔鞘鞠鞡鞣鞧鞨鞫鞬鞭鞮鞯鞲鞳鞴韂韦韧韨韩韪韫韬韭音韵" + "韶页顶顷顸项顺须顼顽顾顿颀颁颂颃预颅领颇颈颉颊颋颌颍颎颏颐频颓颔颖颗题颙颚颛颜额" + "颞颟颠颡颢颤颥颦颧风飏飐飑飒飓飔飕飗飘飙飞食飧飨餍餐餮饔饕饥饧饨饩饪饫饬饭饮饯饰" + "饱饲饳饴饵饶饷饸饹饺饻饼饽饿馁馃馄馅馆馇馈馉馊馋馌馍馏馐馑馒馓馔馕首馗馘香馝馞馥" + "馧馨马驭驮驯驰驱驲驳驴驵驶驷驸驹驺驻驼驽驾驿骀骁骂骃骄骅骆骇骈骉骊骋验骍骎骏骐骑" + "骒骓骕骖骗骘骙骚骛骜骝骞骟骠骡骢骣骤骥骦骧骨骰骱骶骷骸骺骼髀髁髂髃髅髋髌髎髑髓高" + "髡髢髦髫髭髯髹髻髽鬃鬈鬏鬒鬓鬘鬟鬣鬯鬲鬶鬷鬻鬼魁魂魃魄魅魆魇魈魉魋魍魏魑魔鱼鱽鱾" + "鱿鲀鲁鲂鲃鲅鲆鲇鲈鲉鲊鲋鲌鲍鲎鲏鲐鲑鲒鲔鲕鲖鲗鲘鲙鲚鲛鲜鲝鲞鲟鲠鲡鲢鲣鲤鲥鲦鲧鲨" + "鲩鲪鲫鲬鲭鲮鲯鲰鲱鲲鲳鲴鲵鲷鲸鲹鲺鲻鲼鲽鲾鲿鳀鳁鳂鳃鳄鳅鳇鳈鳉鳊鳌鳍鳎鳏鳐鳑鳒鳓" + "鳔鳕鳖鳗鳘鳙鳚鳛鳜鳝鳞鳟鳠鳡鳢鳣鳤鸟鸠鸡鸢鸣鸤鸥鸦鸧鸨鸩鸪鸫鸬鸭鸮鸯鸰鸱鸲鸳鸵鸶" + "鸷鸸鸹鸺鸻鸼鸽鸾鸿鹀鹁鹂鹃鹄鹅鹆鹇鹈鹉鹊鹋鹌鹍鹎鹏鹐鹑鹒鹔鹕鹖鹗鹘鹙鹚鹛鹜鹝鹞鹟" + "鹠鹡鹢鹣鹤鹦鹧鹨鹩鹪鹫鹬鹭鹮鹯鹰鹱鹲鹳鹴鹾鹿麀麂麇麈麋麑麒麓麖麝麟麦麸麹麻麽麾黄" + "黇黉黍黎黏黑黔默黛黜黝黟黠黡黢黥黧黩黪黯黹黻黼黾鼋鼍鼎鼐鼒鼓鼗鼙鼠鼢鼩鼫鼬鼯鼱鼷" + "鼹鼻鼽鼾齁齇齉齐齑齿龀龁龂龃龄龅龆龇龈龉龊龋龌龙龚龛龟龠龢鿍鿎鿏㑇㑊㕮㘎㙍㙘㙦㛃" + "㛚㛹㟃㠇㠓㤘㥄㧐㧑㧟㫰㬊㬎㬚㭎㭕㮾㰀㳇㳘㳚㴔㵐㶲㸆㸌㺄㻬㽏㿠䁖䂮䃅䃎䅟䌹䎃䎖䏝䏡" + "䏲䐃䓖䓛䓨䓫䓬䗖䗛䗪䗴䜣䝙䢺䢼䣘䥽䦃䲟䲠䲢䴓䴔䴕䴖䴗䴘䴙䶮𠅤𠙶𠳐𡎚𡐓𣗋𣲗𣲘𣸣𤧛𤩽" + "𤫉𥔲𥕢𥖨𥻗𦈡𦒍𦙶𦝼𦭜𦰡𧿹𨐈𨙸𨚕𨟠𨭉𨱇𨱏𨱑𨱔𨺙𩽾𩾃𩾌𪟝𪣻𪤗𪨰𪨶𪩘𪾢𫄧𫄨𫄷𫄸𫇭𫌀𫍣𫍯" + "𫍲𫍽𫐄𫐐𫐓𫑡𫓧𫓯𫓶𫓹𫔍𫔎𫔶𫖮𫖯𫖳𫗧𫗴𫘜𫘝𫘦𫘧𫘨𫘪𫘬𫚕𫚖𫚭𫛭𫞩𫟅𫟦𫟹𫟼𫠆𫠊𫠜𫢸𫫇𫭟" + "𫭢𫭼𫮃𫰛𫵷𫶇𫷷𫸩𬀩𬀪𬂩𬃊𬇕𬇙𬇹𬉼𬊈𬊤𬌗𬍛𬍡𬍤𬒈𬒔𬒗𬕂𬘓𬘘𬘡𬘩𬘫𬘬𬘭𬘯𬙂𬙊𬙋𬜬𬜯𬞟" + "𬟁𬟽𬣙𬣞𬣡𬣳𬤇𬤊𬤝𬨂𬨎𬩽𬪩𬬩𬬭𬬮𬬱𬬸𬬹𬬻𬬿𬭁𬭊𬭎𬭚𬭛𬭤𬭩𬭬𬭯𬭳𬭶𬭸𬭼𬮱𬮿𬯀𬯎𬱖𬱟" + "𬳵𬳶𬳽𬳿𬴂𬴃𬴊𬶋𬶍𬶏𬶐𬶟𬶠𬶨𬶭𬶮𬷕𬸘𬸚𬸣𬸦𬸪𬹼𬺈𬺓" +) +CN_CHARS_EXT = "吶诶屌囧飚屄" + +CN_CHARS = CN_CHARS_COMMON + CN_CHARS_EXT +IN_CH_CHARS = {c: True for c in CN_CHARS} + +EN_CHARS = string.ascii_letters + string.digits +IN_EN_CHARS = {c: True for c in EN_CHARS} + +VALID_CHARS = CN_CHARS + EN_CHARS + " " +IN_VALID_CHARS = {c: True for c in VALID_CHARS} + + +# ================================================================================ # +# basic class +# ================================================================================ # +class ChineseChar(object): + """ + 中文字符 + 每个字符对应简体和繁体, + e.g. 简体 = '负', 繁体 = '負' + 转换时可转换为简体或繁体 + """ + + def __init__(self, simplified, traditional): + self.simplified = simplified + self.traditional = traditional + # self.__repr__ = self.__str__ + + def __str__(self): + return self.simplified or self.traditional or None + + def __repr__(self): + return self.__str__() + + +class ChineseNumberUnit(ChineseChar): + """ + 中文数字/数位字符 + 每个字符除繁简体外还有一个额外的大写字符 + e.g. '陆' 和 '陸' + """ + + def __init__(self, power, simplified, traditional, big_s, big_t): + super(ChineseNumberUnit, self).__init__(simplified, traditional) + self.power = power + self.big_s = big_s + self.big_t = big_t + + def __str__(self): + return "10^{}".format(self.power) + + @classmethod + def create(cls, index, value, numbering_type=NUMBERING_TYPES[1], small_unit=False): + + if small_unit: + return ChineseNumberUnit( + power=index + 1, + simplified=value[0], + traditional=value[1], + big_s=value[1], + big_t=value[1], + ) + elif numbering_type == NUMBERING_TYPES[0]: + return ChineseNumberUnit( + power=index + 8, + simplified=value[0], + traditional=value[1], + big_s=value[0], + big_t=value[1], + ) + elif numbering_type == NUMBERING_TYPES[1]: + return ChineseNumberUnit( + power=(index + 2) * 4, + simplified=value[0], + traditional=value[1], + big_s=value[0], + big_t=value[1], + ) + elif numbering_type == NUMBERING_TYPES[2]: + return ChineseNumberUnit( + power=pow(2, index + 3), + simplified=value[0], + traditional=value[1], + big_s=value[0], + big_t=value[1], + ) + else: + raise ValueError( + "Counting type should be in {0} ({1} provided).".format( + NUMBERING_TYPES, numbering_type + ) + ) + + +class ChineseNumberDigit(ChineseChar): + """ + 中文数字字符 + """ + + def __init__(self, value, simplified, traditional, big_s, big_t, alt_s=None, alt_t=None): + super(ChineseNumberDigit, self).__init__(simplified, traditional) + self.value = value + self.big_s = big_s + self.big_t = big_t + self.alt_s = alt_s + self.alt_t = alt_t + + def __str__(self): + return str(self.value) + + @classmethod + def create(cls, i, v): + return ChineseNumberDigit(i, v[0], v[1], v[2], v[3]) + + +class ChineseMath(ChineseChar): + """ + 中文数位字符 + """ + + def __init__(self, simplified, traditional, symbol, expression=None): + super(ChineseMath, self).__init__(simplified, traditional) + self.symbol = symbol + self.expression = expression + self.big_s = simplified + self.big_t = traditional + + +CC, CNU, CND, CM = ChineseChar, ChineseNumberUnit, ChineseNumberDigit, ChineseMath + + +class NumberSystem(object): + """ + 中文数字系统 + """ + + pass + + +class MathSymbol(object): + """ + 用于中文数字系统的数学符号 (繁/简体), e.g. + positive = ['正', '正'] + negative = ['负', '負'] + point = ['点', '點'] + """ + + def __init__(self, positive, negative, point): + self.positive = positive + self.negative = negative + self.point = point + + def __iter__(self): + for v in self.__dict__.values(): + yield v + + +# class OtherSymbol(object): +# """ +# 其他符号 +# """ +# +# def __init__(self, sil): +# self.sil = sil +# +# def __iter__(self): +# for v in self.__dict__.values(): +# yield v + + +# ================================================================================ # +# basic utils +# ================================================================================ # +def create_system(numbering_type=NUMBERING_TYPES[1]): + """ + 根据数字系统类型返回创建相应的数字系统,默认为 mid + NUMBERING_TYPES = ['low', 'mid', 'high']: 中文数字系统类型 + low: '兆' = '亿' * '十' = $10^{9}$, '京' = '兆' * '十', etc. + mid: '兆' = '亿' * '万' = $10^{12}$, '京' = '兆' * '万', etc. + high: '兆' = '亿' * '亿' = $10^{16}$, '京' = '兆' * '兆', etc. + 返回对应的数字系统 + """ + + # chinese number units of '亿' and larger + all_larger_units = zip( + LARGER_CHINESE_NUMERING_UNITS_SIMPLIFIED, LARGER_CHINESE_NUMERING_UNITS_TRADITIONAL + ) + larger_units = [CNU.create(i, v, numbering_type, False) for i, v in enumerate(all_larger_units)] + # chinese number units of '十, 百, 千, 万' + all_smaller_units = zip( + SMALLER_CHINESE_NUMERING_UNITS_SIMPLIFIED, SMALLER_CHINESE_NUMERING_UNITS_TRADITIONAL + ) + smaller_units = [CNU.create(i, v, small_unit=True) for i, v in enumerate(all_smaller_units)] + # digis + chinese_digis = zip( + CHINESE_DIGIS, CHINESE_DIGIS, BIG_CHINESE_DIGIS_SIMPLIFIED, BIG_CHINESE_DIGIS_TRADITIONAL + ) + digits = [CND.create(i, v) for i, v in enumerate(chinese_digis)] + digits[0].alt_s, digits[0].alt_t = ZERO_ALT, ZERO_ALT + digits[1].alt_s, digits[1].alt_t = ONE_ALT, ONE_ALT + digits[2].alt_s, digits[2].alt_t = TWO_ALTS[0], TWO_ALTS[1] + + # symbols + positive_cn = CM(POSITIVE[0], POSITIVE[1], "+", lambda x: x) + negative_cn = CM(NEGATIVE[0], NEGATIVE[1], "-", lambda x: -x) + point_cn = CM(POINT[0], POINT[1], ".", lambda x, y: float(str(x) + "." + str(y))) + # sil_cn = CM(SIL[0], SIL[1], '-', lambda x, y: float(str(x) + '-' + str(y))) + system = NumberSystem() + system.units = smaller_units + larger_units + system.digits = digits + system.math = MathSymbol(positive_cn, negative_cn, point_cn) + # system.symbols = OtherSymbol(sil_cn) + return system + + +def chn2num(chinese_string, numbering_type=NUMBERING_TYPES[1]): + + def get_symbol(char, system): + for u in system.units: + if char in [u.traditional, u.simplified, u.big_s, u.big_t]: + return u + for d in system.digits: + if char in [d.traditional, d.simplified, d.big_s, d.big_t, d.alt_s, d.alt_t]: + return d + for m in system.math: + if char in [m.traditional, m.simplified]: + return m + + def string2symbols(chinese_string, system): + int_string, dec_string = chinese_string, "" + for p in [system.math.point.simplified, system.math.point.traditional]: + if p in chinese_string: + int_string, dec_string = chinese_string.split(p) + break + return [get_symbol(c, system) for c in int_string], [ + get_symbol(c, system) for c in dec_string + ] + + def correct_symbols(integer_symbols, system): + """ + 一百八 to 一百八十 + 一亿一千三百万 to 一亿 一千万 三百万 + """ + + if integer_symbols and isinstance(integer_symbols[0], CNU): + if integer_symbols[0].power == 1: + integer_symbols = [system.digits[1]] + integer_symbols + + if len(integer_symbols) > 1: + if isinstance(integer_symbols[-1], CND) and isinstance(integer_symbols[-2], CNU): + integer_symbols.append(CNU(integer_symbols[-2].power - 1, None, None, None, None)) + + result = [] + unit_count = 0 + for s in integer_symbols: + if isinstance(s, CND): + result.append(s) + unit_count = 0 + elif isinstance(s, CNU): + current_unit = CNU(s.power, None, None, None, None) + unit_count += 1 + + if unit_count == 1: + result.append(current_unit) + elif unit_count > 1: + for i in range(len(result)): + if ( + isinstance(result[-i - 1], CNU) + and result[-i - 1].power < current_unit.power + ): + result[-i - 1] = CNU( + result[-i - 1].power + current_unit.power, None, None, None, None + ) + return result + + def compute_value(integer_symbols): + """ + Compute the value. + When current unit is larger than previous unit, current unit * all previous units will be used as all previous units. + e.g. '两千万' = 2000 * 10000 not 2000 + 10000 + """ + value = [0] + last_power = 0 + for s in integer_symbols: + if isinstance(s, CND): + value[-1] = s.value + elif isinstance(s, CNU): + value[-1] *= pow(10, s.power) + if s.power > last_power: + value[:-1] = list(map(lambda v: v * pow(10, s.power), value[:-1])) + last_power = s.power + value.append(0) + return sum(value) + + system = create_system(numbering_type) + int_part, dec_part = string2symbols(chinese_string, system) + int_part = correct_symbols(int_part, system) + int_str = str(compute_value(int_part)) + dec_str = "".join([str(d.value) for d in dec_part]) + if dec_part: + return "{0}.{1}".format(int_str, dec_str) + else: + return int_str + + +def num2chn( + number_string, + numbering_type=NUMBERING_TYPES[1], + big=False, + traditional=False, + alt_zero=False, + alt_one=False, + alt_two=True, + use_zeros=True, + use_units=True, +): + + def get_value(value_string, use_zeros=True): + + striped_string = value_string.lstrip("0") + + # record nothing if all zeros + if not striped_string: + return [] + + # record one digits + elif len(striped_string) == 1: + if use_zeros and len(value_string) != len(striped_string): + return [system.digits[0], system.digits[int(striped_string)]] + else: + return [system.digits[int(striped_string)]] + + # recursively record multiple digits + else: + result_unit = next(u for u in reversed(system.units) if u.power < len(striped_string)) + result_string = value_string[: -result_unit.power] + return ( + get_value(result_string) + + [result_unit] + + get_value(striped_string[-result_unit.power :]) + ) + + system = create_system(numbering_type) + + int_dec = number_string.split(".") + if len(int_dec) == 1: + int_string = int_dec[0] + dec_string = "" + elif len(int_dec) == 2: + int_string = int_dec[0] + dec_string = int_dec[1] + else: + raise ValueError( + "invalid input num string with more than one dot: {}".format(number_string) + ) + + if use_units and len(int_string) > 1: + result_symbols = get_value(int_string) + else: + result_symbols = [system.digits[int(c)] for c in int_string] + dec_symbols = [system.digits[int(c)] for c in dec_string] + if dec_string: + result_symbols += [system.math.point] + dec_symbols + + if alt_two: + liang = CND( + 2, + system.digits[2].alt_s, + system.digits[2].alt_t, + system.digits[2].big_s, + system.digits[2].big_t, + ) + for i, v in enumerate(result_symbols): + if isinstance(v, CND) and v.value == 2: + next_symbol = result_symbols[i + 1] if i < len(result_symbols) - 1 else None + previous_symbol = result_symbols[i - 1] if i > 0 else None + if isinstance(next_symbol, CNU) and isinstance(previous_symbol, (CNU, type(None))): + if next_symbol.power != 1 and ( + (previous_symbol is None) or (previous_symbol.power != 1) + ): + result_symbols[i] = liang + + # if big is True, '两' will not be used and `alt_two` has no impact on output + if big: + attr_name = "big_" + if traditional: + attr_name += "t" + else: + attr_name += "s" + else: + if traditional: + attr_name = "traditional" + else: + attr_name = "simplified" + + result = "".join([getattr(s, attr_name) for s in result_symbols]) + + # if not use_zeros: + # result = result.strip(getattr(system.digits[0], attr_name)) + + if alt_zero: + result = result.replace(getattr(system.digits[0], attr_name), system.digits[0].alt_s) + + if alt_one: + result = result.replace(getattr(system.digits[1], attr_name), system.digits[1].alt_s) + + for i, p in enumerate(POINT): + if result.startswith(p): + return CHINESE_DIGIS[0] + result + + # ^10, 11, .., 19 + if ( + len(result) >= 2 + and result[1] + in [ + SMALLER_CHINESE_NUMERING_UNITS_SIMPLIFIED[0], + SMALLER_CHINESE_NUMERING_UNITS_TRADITIONAL[0], + ] + and result[0] + in [CHINESE_DIGIS[1], BIG_CHINESE_DIGIS_SIMPLIFIED[1], BIG_CHINESE_DIGIS_TRADITIONAL[1]] + ): + result = result[1:] + + return result + + +# ================================================================================ # +# different types of rewriters +# ================================================================================ # +class Cardinal: + """ + CARDINAL类 + """ + + def __init__(self, cardinal=None, chntext=None): + self.cardinal = cardinal + self.chntext = chntext + + def chntext2cardinal(self): + return chn2num(self.chntext) + + def cardinal2chntext(self): + return num2chn(self.cardinal) + + +class Digit: + """ + DIGIT类 + """ + + def __init__(self, digit=None, chntext=None): + self.digit = digit + self.chntext = chntext + + # def chntext2digit(self): + # return chn2num(self.chntext) + + def digit2chntext(self): + return num2chn(self.digit, alt_two=False, use_units=False) + + +class TelePhone: + """ + TELEPHONE类 + """ + + def __init__(self, telephone=None, raw_chntext=None, chntext=None): + self.telephone = telephone + self.raw_chntext = raw_chntext + self.chntext = chntext + + # def chntext2telephone(self): + # sil_parts = self.raw_chntext.split('') + # self.telephone = '-'.join([ + # str(chn2num(p)) for p in sil_parts + # ]) + # return self.telephone + + def telephone2chntext(self, fixed=False): + + if fixed: + sil_parts = self.telephone.split("-") + self.raw_chntext = "".join( + [num2chn(part, alt_two=False, use_units=False) for part in sil_parts] + ) + self.chntext = self.raw_chntext.replace("", "") + else: + sp_parts = self.telephone.strip("+").split() + self.raw_chntext = "".join( + [num2chn(part, alt_two=False, use_units=False) for part in sp_parts] + ) + self.chntext = self.raw_chntext.replace("", "") + return self.chntext + + +class Fraction: + """ + FRACTION类 + """ + + def __init__(self, fraction=None, chntext=None): + self.fraction = fraction + self.chntext = chntext + + def chntext2fraction(self): + denominator, numerator = self.chntext.split("分之") + return chn2num(numerator) + "/" + chn2num(denominator) + + def fraction2chntext(self): + numerator, denominator = self.fraction.split("/") + return num2chn(denominator) + "分之" + num2chn(numerator) + + +class Date: + """ + DATE类 + """ + + def __init__(self, date=None, chntext=None): + self.date = date + self.chntext = chntext + + # def chntext2date(self): + # chntext = self.chntext + # try: + # year, other = chntext.strip().split('年', maxsplit=1) + # year = Digit(chntext=year).digit2chntext() + '年' + # except ValueError: + # other = chntext + # year = '' + # if other: + # try: + # month, day = other.strip().split('月', maxsplit=1) + # month = Cardinal(chntext=month).chntext2cardinal() + '月' + # except ValueError: + # day = chntext + # month = '' + # if day: + # day = Cardinal(chntext=day[:-1]).chntext2cardinal() + day[-1] + # else: + # month = '' + # day = '' + # date = year + month + day + # self.date = date + # return self.date + + def date2chntext(self): + date = self.date + try: + year, other = date.strip().split("年", 1) + year = Digit(digit=year).digit2chntext() + "年" + except ValueError: + other = date + year = "" + if other: + try: + month, day = other.strip().split("月", 1) + month = Cardinal(cardinal=month).cardinal2chntext() + "月" + except ValueError: + day = date + month = "" + if day: + day = Cardinal(cardinal=day[:-1]).cardinal2chntext() + day[-1] + else: + month = "" + day = "" + chntext = year + month + day + self.chntext = chntext + return self.chntext + + +class Money: + """ + MONEY类 + """ + + def __init__(self, money=None, chntext=None): + self.money = money + self.chntext = chntext + + # def chntext2money(self): + # return self.money + + def money2chntext(self): + money = self.money + pattern = re.compile(r"(\d+(\.\d+)?)") + matchers = pattern.findall(money) + if matchers: + for matcher in matchers: + money = money.replace(matcher[0], Cardinal(cardinal=matcher[0]).cardinal2chntext()) + self.chntext = money + return self.chntext + + +class Percentage: + """ + PERCENTAGE类 + """ + + def __init__(self, percentage=None, chntext=None): + self.percentage = percentage + self.chntext = chntext + + def chntext2percentage(self): + return chn2num(self.chntext.strip().strip("百分之")) + "%" + + def percentage2chntext(self): + return "百分之" + num2chn(self.percentage.strip().strip("%")) + + +def normalize_nsw(raw_text): + text = "^" + raw_text + "$" + + # 规范化日期 + pattern = re.compile(r"\D+((([089]\d|(19|20)\d{2})年)?(\d{1,2}月(\d{1,2}[日号])?)?)") + matchers = pattern.findall(text) + if matchers: + # print('date') + for matcher in matchers: + text = text.replace(matcher[0], Date(date=matcher[0]).date2chntext(), 1) + + # 规范化金钱 + pattern = re.compile( + r"\D+((\d+(\.\d+)?)[多余几]?" + CURRENCY_UNITS + r"(\d" + CURRENCY_UNITS + r"?)?)" + ) + matchers = pattern.findall(text) + if matchers: + # print('money') + for matcher in matchers: + text = text.replace(matcher[0], Money(money=matcher[0]).money2chntext(), 1) + + # 规范化固话/手机号码 + # 手机 + # http://www.jihaoba.com/news/show/13680 + # 移动:139、138、137、136、135、134、159、158、157、150、151、152、188、187、182、183、184、178、198 + # 联通:130、131、132、156、155、186、185、176 + # 电信:133、153、189、180、181、177 + pattern = re.compile(r"\D((\+?86 ?)?1([38]\d|5[0-35-9]|7[678]|9[89])\d{8})\D") + matchers = pattern.findall(text) + if matchers: + # print('telephone') + for matcher in matchers: + text = text.replace(matcher[0], TelePhone(telephone=matcher[0]).telephone2chntext(), 1) + # 固话 + pattern = re.compile(r"\D((0(10|2[1-3]|[3-9]\d{2})-?)?[1-9]\d{6,7})\D") + matchers = pattern.findall(text) + if matchers: + # print('fixed telephone') + for matcher in matchers: + text = text.replace( + matcher[0], TelePhone(telephone=matcher[0]).telephone2chntext(fixed=True), 1 + ) + + # 规范化分数 + pattern = re.compile(r"(\d+/\d+)") + matchers = pattern.findall(text) + if matchers: + # print('fraction') + for matcher in matchers: + text = text.replace(matcher, Fraction(fraction=matcher).fraction2chntext(), 1) + + # 规范化百分数 + text = text.replace("%", "%") + pattern = re.compile(r"(\d+(\.\d+)?%)") + matchers = pattern.findall(text) + if matchers: + # print('percentage') + for matcher in matchers: + text = text.replace( + matcher[0], Percentage(percentage=matcher[0]).percentage2chntext(), 1 + ) + + # 规范化纯数+量词 + pattern = re.compile(r"(\d+(\.\d+)?)[多余几]?" + COM_QUANTIFIERS) + matchers = pattern.findall(text) + if matchers: + # print('cardinal+quantifier') + for matcher in matchers: + text = text.replace(matcher[0], Cardinal(cardinal=matcher[0]).cardinal2chntext(), 1) + + # 规范化数字编号 + pattern = re.compile(r"(\d{4,32})") + matchers = pattern.findall(text) + if matchers: + # print('digit') + for matcher in matchers: + text = text.replace(matcher, Digit(digit=matcher).digit2chntext(), 1) + + # 规范化纯数 + pattern = re.compile(r"(\d+(\.\d+)?)") + matchers = pattern.findall(text) + if matchers: + # print('cardinal') + for matcher in matchers: + text = text.replace(matcher[0], Cardinal(cardinal=matcher[0]).cardinal2chntext(), 1) + + # restore P2P, O2O, B2C, B2B etc + pattern = re.compile(r"(([a-zA-Z]+)二([a-zA-Z]+))") + matchers = pattern.findall(text) + if matchers: + # print('particular') + for matcher in matchers: + text = text.replace(matcher[0], matcher[1] + "2" + matcher[2], 1) + + return text.lstrip("^").rstrip("$") + + +def remove_erhua(text): + """ + 去除儿化音词中的儿: + 他女儿在那边儿 -> 他女儿在那边 + """ + + new_str = "" + while re.search("儿", text): + a = re.search("儿", text).span() + remove_er_flag = 0 + + if ER_WHITELIST_PATTERN.search(text): + b = ER_WHITELIST_PATTERN.search(text).span() + if b[0] <= a[0]: + remove_er_flag = 1 + + if remove_er_flag == 0: + new_str = new_str + text[0 : a[0]] + text = text[a[1] :] + else: + new_str = new_str + text[0 : b[1]] + text = text[b[1] :] + + text = new_str + text + return text + + +def remove_space(text): + tokens = text.split() + new = [] + for k, t in enumerate(tokens): + if k != 0: + if IN_EN_CHARS.get(tokens[k - 1][-1]) and IN_EN_CHARS.get(t[0]): + new.append(" ") + new.append(t) + return "".join(new) + + +class TextNorm: + def __init__( + self, + to_banjiao: bool = False, + to_upper: bool = False, + to_lower: bool = False, + remove_fillers: bool = False, + remove_erhua: bool = False, + check_chars: bool = False, + remove_space: bool = False, + ): + self.to_banjiao = to_banjiao + self.to_upper = to_upper + self.to_lower = to_lower + self.remove_fillers = remove_fillers + self.remove_erhua = remove_erhua + self.check_chars = check_chars + self.remove_space = remove_space + + def __call__(self, text): + if self.to_banjiao: + text = text.translate(QJ2BJ_TRANSFORM) + + if self.to_upper: + text = text.upper() + + if self.to_lower: + text = text.lower() + + if self.remove_fillers: + for c in FILLER_CHARS: + text = text.replace(c, "") + + if self.remove_erhua: + text = remove_erhua(text) + + text = normalize_nsw(text) + + text = text.translate(PUNCS_TRANSFORM) + + if self.check_chars: + for c in text: + if not IN_VALID_CHARS.get(c): + print(f"WARNING: illegal char {c} in: {text}", file=sys.stderr) + return "" + + if self.remove_space: + text = remove_space(text) + + return text + + +if __name__ == "__main__": + p = argparse.ArgumentParser() + + # normalizer options + p.add_argument("--to_banjiao", action="store_true", help="convert quanjiao chars to banjiao") + p.add_argument("--to_upper", action="store_true", help="convert to upper case") + p.add_argument("--to_lower", action="store_true", help="convert to lower case") + p.add_argument( + "--remove_fillers", action="store_true", help='remove filler chars such as "呃, 啊"' + ) + p.add_argument( + "--remove_erhua", + action="store_true", + help='remove erhua chars such as "他女儿在那边儿 -> 他女儿在那边"', + ) + p.add_argument( + "--check_chars", action="store_true", help="skip sentences containing illegal chars" + ) + p.add_argument("--remove_space", action="store_true", help="remove whitespace") + + # I/O options + p.add_argument( + "--log_interval", type=int, default=10000, help="log interval in number of processed lines" + ) + p.add_argument( + "--has_key", action="store_true", help="will be deprecated, set --format ark instead" + ) + p.add_argument( + "--format", type=str, choices=["txt", "ark", "tsv"], default="txt", help="input format" + ) + p.add_argument("ifile", help="input filename, assume utf-8 encoding") + p.add_argument("ofile", help="output filename") + + args = p.parse_args() + + if args.has_key: + args.format = "ark" + + normalizer = TextNorm( + to_banjiao=args.to_banjiao, + to_upper=args.to_upper, + to_lower=args.to_lower, + remove_fillers=args.remove_fillers, + remove_erhua=args.remove_erhua, + check_chars=args.check_chars, + remove_space=args.remove_space, + ) + + ndone = 0 + with open(args.ifile, "r", encoding="utf8") as istream, open( + args.ofile, "w+", encoding="utf8" + ) as ostream: + if args.format == "tsv": + reader = csv.DictReader(istream, delimiter="\t") + assert "TEXT" in reader.fieldnames + print("\t".join(reader.fieldnames), file=ostream) + + for item in reader: + text = item["TEXT"] + + if text: + text = normalizer(text) + + if text: + item["TEXT"] = text + print("\t".join([item[f] for f in reader.fieldnames]), file=ostream) + + ndone += 1 + if ndone % args.log_interval == 0: + print(f"text norm: {ndone} lines done.", file=sys.stderr, flush=True) + else: + for l in istream: + key, text = "", "" + if args.format == "ark": # KALDI archive, line format: "key text" + cols = l.strip().split(maxsplit=1) + key, text = cols[0], cols[1] if len(cols) == 2 else "" + else: + text = l.strip() + + if text: + text = normalizer(text) + + if text: + if args.format == "ark": + print(key + "\t" + text, file=ostream) + else: + print(text, file=ostream) + else: + print(" ", file=ostream) + ndone += 1 + if ndone % args.log_interval == 0: + print(f"text norm: {ndone} lines done.", file=sys.stderr, flush=True) + print(f"text norm: {ndone} lines done in total.", file=sys.stderr, flush=True) diff --git a/examples/industrial_data_pretraining/minmo/utils/compute-wer4 b/examples/industrial_data_pretraining/minmo/utils/compute-wer4 new file mode 100644 index 000000000..c5d679072 Binary files /dev/null and b/examples/industrial_data_pretraining/minmo/utils/compute-wer4 differ diff --git a/examples/industrial_data_pretraining/minmo/utils/fleur_proc.py b/examples/industrial_data_pretraining/minmo/utils/fleur_proc.py new file mode 100644 index 000000000..b9cc88ac3 --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/fleur_proc.py @@ -0,0 +1,14 @@ +import re +import sys + +in_f = sys.argv[1] +out_f = sys.argv[2] + +pattern = re.compile(r"([^)]*)") + +with open(in_f, "r") as infile, open(out_f, "w") as outfile: + for line in infile: + key, response = line.strip().split(maxsplit=1) + # cleaned_response = re.sub(r"[^\w\s\u3000\u4e00-\u9fff]+", "", response) + cleaned_response = pattern.sub("", response) + outfile.write(key + "\t" + cleaned_response + "\n") diff --git a/examples/industrial_data_pretraining/minmo/utils/format5resV2.py b/examples/industrial_data_pretraining/minmo/utils/format5resV2.py new file mode 100644 index 000000000..10e5672a8 --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/format5resV2.py @@ -0,0 +1,295 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/python +import sys, re + +# f=sys.stdin + + +def scoreformat(name, line, flag=0): + newline = "" + for i in range(0, len(line)): + curr = line[i] + currEn = False + if curr == "": + continue + if curr.upper() >= "A" and curr.upper() <= "Z" or curr == "'": + currEn = True + if i == 0: + newline = newline + curr.upper() + else: + if lastEn == True and currEn == True: + newline = newline + curr.upper() + else: + newline = newline + " " + curr.upper() + if flag == -1: + lastEn = False + else: + lastEn = currEn + ret = re.sub("[ ]{1,}", " ", newline) + ret = ret + if flag <= 0: + ret = ret + " " + "(" + name + ")" + else: + ret = name + "\t" + ret + return ret + + +def recoformat(line): + newline = "" + en_flag = 0 # 0: no-english 1 : english 2: former + for i in range(0, len(line)): + word = line[i] + if ord(word) == 32: + if en_flag == 0: + continue + else: + en_flag = 0 + newline += " " + # print line[i],ord(word) + if (word >= "\u4e00" and word <= "\u9fa5") or (word >= "\u0030" and word <= "\u0039"): + if en_flag == 1: + newline += " " + word + else: + newline += word + en_flag = 0 + # print "-----",newline + elif ( + (word >= "\u0041" and word <= "\u005a") + or (word >= "\u0061" and word <= "\u007a") + or word == "'" + ): + if en_flag == 0: + newline += " " + ("" if (word == "'") else word) + else: + newline += word + en_flag = 1 + # print "+++",newline + else: + newline += " " + word + # print "0-0-0-0",newline + newline = newline + newline = re.sub("[ ]{1,}", " ", newline) + newline = newline + return newline + + +def numbersingle(line): + chnu = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "点"] + newline = "" + for id in range(len(line)): + if re.findall(r"\.", line[id]): + if re.findall(r"\.\s*$", line[id]): + newline += "." + else: + newline += chnu[10] + elif re.search(r"0", line[id]): + if id > 0 and id < len(line) - 1: + if ( + re.search(r"\d", line[id - 1]) + and (not re.search(r"\d", line[id + 1])) + and (not re.search(r"0", line[id - 1])) + ): + if id > 2 and len(line) > 2 and (not re.search(r"\d", line[id - 1])): + newline = newline[:-1] + newline += chnu[int(line[id - 1])] + "十" + else: + newline += chnu[int(line[id])] + else: + newline += chnu[int(line[id])] + else: + newline += chnu[int(line[id])] + elif re.search(r"\d", line[id]): + newline += chnu[int(line[id])] + else: + newline += line[id] + return newline + + +def ch_number2digit(line): + number_flag = 0 + zero_flag = 0 + # print "ch_umber2digit---------",line + bits = { + "零": "1", + "十": "2", + "百": "3", + "千": "4", + "万": "5", + "十万": "6", + "百万": "7", + "千万": "8", + } + # chnu={'零':"0",'一':"1",'二':"2",'三':"3",'四':"4",'五':"5",'六':"6",'七':"7",'八':"8",'九':"9",'十':"10"] + chsh = { + "一": "1", + "二": "2", + "三": "3", + "四": "4", + "五": "5", + "六": "6", + "七": "7", + "八": "8", + "九": "9", + "两": "2", + "幺": "1", + } + unit = {"里": "1", "克": "1", "米": "1"} + newline = "" + digit = [] + bit = [] + onebit = "" + # digitstr="" + for i in range(len(line)): + if ord(line[i]) == 32: + newline += " " + continue + # print line[i],str(ord(line[i])) + if line[i] in chsh: + number_flag = 1 + if line[i] == "两": + if (i == len(line) - 1) or ( + (line[i + 1] not in chsh.keys()) and (line[i + 1] not in bits.keys()) + ): + number_flag = -1 + if number_flag == 1: + digit.append(chsh[line[i]]) + + elif "十" == line[i] and number_flag == 0: + number_flag = 2 + digit.append("1") + bit.append(line[i]) + elif "十" == line[i] and number_flag == 3: + digit.append("1") + bit.append(line[i]) + elif ("零" == line[i]) and (number_flag == 0 or number_flag == 1): + digit.append("0") + elif ("零" == line[i]) and number_flag == 3: + zero_flag = 1 + elif number_flag == 1 and line[i] in bits: + number_flag = 3 + if line[i] == "千": + if i < len(line) - 1: + if line[i + 1] in unit: + number_flag = -1 + if number_flag == 3: + onebit = line[i] + bit.append(onebit) + elif number_flag == 3 and line[i] in bits: + onebit = bit[-1] + line[i] + if onebit in bits: + bit[-1] = onebit + else: + number_flag = -2 + else: + number_flag = -1 + if len(digit) > 0 and number_flag == -1: + number_flag = -2 + if i == (len(line) - 1) and number_flag >= 0: + number_flag = -1 + # print "number_end_flag",number_flag + if number_flag < 0: + newdigit = "" + # print digit + # print "length:",len(bit), #bit[0]#,bit[0] + if len(digit) > 0: # and (len(digit) == len(bit))): + if len(bit) == 1 and zero_flag == 0 and bit[0] == "百" and len(bit) != len(digit): + bit.append("十") + if len(digit) == (len(bit) + 1): + bit.append("零") + # print digit[:] + # print bit[:] + if len(digit) == len(bit): + for m in range(len(digit))[-1::-1]: + if int(bits[bit[m]]) == int(len(newdigit) + 1): + newdigit += digit[m] + else: + nu = int(bits[bit[m]]) - len(newdigit) - 1 + for n in range(nu): + newdigit += "0" + newdigit += digit[m] + for z in range(len(newdigit))[-1::-1]: + newline += newdigit[z] + else: + newline += "".join(digit) + bit = [] + digit = [] + zero_flag = 0 + else: + newline += line[i] + if number_flag == -2: + newline += line[i] + number_flag = 0 + return newline + + +def special(line): + # print line + newline = "" + for e in range(len(line)): + # print "e ord\t",line[e],ord(line[e]) + if ord(line[e]) == 247: + newline += "除以" + elif ord(line[e]) == 215: + newline += "乘以" + elif ord(line[e]) == 61: + newline += "等于" + elif ord(line[e]) == 43: + newline += "加" + elif ord(line[e]) == 45: + newline += "负" + elif ord(line[e]) == 8451: + newline += "摄氏度" + elif ord(line[e]) == 13217: + newline += "平方米" + elif ord(line[e]) == 8240 or ord(line[e]) == 65130: + newline += "%" + elif ord(line[e]) == 46: + newline += "点" + elif ord(line[e]) == 176: + newline += "度" + angel = 1 + elif ord(line[e]) == 8242 and angel == 1: + newline += "分" + else: + newline += line[e] + return newline + + +if __name__ == "__main__": + if len(sys.argv[1:]) < 1: + sys.stderr.write("Usage:\n .py reco.result\n") + sys.stderr.write(" reco.result: idrecoresult\n") + sys.exit(1) + f = open(sys.argv[1]) + flag = 0 + if len(sys.argv[1:]) > 1: + flag = int(sys.argv[2]) + for line in f.readlines(): + if not line: + continue + line = line.rstrip() + # print line + tmp = line.split("\t") + if len(tmp) < 2: + tmp = line.split(",") + if len(tmp) < 2: + tmp = line.split(" ", 1) + if len(tmp) < 2: + name = tmp[0] + content = "" + print(content) + continue + name = tmp[0] + content = tmp[1] + name = re.sub("\.pcm", "", name) + name = re.sub("\.wav", "", name) + content = recoformat(content) + content = numbersingle(content) + # print "single",content + content = ch_number2digit(content) + # print "digit",content + content = special(content) + # print "special",content + content = scoreformat(name, content, flag) + print(content) + f.close() diff --git a/examples/industrial_data_pretraining/minmo/utils/format_bpemodel_res.py b/examples/industrial_data_pretraining/minmo/utils/format_bpemodel_res.py new file mode 100644 index 000000000..877bf6cda --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/format_bpemodel_res.py @@ -0,0 +1,43 @@ +import sentencepiece as spm +import sys +import string + + +input_file = sys.argv[1] +output_file = sys.argv[2] + +vocab_file = "/nfsspeech/beinian.lzr/workspace/datasets/vocab/funasr/chn_jpn_yue_eng_langid/chn_jpn_yue_eng_langid.vocab.funasr" +bpemodel_file = "/nfsspeech/beinian.lzr/workspace/datasets/vocab/funasr/chn_jpn_yue_eng_langid/chn_jpn_yue_eng_langid.bpe.model" + +vocab_file = "/nfs/beinian.lzr/workspace/local_dataset/vocab/chn_jpn_yue_eng_aed_ser/chn_jpn_yue_eng_spectok.vocab.funasr" +bpemodel_file = "/nfs/beinian.lzr/workspace/local_dataset/vocab/chn_jpn_yue_eng_aed_ser/chn_jpn_yue_eng_spectok.bpe.model" + +vocab_file = "/nfs/beinian.lzr/workspace/local_dataset/vocab/chn_jpn_yue_eng_aed_ser_fix_missing/chn_jpn_yue_eng_spectok_fix.vocab.funasr" +bpemodel_file = "/nfs/beinian.lzr/workspace/local_dataset/vocab/chn_jpn_yue_eng_aed_ser_fix_missing/chn_jpn_yue_eng_spectok_fix.bpe.model" + +sp = spm.SentencePieceProcessor() +sp.load(bpemodel_file) + +vocab_dct = {} +idx = 0 +with open(vocab_file) as f: + for line in f: + ch = line.strip() + vocab_dct[ch] = idx + idx += 1 + +output_fout = open(output_file, "w") + +with open(input_file) as f: + for line in f: + content = line.strip().split(" ", 1) + if len(content) == 2: + key = content[0] + token = content[1].split() + else: + key = content[0] + token = [] + token_int = [vocab_dct[x] for x in token] + token_int = list(filter(lambda x: x < 20055, token_int)) + text = sp.decode(token_int).lower() + output_fout.writelines("{} {}\n".format(key, text)) diff --git a/examples/industrial_data_pretraining/minmo/utils/format_remove_punc.py b/examples/industrial_data_pretraining/minmo/utils/format_remove_punc.py new file mode 100644 index 000000000..0c6b694f7 --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/format_remove_punc.py @@ -0,0 +1,24 @@ +import sys +import string + +input_file = sys.argv[1] +output_file = sys.argv[2] + +with open(input_file, "r") as infile, open(output_file, "w") as outfile: + for line in infile: + content = line.strip().split("\t", 1) + if len(content) == 2: + utt, text = content[0], content[1] + else: + utt = content[0] + text = "" + # 创建一个翻译表,将所有标点符号(除了撇号)映射为 None + translator = str.maketrans("", "", string.punctuation.replace("'", "")) + + # 使用翻译表去除标点符号 + no_punctuation_text = text.translate(translator) + + # 将所有英文字符转换成小写 + lowercase_text = no_punctuation_text.lower() + + outfile.write(utt + "\t" + lowercase_text + "\n") diff --git a/examples/industrial_data_pretraining/minmo/utils/openai_whisper.json b/examples/industrial_data_pretraining/minmo/utils/openai_whisper.json new file mode 100644 index 000000000..bd84ae73f --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/openai_whisper.json @@ -0,0 +1,1742 @@ +{ + "accessorise": "accessorize", + "accessorised": "accessorized", + "accessorises": "accessorizes", + "accessorising": "accessorizing", + "acclimatisation": "acclimatization", + "acclimatise": "acclimatize", + "acclimatised": "acclimatized", + "acclimatises": "acclimatizes", + "acclimatising": "acclimatizing", + "accoutrements": "accouterments", + "aeon": "eon", + "aeons": "eons", + "aerogramme": "aerogram", + "aerogrammes": "aerograms", + "aeroplane": "airplane", + "aeroplanes": "airplanes", + "aesthete": "esthete", + "aesthetes": "esthetes", + "aesthetic": "esthetic", + "aesthetically": "esthetically", + "aesthetics": "esthetics", + "aetiology": "etiology", + "ageing": "aging", + "aggrandisement": "aggrandizement", + "agonise": "agonize", + "agonised": "agonized", + "agonises": "agonizes", + "agonising": "agonizing", + "agonisingly": "agonizingly", + "almanack": "almanac", + "almanacks": "almanacs", + "aluminium": "aluminum", + "amortisable": "amortizable", + "amortisation": "amortization", + "amortisations": "amortizations", + "amortise": "amortize", + "amortised": "amortized", + "amortises": "amortizes", + "amortising": "amortizing", + "amphitheatre": "amphitheater", + "amphitheatres": "amphitheaters", + "anaemia": "anemia", + "anaemic": "anemic", + "anaesthesia": "anesthesia", + "anaesthetic": "anesthetic", + "anaesthetics": "anesthetics", + "anaesthetise": "anesthetize", + "anaesthetised": "anesthetized", + "anaesthetises": "anesthetizes", + "anaesthetising": "anesthetizing", + "anaesthetist": "anesthetist", + "anaesthetists": "anesthetists", + "anaesthetize": "anesthetize", + "anaesthetized": "anesthetized", + "anaesthetizes": "anesthetizes", + "anaesthetizing": "anesthetizing", + "analogue": "analog", + "analogues": "analogs", + "analyse": "analyze", + "analysed": "analyzed", + "analyses": "analyzes", + "analysing": "analyzing", + "anglicise": "anglicize", + "anglicised": "anglicized", + "anglicises": "anglicizes", + "anglicising": "anglicizing", + "annualised": "annualized", + "antagonise": "antagonize", + "antagonised": "antagonized", + "antagonises": "antagonizes", + "antagonising": "antagonizing", + "apologise": "apologize", + "apologised": "apologized", + "apologises": "apologizes", + "apologising": "apologizing", + "appal": "appall", + "appals": "appalls", + "appetiser": "appetizer", + "appetisers": "appetizers", + "appetising": "appetizing", + "appetisingly": "appetizingly", + "arbour": "arbor", + "arbours": "arbors", + "archeological": "archaeological", + "archaeologically": "archeologically", + "archaeologist": "archeologist", + "archaeologists": "archeologists", + "archaeology": "archeology", + "ardour": "ardor", + "armour": "armor", + "armoured": "armored", + "armourer": "armorer", + "armourers": "armorers", + "armouries": "armories", + "armoury": "armory", + "artefact": "artifact", + "artefacts": "artifacts", + "authorise": "authorize", + "authorised": "authorized", + "authorises": "authorizes", + "authorising": "authorizing", + "axe": "ax", + "backpedalled": "backpedaled", + "backpedalling": "backpedaling", + "bannister": "banister", + "bannisters": "banisters", + "baptise": "baptize", + "baptised": "baptized", + "baptises": "baptizes", + "baptising": "baptizing", + "bastardise": "bastardize", + "bastardised": "bastardized", + "bastardises": "bastardizes", + "bastardising": "bastardizing", + "battleax": "battleaxe", + "baulk": "balk", + "baulked": "balked", + "baulking": "balking", + "baulks": "balks", + "bedevilled": "bedeviled", + "bedevilling": "bedeviling", + "behaviour": "behavior", + "behavioural": "behavioral", + "behaviourism": "behaviorism", + "behaviourist": "behaviorist", + "behaviourists": "behaviorists", + "behaviours": "behaviors", + "behove": "behoove", + "behoved": "behooved", + "behoves": "behooves", + "bejewelled": "bejeweled", + "belabour": "belabor", + "belaboured": "belabored", + "belabouring": "belaboring", + "belabours": "belabors", + "bevelled": "beveled", + "bevvies": "bevies", + "bevvy": "bevy", + "biassed": "biased", + "biassing": "biasing", + "bingeing": "binging", + "bougainvillaea": "bougainvillea", + "bougainvillaeas": "bougainvilleas", + "bowdlerise": "bowdlerize", + "bowdlerised": "bowdlerized", + "bowdlerises": "bowdlerizes", + "bowdlerising": "bowdlerizing", + "breathalyse": "breathalyze", + "breathalysed": "breathalyzed", + "breathalyser": "breathalyzer", + "breathalysers": "breathalyzers", + "breathalyses": "breathalyzes", + "breathalysing": "breathalyzing", + "brutalise": "brutalize", + "brutalised": "brutalized", + "brutalises": "brutalizes", + "brutalising": "brutalizing", + "busses": "buses", + "bussing": "busing", + "caesarean": "cesarean", + "caesareans": "cesareans", + "calibre": "caliber", + "calibres": "calibers", + "calliper": "caliper", + "callipers": "calipers", + "callisthenics": "calisthenics", + "canalise": "canalize", + "canalised": "canalized", + "canalises": "canalizes", + "canalising": "canalizing", + "cancelation": "cancellation", + "cancelations": "cancellations", + "cancelled": "canceled", + "cancelling": "canceling", + "candour": "candor", + "cannibalise": "cannibalize", + "cannibalised": "cannibalized", + "cannibalises": "cannibalizes", + "cannibalising": "cannibalizing", + "canonise": "canonize", + "canonised": "canonized", + "canonises": "canonizes", + "canonising": "canonizing", + "capitalise": "capitalize", + "capitalised": "capitalized", + "capitalises": "capitalizes", + "capitalising": "capitalizing", + "caramelise": "caramelize", + "caramelised": "caramelized", + "caramelises": "caramelizes", + "caramelising": "caramelizing", + "carbonise": "carbonize", + "carbonised": "carbonized", + "carbonises": "carbonizes", + "carbonising": "carbonizing", + "carolled": "caroled", + "carolling": "caroling", + "catalogue": "catalog", + "catalogued": "cataloged", + "catalogues": "catalogs", + "cataloguing": "cataloging", + "catalyse": "catalyze", + "catalysed": "catalyzed", + "catalyses": "catalyzes", + "catalysing": "catalyzing", + "categorise": "categorize", + "categorised": "categorized", + "categorises": "categorizes", + "categorising": "categorizing", + "cauterise": "cauterize", + "cauterised": "cauterized", + "cauterises": "cauterizes", + "cauterising": "cauterizing", + "cavilled": "caviled", + "cavilling": "caviling", + "centigramme": "centigram", + "centigrammes": "centigrams", + "centilitre": "centiliter", + "centilitres": "centiliters", + "centimetre": "centimeter", + "centimetres": "centimeters", + "centralise": "centralize", + "centralised": "centralized", + "centralises": "centralizes", + "centralising": "centralizing", + "centre": "center", + "centred": "centered", + "centrefold": "centerfold", + "centrefolds": "centerfolds", + "centrepiece": "centerpiece", + "centrepieces": "centerpieces", + "centres": "centers", + "channelled": "channeled", + "channelling": "channeling", + "characterise": "characterize", + "characterised": "characterized", + "characterises": "characterizes", + "characterising": "characterizing", + "cheque": "check", + "chequebook": "checkbook", + "chequebooks": "checkbooks", + "chequered": "checkered", + "cheques": "checks", + "chilli": "chili", + "chimaera": "chimera", + "chimaeras": "chimeras", + "chiselled": "chiseled", + "chiselling": "chiseling", + "circularise": "circularize", + "circularised": "circularized", + "circularises": "circularizes", + "circularising": "circularizing", + "civilise": "civilize", + "civilised": "civilized", + "civilises": "civilizes", + "civilising": "civilizing", + "clamour": "clamor", + "clamoured": "clamored", + "clamouring": "clamoring", + "clamours": "clamors", + "clangour": "clangor", + "clarinettist": "clarinetist", + "clarinettists": "clarinetists", + "collectivise": "collectivize", + "collectivised": "collectivized", + "collectivises": "collectivizes", + "collectivising": "collectivizing", + "colonisation": "colonization", + "colonise": "colonize", + "colonised": "colonized", + "coloniser": "colonizer", + "colonisers": "colonizers", + "colonises": "colonizes", + "colonising": "colonizing", + "colour": "color", + "colourant": "colorant", + "colourants": "colorants", + "coloured": "colored", + "coloureds": "coloreds", + "colourful": "colorful", + "colourfully": "colorfully", + "colouring": "coloring", + "colourize": "colorize", + "colourized": "colorized", + "colourizes": "colorizes", + "colourizing": "colorizing", + "colourless": "colorless", + "colours": "colors", + "commercialise": "commercialize", + "commercialised": "commercialized", + "commercialises": "commercializes", + "commercialising": "commercializing", + "compartmentalise": "compartmentalize", + "compartmentalised": "compartmentalized", + "compartmentalises": "compartmentalizes", + "compartmentalising": "compartmentalizing", + "computerise": "computerize", + "computerised": "computerized", + "computerises": "computerizes", + "computerising": "computerizing", + "conceptualise": "conceptualize", + "conceptualised": "conceptualized", + "conceptualises": "conceptualizes", + "conceptualising": "conceptualizing", + "connexion": "connection", + "connexions": "connections", + "contextualise": "contextualize", + "contextualised": "contextualized", + "contextualises": "contextualizes", + "contextualising": "contextualizing", + "cosier": "cozier", + "cosies": "cozies", + "cosiest": "coziest", + "cosily": "cozily", + "cosiness": "coziness", + "cosy": "cozy", + "councillor": "councilor", + "councillors": "councilors", + "counselled": "counseled", + "counselling": "counseling", + "counsellor": "counselor", + "counsellors": "counselors", + "crenelated": "crenellated", + "criminalise": "criminalize", + "criminalised": "criminalized", + "criminalises": "criminalizes", + "criminalising": "criminalizing", + "criticise": "criticize", + "criticised": "criticized", + "criticises": "criticizes", + "criticising": "criticizing", + "crueller": "crueler", + "cruellest": "cruelest", + "crystallisation": "crystallization", + "crystallise": "crystallize", + "crystallised": "crystallized", + "crystallises": "crystallizes", + "crystallising": "crystallizing", + "cudgelled": "cudgeled", + "cudgelling": "cudgeling", + "customise": "customize", + "customised": "customized", + "customises": "customizes", + "customising": "customizing", + "cypher": "cipher", + "cyphers": "ciphers", + "decentralisation": "decentralization", + "decentralise": "decentralize", + "decentralised": "decentralized", + "decentralises": "decentralizes", + "decentralising": "decentralizing", + "decriminalisation": "decriminalization", + "decriminalise": "decriminalize", + "decriminalised": "decriminalized", + "decriminalises": "decriminalizes", + "decriminalising": "decriminalizing", + "defence": "defense", + "defenceless": "defenseless", + "defences": "defenses", + "dehumanisation": "dehumanization", + "dehumanise": "dehumanize", + "dehumanised": "dehumanized", + "dehumanises": "dehumanizes", + "dehumanising": "dehumanizing", + "demeanour": "demeanor", + "demilitarisation": "demilitarization", + "demilitarise": "demilitarize", + "demilitarised": "demilitarized", + "demilitarises": "demilitarizes", + "demilitarising": "demilitarizing", + "demobilisation": "demobilization", + "demobilise": "demobilize", + "demobilised": "demobilized", + "demobilises": "demobilizes", + "demobilising": "demobilizing", + "democratisation": "democratization", + "democratise": "democratize", + "democratised": "democratized", + "democratises": "democratizes", + "democratising": "democratizing", + "demonise": "demonize", + "demonised": "demonized", + "demonises": "demonizes", + "demonising": "demonizing", + "demoralisation": "demoralization", + "demoralise": "demoralize", + "demoralised": "demoralized", + "demoralises": "demoralizes", + "demoralising": "demoralizing", + "denationalisation": "denationalization", + "denationalise": "denationalize", + "denationalised": "denationalized", + "denationalises": "denationalizes", + "denationalising": "denationalizing", + "deodorise": "deodorize", + "deodorised": "deodorized", + "deodorises": "deodorizes", + "deodorising": "deodorizing", + "depersonalise": "depersonalize", + "depersonalised": "depersonalized", + "depersonalises": "depersonalizes", + "depersonalising": "depersonalizing", + "deputise": "deputize", + "deputised": "deputized", + "deputises": "deputizes", + "deputising": "deputizing", + "desensitisation": "desensitization", + "desensitise": "desensitize", + "desensitised": "desensitized", + "desensitises": "desensitizes", + "desensitising": "desensitizing", + "destabilisation": "destabilization", + "destabilise": "destabilize", + "destabilised": "destabilized", + "destabilises": "destabilizes", + "destabilising": "destabilizing", + "dialled": "dialed", + "dialling": "dialing", + "dialogue": "dialog", + "dialogues": "dialogs", + "diarrhoea": "diarrhea", + "digitise": "digitize", + "digitised": "digitized", + "digitises": "digitizes", + "digitising": "digitizing", + "disc": "disk", + "discolour": "discolor", + "discoloured": "discolored", + "discolouring": "discoloring", + "discolours": "discolors", + "discs": "disks", + "disembowelled": "disemboweled", + "disembowelling": "disemboweling", + "disfavour": "disfavor", + "dishevelled": "disheveled", + "dishonour": "dishonor", + "dishonourable": "dishonorable", + "dishonourably": "dishonorably", + "dishonoured": "dishonored", + "dishonouring": "dishonoring", + "dishonours": "dishonors", + "disorganisation": "disorganization", + "disorganised": "disorganized", + "distil": "distill", + "distils": "distills", + "dramatisation": "dramatization", + "dramatisations": "dramatizations", + "dramatise": "dramatize", + "dramatised": "dramatized", + "dramatises": "dramatizes", + "dramatising": "dramatizing", + "draught": "draft", + "draughtboard": "draftboard", + "draughtboards": "draftboards", + "draughtier": "draftier", + "draughtiest": "draftiest", + "draughts": "drafts", + "draughtsman": "draftsman", + "draughtsmanship": "draftsmanship", + "draughtsmen": "draftsmen", + "draughtswoman": "draftswoman", + "draughtswomen": "draftswomen", + "draughty": "drafty", + "drivelled": "driveled", + "drivelling": "driveling", + "duelled": "dueled", + "duelling": "dueling", + "economise": "economize", + "economised": "economized", + "economises": "economizes", + "economising": "economizing", + "edoema": "edema", + "editorialise": "editorialize", + "editorialised": "editorialized", + "editorialises": "editorializes", + "editorialising": "editorializing", + "empathise": "empathize", + "empathised": "empathized", + "empathises": "empathizes", + "empathising": "empathizing", + "emphasise": "emphasize", + "emphasised": "emphasized", + "emphasises": "emphasizes", + "emphasising": "emphasizing", + "enamelled": "enameled", + "enamelling": "enameling", + "enamoured": "enamored", + "encyclopaedia": "encyclopedia", + "encyclopaedias": "encyclopedias", + "encyclopaedic": "encyclopedic", + "endeavour": "endeavor", + "endeavoured": "endeavored", + "endeavouring": "endeavoring", + "endeavours": "endeavors", + "energise": "energize", + "energised": "energized", + "energises": "energizes", + "energising": "energizing", + "enrol": "enroll", + "enrols": "enrolls", + "enthral": "enthrall", + "enthrals": "enthralls", + "epaulette": "epaulet", + "epaulettes": "epaulets", + "epicentre": "epicenter", + "epicentres": "epicenters", + "epilogue": "epilog", + "epilogues": "epilogs", + "epitomise": "epitomize", + "epitomised": "epitomized", + "epitomises": "epitomizes", + "epitomising": "epitomizing", + "equalisation": "equalization", + "equalise": "equalize", + "equalised": "equalized", + "equaliser": "equalizer", + "equalisers": "equalizers", + "equalises": "equalizes", + "equalising": "equalizing", + "eulogise": "eulogize", + "eulogised": "eulogized", + "eulogises": "eulogizes", + "eulogising": "eulogizing", + "evangelise": "evangelize", + "evangelised": "evangelized", + "evangelises": "evangelizes", + "evangelising": "evangelizing", + "exorcise": "exorcize", + "exorcised": "exorcized", + "exorcises": "exorcizes", + "exorcising": "exorcizing", + "extemporisation": "extemporization", + "extemporise": "extemporize", + "extemporised": "extemporized", + "extemporises": "extemporizes", + "extemporising": "extemporizing", + "externalisation": "externalization", + "externalisations": "externalizations", + "externalise": "externalize", + "externalised": "externalized", + "externalises": "externalizes", + "externalising": "externalizing", + "factorise": "factorize", + "factorised": "factorized", + "factorises": "factorizes", + "factorising": "factorizing", + "faecal": "fecal", + "faeces": "feces", + "familiarisation": "familiarization", + "familiarise": "familiarize", + "familiarised": "familiarized", + "familiarises": "familiarizes", + "familiarising": "familiarizing", + "fantasise": "fantasize", + "fantasised": "fantasized", + "fantasises": "fantasizes", + "fantasising": "fantasizing", + "favour": "favor", + "favourable": "favorable", + "favourably": "favorably", + "favoured": "favored", + "favouring": "favoring", + "favourite": "favorite", + "favourites": "favorites", + "favouritism": "favoritism", + "favours": "favors", + "feminise": "feminize", + "feminised": "feminized", + "feminises": "feminizes", + "feminising": "feminizing", + "fertilisation": "fertilization", + "fertilise": "fertilize", + "fertilised": "fertilized", + "fertiliser": "fertilizer", + "fertilisers": "fertilizers", + "fertilises": "fertilizes", + "fertilising": "fertilizing", + "fervour": "fervor", + "fibre": "fiber", + "fibreglass": "fiberglass", + "fibres": "fibers", + "fictionalisation": "fictionalization", + "fictionalisations": "fictionalizations", + "fictionalise": "fictionalize", + "fictionalised": "fictionalized", + "fictionalises": "fictionalizes", + "fictionalising": "fictionalizing", + "fillet": "filet", + "filleted": "fileted", + "filleting": "fileting", + "fillets": "filets", + "finalisation": "finalization", + "finalise": "finalize", + "finalised": "finalized", + "finalises": "finalizes", + "finalising": "finalizing", + "flautist": "flutist", + "flautists": "flutists", + "flavour": "flavor", + "flavoured": "flavored", + "flavouring": "flavoring", + "flavourings": "flavorings", + "flavourless": "flavorless", + "flavours": "flavors", + "flavoursome": "flavorsome", + "flyer / flier": "flier / flyer", + "foetal": "fetal", + "foetid": "fetid", + "foetus": "fetus", + "foetuses": "fetuses", + "formalisation": "formalization", + "formalise": "formalize", + "formalised": "formalized", + "formalises": "formalizes", + "formalising": "formalizing", + "fossilisation": "fossilization", + "fossilise": "fossilize", + "fossilised": "fossilized", + "fossilises": "fossilizes", + "fossilising": "fossilizing", + "fraternisation": "fraternization", + "fraternise": "fraternize", + "fraternised": "fraternized", + "fraternises": "fraternizes", + "fraternising": "fraternizing", + "fulfil": "fulfill", + "fulfilment": "fulfillment", + "fulfils": "fulfills", + "funnelled": "funneled", + "funnelling": "funneling", + "galvanise": "galvanize", + "galvanised": "galvanized", + "galvanises": "galvanizes", + "galvanising": "galvanizing", + "gambolled": "gamboled", + "gambolling": "gamboling", + "gaol": "jail", + "gaolbird": "jailbird", + "gaolbirds": "jailbirds", + "gaolbreak": "jailbreak", + "gaolbreaks": "jailbreaks", + "gaoled": "jailed", + "gaoler": "jailer", + "gaolers": "jailers", + "gaoling": "jailing", + "gaols": "jails", + "gasses": "gases", + "gage": "gauge", + "gaged": "gauged", + "gages": "gauges", + "gaging": "gauging", + "generalisation": "generalization", + "generalisations": "generalizations", + "generalise": "generalize", + "generalised": "generalized", + "generalises": "generalizes", + "generalising": "generalizing", + "ghettoise": "ghettoize", + "ghettoised": "ghettoized", + "ghettoises": "ghettoizes", + "ghettoising": "ghettoizing", + "gipsies": "gypsies", + "glamorise": "glamorize", + "glamorised": "glamorized", + "glamorises": "glamorizes", + "glamorising": "glamorizing", + "glamor": "glamour", + "globalisation": "globalization", + "globalise": "globalize", + "globalised": "globalized", + "globalises": "globalizes", + "globalising": "globalizing", + "glueing": "gluing", + "goitre": "goiter", + "goitres": "goiters", + "gonorrhoea": "gonorrhea", + "gramme": "gram", + "grammes": "grams", + "gravelled": "graveled", + "grey": "gray", + "greyed": "grayed", + "greying": "graying", + "greyish": "grayish", + "greyness": "grayness", + "greys": "grays", + "grovelled": "groveled", + "grovelling": "groveling", + "groyne": "groin", + "groynes": "groins", + "gruelling": "grueling", + "gruellingly": "gruelingly", + "gryphon": "griffin", + "gryphons": "griffins", + "gynaecological": "gynecological", + "gynaecologist": "gynecologist", + "gynaecologists": "gynecologists", + "gynaecology": "gynecology", + "haematological": "hematological", + "haematologist": "hematologist", + "haematologists": "hematologists", + "haematology": "hematology", + "haemoglobin": "hemoglobin", + "haemophilia": "hemophilia", + "haemophiliac": "hemophiliac", + "haemophiliacs": "hemophiliacs", + "haemorrhage": "hemorrhage", + "haemorrhaged": "hemorrhaged", + "haemorrhages": "hemorrhages", + "haemorrhaging": "hemorrhaging", + "haemorrhoids": "hemorrhoids", + "harbour": "harbor", + "harboured": "harbored", + "harbouring": "harboring", + "harbours": "harbors", + "harmonisation": "harmonization", + "harmonise": "harmonize", + "harmonised": "harmonized", + "harmonises": "harmonizes", + "harmonising": "harmonizing", + "homoeopath": "homeopath", + "homoeopathic": "homeopathic", + "homoeopaths": "homeopaths", + "homoeopathy": "homeopathy", + "homogenise": "homogenize", + "homogenised": "homogenized", + "homogenises": "homogenizes", + "homogenising": "homogenizing", + "honour": "honor", + "honourable": "honorable", + "honourably": "honorably", + "honoured": "honored", + "honouring": "honoring", + "honours": "honors", + "hospitalisation": "hospitalization", + "hospitalise": "hospitalize", + "hospitalised": "hospitalized", + "hospitalises": "hospitalizes", + "hospitalising": "hospitalizing", + "humanise": "humanize", + "humanised": "humanized", + "humanises": "humanizes", + "humanising": "humanizing", + "humour": "humor", + "humoured": "humored", + "humouring": "humoring", + "humourless": "humorless", + "humours": "humors", + "hybridise": "hybridize", + "hybridised": "hybridized", + "hybridises": "hybridizes", + "hybridising": "hybridizing", + "hypnotise": "hypnotize", + "hypnotised": "hypnotized", + "hypnotises": "hypnotizes", + "hypnotising": "hypnotizing", + "hypothesise": "hypothesize", + "hypothesised": "hypothesized", + "hypothesises": "hypothesizes", + "hypothesising": "hypothesizing", + "idealisation": "idealization", + "idealise": "idealize", + "idealised": "idealized", + "idealises": "idealizes", + "idealising": "idealizing", + "idolise": "idolize", + "idolised": "idolized", + "idolises": "idolizes", + "idolising": "idolizing", + "immobilisation": "immobilization", + "immobilise": "immobilize", + "immobilised": "immobilized", + "immobiliser": "immobilizer", + "immobilisers": "immobilizers", + "immobilises": "immobilizes", + "immobilising": "immobilizing", + "immortalise": "immortalize", + "immortalised": "immortalized", + "immortalises": "immortalizes", + "immortalising": "immortalizing", + "immunisation": "immunization", + "immunise": "immunize", + "immunised": "immunized", + "immunises": "immunizes", + "immunising": "immunizing", + "impanelled": "impaneled", + "impanelling": "impaneling", + "imperilled": "imperiled", + "imperilling": "imperiling", + "individualise": "individualize", + "individualised": "individualized", + "individualises": "individualizes", + "individualising": "individualizing", + "industrialise": "industrialize", + "industrialised": "industrialized", + "industrialises": "industrializes", + "industrialising": "industrializing", + "inflexion": "inflection", + "inflexions": "inflections", + "initialise": "initialize", + "initialised": "initialized", + "initialises": "initializes", + "initialising": "initializing", + "initialled": "initialed", + "initialling": "initialing", + "instal": "install", + "instalment": "installment", + "instalments": "installments", + "instals": "installs", + "instil": "instill", + "instils": "instills", + "institutionalisation": "institutionalization", + "institutionalise": "institutionalize", + "institutionalised": "institutionalized", + "institutionalises": "institutionalizes", + "institutionalising": "institutionalizing", + "intellectualise": "intellectualize", + "intellectualised": "intellectualized", + "intellectualises": "intellectualizes", + "intellectualising": "intellectualizing", + "internalisation": "internalization", + "internalise": "internalize", + "internalised": "internalized", + "internalises": "internalizes", + "internalising": "internalizing", + "internationalisation": "internationalization", + "internationalise": "internationalize", + "internationalised": "internationalized", + "internationalises": "internationalizes", + "internationalising": "internationalizing", + "ionisation": "ionization", + "ionise": "ionize", + "ionised": "ionized", + "ioniser": "ionizer", + "ionisers": "ionizers", + "ionises": "ionizes", + "ionising": "ionizing", + "italicise": "italicize", + "italicised": "italicized", + "italicises": "italicizes", + "italicising": "italicizing", + "itemise": "itemize", + "itemised": "itemized", + "itemises": "itemizes", + "itemising": "itemizing", + "jeopardise": "jeopardize", + "jeopardised": "jeopardized", + "jeopardises": "jeopardizes", + "jeopardising": "jeopardizing", + "jewelled": "jeweled", + "jeweller": "jeweler", + "jewellers": "jewelers", + "jewellery": "jewelry", + "judgement": "judgment", + "kilogramme": "kilogram", + "kilogrammes": "kilograms", + "kilometre": "kilometer", + "kilometres": "kilometers", + "labelled": "labeled", + "labelling": "labeling", + "labour": "labor", + "laboured": "labored", + "labourer": "laborer", + "labourers": "laborers", + "labouring": "laboring", + "labours": "labors", + "lacklustre": "lackluster", + "legalisation": "legalization", + "legalise": "legalize", + "legalised": "legalized", + "legalises": "legalizes", + "legalising": "legalizing", + "legitimise": "legitimize", + "legitimised": "legitimized", + "legitimises": "legitimizes", + "legitimising": "legitimizing", + "leukaemia": "leukemia", + "levelled": "leveled", + "leveller": "leveler", + "levellers": "levelers", + "levelling": "leveling", + "libelled": "libeled", + "libelling": "libeling", + "libellous": "libelous", + "liberalisation": "liberalization", + "liberalise": "liberalize", + "liberalised": "liberalized", + "liberalises": "liberalizes", + "liberalising": "liberalizing", + "licence": "license", + "licenced": "licensed", + "licences": "licenses", + "licencing": "licensing", + "likeable": "likable", + "lionisation": "lionization", + "lionise": "lionize", + "lionised": "lionized", + "lionises": "lionizes", + "lionising": "lionizing", + "liquidise": "liquidize", + "liquidised": "liquidized", + "liquidiser": "liquidizer", + "liquidisers": "liquidizers", + "liquidises": "liquidizes", + "liquidising": "liquidizing", + "litre": "liter", + "litres": "liters", + "localise": "localize", + "localised": "localized", + "localises": "localizes", + "localising": "localizing", + "louvre": "louver", + "louvred": "louvered", + "louvres": "louvers", + "lustre": "luster", + "magnetise": "magnetize", + "magnetised": "magnetized", + "magnetises": "magnetizes", + "magnetising": "magnetizing", + "manoeuvrability": "maneuverability", + "manoeuvrable": "maneuverable", + "manoeuvre": "maneuver", + "manoeuvred": "maneuvered", + "manoeuvres": "maneuvers", + "manoeuvring": "maneuvering", + "manoeuvrings": "maneuverings", + "marginalisation": "marginalization", + "marginalise": "marginalize", + "marginalised": "marginalized", + "marginalises": "marginalizes", + "marginalising": "marginalizing", + "marshalled": "marshaled", + "marshalling": "marshaling", + "marvelled": "marveled", + "marvelling": "marveling", + "marvellous": "marvelous", + "marvellously": "marvelously", + "materialisation": "materialization", + "materialise": "materialize", + "materialised": "materialized", + "materialises": "materializes", + "materialising": "materializing", + "maximisation": "maximization", + "maximise": "maximize", + "maximised": "maximized", + "maximises": "maximizes", + "maximising": "maximizing", + "meagre": "meager", + "mechanisation": "mechanization", + "mechanise": "mechanize", + "mechanised": "mechanized", + "mechanises": "mechanizes", + "mechanising": "mechanizing", + "mediaeval": "medieval", + "memorialise": "memorialize", + "memorialised": "memorialized", + "memorialises": "memorializes", + "memorialising": "memorializing", + "memorise": "memorize", + "memorised": "memorized", + "memorises": "memorizes", + "memorising": "memorizing", + "mesmerise": "mesmerize", + "mesmerised": "mesmerized", + "mesmerises": "mesmerizes", + "mesmerising": "mesmerizing", + "metabolise": "metabolize", + "metabolised": "metabolized", + "metabolises": "metabolizes", + "metabolising": "metabolizing", + "metre": "meter", + "metres": "meters", + "micrometre": "micrometer", + "micrometres": "micrometers", + "militarise": "militarize", + "militarised": "militarized", + "militarises": "militarizes", + "militarising": "militarizing", + "milligramme": "milligram", + "milligrammes": "milligrams", + "millilitre": "milliliter", + "millilitres": "milliliters", + "millimetre": "millimeter", + "millimetres": "millimeters", + "miniaturisation": "miniaturization", + "miniaturise": "miniaturize", + "miniaturised": "miniaturized", + "miniaturises": "miniaturizes", + "miniaturising": "miniaturizing", + "minibusses": "minibuses", + "minimise": "minimize", + "minimised": "minimized", + "minimises": "minimizes", + "minimising": "minimizing", + "misbehaviour": "misbehavior", + "misdemeanour": "misdemeanor", + "misdemeanours": "misdemeanors", + "misspelt": "misspelled", + "mitre": "miter", + "mitres": "miters", + "mobilisation": "mobilization", + "mobilise": "mobilize", + "mobilised": "mobilized", + "mobilises": "mobilizes", + "mobilising": "mobilizing", + "modelled": "modeled", + "modeller": "modeler", + "modellers": "modelers", + "modelling": "modeling", + "modernise": "modernize", + "modernised": "modernized", + "modernises": "modernizes", + "modernising": "modernizing", + "moisturise": "moisturize", + "moisturised": "moisturized", + "moisturiser": "moisturizer", + "moisturisers": "moisturizers", + "moisturises": "moisturizes", + "moisturising": "moisturizing", + "monologue": "monolog", + "monologues": "monologs", + "monopolisation": "monopolization", + "monopolise": "monopolize", + "monopolised": "monopolized", + "monopolises": "monopolizes", + "monopolising": "monopolizing", + "moralise": "moralize", + "moralised": "moralized", + "moralises": "moralizes", + "moralising": "moralizing", + "motorised": "motorized", + "mould": "mold", + "moulded": "molded", + "moulder": "molder", + "mouldered": "moldered", + "mouldering": "moldering", + "moulders": "molders", + "mouldier": "moldier", + "mouldiest": "moldiest", + "moulding": "molding", + "mouldings": "moldings", + "moulds": "molds", + "mouldy": "moldy", + "moult": "molt", + "moulted": "molted", + "moulting": "molting", + "moults": "molts", + "moustache": "mustache", + "moustached": "mustached", + "moustaches": "mustaches", + "moustachioed": "mustachioed", + "multicoloured": "multicolored", + "nationalisation": "nationalization", + "nationalisations": "nationalizations", + "nationalise": "nationalize", + "nationalised": "nationalized", + "nationalises": "nationalizes", + "nationalising": "nationalizing", + "naturalisation": "naturalization", + "naturalise": "naturalize", + "naturalised": "naturalized", + "naturalises": "naturalizes", + "naturalising": "naturalizing", + "neighbour": "neighbor", + "neighbourhood": "neighborhood", + "neighbourhoods": "neighborhoods", + "neighbouring": "neighboring", + "neighbourliness": "neighborliness", + "neighbourly": "neighborly", + "neighbours": "neighbors", + "neutralisation": "neutralization", + "neutralise": "neutralize", + "neutralised": "neutralized", + "neutralises": "neutralizes", + "neutralising": "neutralizing", + "normalisation": "normalization", + "normalise": "normalize", + "normalised": "normalized", + "normalises": "normalizes", + "normalising": "normalizing", + "odour": "odor", + "odourless": "odorless", + "odours": "odors", + "oesophagus": "esophagus", + "oesophaguses": "esophaguses", + "oestrogen": "estrogen", + "offence": "offense", + "offences": "offenses", + "omelette": "omelet", + "omelettes": "omelets", + "optimise": "optimize", + "optimised": "optimized", + "optimises": "optimizes", + "optimising": "optimizing", + "organisation": "organization", + "organisational": "organizational", + "organisations": "organizations", + "organise": "organize", + "organised": "organized", + "organiser": "organizer", + "organisers": "organizers", + "organises": "organizes", + "organising": "organizing", + "orthopaedic": "orthopedic", + "orthopaedics": "orthopedics", + "ostracise": "ostracize", + "ostracised": "ostracized", + "ostracises": "ostracizes", + "ostracising": "ostracizing", + "outmanoeuvre": "outmaneuver", + "outmanoeuvred": "outmaneuvered", + "outmanoeuvres": "outmaneuvers", + "outmanoeuvring": "outmaneuvering", + "overemphasise": "overemphasize", + "overemphasised": "overemphasized", + "overemphasises": "overemphasizes", + "overemphasising": "overemphasizing", + "oxidisation": "oxidization", + "oxidise": "oxidize", + "oxidised": "oxidized", + "oxidises": "oxidizes", + "oxidising": "oxidizing", + "paederast": "pederast", + "paederasts": "pederasts", + "paediatric": "pediatric", + "paediatrician": "pediatrician", + "paediatricians": "pediatricians", + "paediatrics": "pediatrics", + "paedophile": "pedophile", + "paedophiles": "pedophiles", + "paedophilia": "pedophilia", + "palaeolithic": "paleolithic", + "palaeontologist": "paleontologist", + "palaeontologists": "paleontologists", + "palaeontology": "paleontology", + "panelled": "paneled", + "panelling": "paneling", + "panellist": "panelist", + "panellists": "panelists", + "paralyse": "paralyze", + "paralysed": "paralyzed", + "paralyses": "paralyzes", + "paralysing": "paralyzing", + "parcelled": "parceled", + "parcelling": "parceling", + "parlour": "parlor", + "parlours": "parlors", + "particularise": "particularize", + "particularised": "particularized", + "particularises": "particularizes", + "particularising": "particularizing", + "passivisation": "passivization", + "passivise": "passivize", + "passivised": "passivized", + "passivises": "passivizes", + "passivising": "passivizing", + "pasteurisation": "pasteurization", + "pasteurise": "pasteurize", + "pasteurised": "pasteurized", + "pasteurises": "pasteurizes", + "pasteurising": "pasteurizing", + "patronise": "patronize", + "patronised": "patronized", + "patronises": "patronizes", + "patronising": "patronizing", + "patronisingly": "patronizingly", + "pedalled": "pedaled", + "pedalling": "pedaling", + "pedestrianisation": "pedestrianization", + "pedestrianise": "pedestrianize", + "pedestrianised": "pedestrianized", + "pedestrianises": "pedestrianizes", + "pedestrianising": "pedestrianizing", + "penalise": "penalize", + "penalised": "penalized", + "penalises": "penalizes", + "penalising": "penalizing", + "pencilled": "penciled", + "pencilling": "penciling", + "personalise": "personalize", + "personalised": "personalized", + "personalises": "personalizes", + "personalising": "personalizing", + "pharmacopoeia": "pharmacopeia", + "pharmacopoeias": "pharmacopeias", + "philosophise": "philosophize", + "philosophised": "philosophized", + "philosophises": "philosophizes", + "philosophising": "philosophizing", + "philtre": "filter", + "philtres": "filters", + "phoney": "phony", + "plagiarise": "plagiarize", + "plagiarised": "plagiarized", + "plagiarises": "plagiarizes", + "plagiarising": "plagiarizing", + "plough": "plow", + "ploughed": "plowed", + "ploughing": "plowing", + "ploughman": "plowman", + "ploughmen": "plowmen", + "ploughs": "plows", + "ploughshare": "plowshare", + "ploughshares": "plowshares", + "polarisation": "polarization", + "polarise": "polarize", + "polarised": "polarized", + "polarises": "polarizes", + "polarising": "polarizing", + "politicisation": "politicization", + "politicise": "politicize", + "politicised": "politicized", + "politicises": "politicizes", + "politicising": "politicizing", + "popularisation": "popularization", + "popularise": "popularize", + "popularised": "popularized", + "popularises": "popularizes", + "popularising": "popularizing", + "pouffe": "pouf", + "pouffes": "poufs", + "practise": "practice", + "practised": "practiced", + "practises": "practices", + "practising": "practicing", + "praesidium": "presidium", + "praesidiums": "presidiums", + "pressurisation": "pressurization", + "pressurise": "pressurize", + "pressurised": "pressurized", + "pressurises": "pressurizes", + "pressurising": "pressurizing", + "pretence": "pretense", + "pretences": "pretenses", + "primaeval": "primeval", + "prioritisation": "prioritization", + "prioritise": "prioritize", + "prioritised": "prioritized", + "prioritises": "prioritizes", + "prioritising": "prioritizing", + "privatisation": "privatization", + "privatisations": "privatizations", + "privatise": "privatize", + "privatised": "privatized", + "privatises": "privatizes", + "privatising": "privatizing", + "professionalisation": "professionalization", + "professionalise": "professionalize", + "professionalised": "professionalized", + "professionalises": "professionalizes", + "professionalising": "professionalizing", + "programme": "program", + "programmes": "programs", + "prologue": "prolog", + "prologues": "prologs", + "propagandise": "propagandize", + "propagandised": "propagandized", + "propagandises": "propagandizes", + "propagandising": "propagandizing", + "proselytise": "proselytize", + "proselytised": "proselytized", + "proselytiser": "proselytizer", + "proselytisers": "proselytizers", + "proselytises": "proselytizes", + "proselytising": "proselytizing", + "psychoanalyse": "psychoanalyze", + "psychoanalysed": "psychoanalyzed", + "psychoanalyses": "psychoanalyzes", + "psychoanalysing": "psychoanalyzing", + "publicise": "publicize", + "publicised": "publicized", + "publicises": "publicizes", + "publicising": "publicizing", + "pulverisation": "pulverization", + "pulverise": "pulverize", + "pulverised": "pulverized", + "pulverises": "pulverizes", + "pulverising": "pulverizing", + "pummelled": "pummel", + "pummelling": "pummeled", + "pyjama": "pajama", + "pyjamas": "pajamas", + "pzazz": "pizzazz", + "quarrelled": "quarreled", + "quarrelling": "quarreling", + "radicalise": "radicalize", + "radicalised": "radicalized", + "radicalises": "radicalizes", + "radicalising": "radicalizing", + "rancour": "rancor", + "randomise": "randomize", + "randomised": "randomized", + "randomises": "randomizes", + "randomising": "randomizing", + "rationalisation": "rationalization", + "rationalisations": "rationalizations", + "rationalise": "rationalize", + "rationalised": "rationalized", + "rationalises": "rationalizes", + "rationalising": "rationalizing", + "ravelled": "raveled", + "ravelling": "raveling", + "realisable": "realizable", + "realisation": "realization", + "realisations": "realizations", + "realise": "realize", + "realised": "realized", + "realises": "realizes", + "realising": "realizing", + "recognisable": "recognizable", + "recognisably": "recognizably", + "recognisance": "recognizance", + "recognise": "recognize", + "recognised": "recognized", + "recognises": "recognizes", + "recognising": "recognizing", + "reconnoitre": "reconnoiter", + "reconnoitred": "reconnoitered", + "reconnoitres": "reconnoiters", + "reconnoitring": "reconnoitering", + "refuelled": "refueled", + "refuelling": "refueling", + "regularisation": "regularization", + "regularise": "regularize", + "regularised": "regularized", + "regularises": "regularizes", + "regularising": "regularizing", + "remodelled": "remodeled", + "remodelling": "remodeling", + "remould": "remold", + "remoulded": "remolded", + "remoulding": "remolding", + "remoulds": "remolds", + "reorganisation": "reorganization", + "reorganisations": "reorganizations", + "reorganise": "reorganize", + "reorganised": "reorganized", + "reorganises": "reorganizes", + "reorganising": "reorganizing", + "revelled": "reveled", + "reveller": "reveler", + "revellers": "revelers", + "revelling": "reveling", + "revitalise": "revitalize", + "revitalised": "revitalized", + "revitalises": "revitalizes", + "revitalising": "revitalizing", + "revolutionise": "revolutionize", + "revolutionised": "revolutionized", + "revolutionises": "revolutionizes", + "revolutionising": "revolutionizing", + "rhapsodise": "rhapsodize", + "rhapsodised": "rhapsodized", + "rhapsodises": "rhapsodizes", + "rhapsodising": "rhapsodizing", + "rigour": "rigor", + "rigours": "rigors", + "ritualised": "ritualized", + "rivalled": "rivaled", + "rivalling": "rivaling", + "romanticise": "romanticize", + "romanticised": "romanticized", + "romanticises": "romanticizes", + "romanticising": "romanticizing", + "rumour": "rumor", + "rumoured": "rumored", + "rumours": "rumors", + "sabre": "saber", + "sabres": "sabers", + "saltpetre": "saltpeter", + "sanitise": "sanitize", + "sanitised": "sanitized", + "sanitises": "sanitizes", + "sanitising": "sanitizing", + "satirise": "satirize", + "satirised": "satirized", + "satirises": "satirizes", + "satirising": "satirizing", + "saviour": "savior", + "saviours": "saviors", + "savour": "savor", + "savoured": "savored", + "savouries": "savories", + "savouring": "savoring", + "savours": "savors", + "savoury": "savory", + "scandalise": "scandalize", + "scandalised": "scandalized", + "scandalises": "scandalizes", + "scandalising": "scandalizing", + "sceptic": "skeptic", + "sceptical": "skeptical", + "sceptically": "skeptically", + "scepticism": "skepticism", + "sceptics": "skeptics", + "sceptre": "scepter", + "sceptres": "scepters", + "scrutinise": "scrutinize", + "scrutinised": "scrutinized", + "scrutinises": "scrutinizes", + "scrutinising": "scrutinizing", + "secularisation": "secularization", + "secularise": "secularize", + "secularised": "secularized", + "secularises": "secularizes", + "secularising": "secularizing", + "sensationalise": "sensationalize", + "sensationalised": "sensationalized", + "sensationalises": "sensationalizes", + "sensationalising": "sensationalizing", + "sensitise": "sensitize", + "sensitised": "sensitized", + "sensitises": "sensitizes", + "sensitising": "sensitizing", + "sentimentalise": "sentimentalize", + "sentimentalised": "sentimentalized", + "sentimentalises": "sentimentalizes", + "sentimentalising": "sentimentalizing", + "sepulchre": "sepulcher", + "sepulchres": "sepulchers", + "serialisation": "serialization", + "serialisations": "serializations", + "serialise": "serialize", + "serialised": "serialized", + "serialises": "serializes", + "serialising": "serializing", + "sermonise": "sermonize", + "sermonised": "sermonized", + "sermonises": "sermonizes", + "sermonising": "sermonizing", + "sheikh": "sheik", + "shovelled": "shoveled", + "shovelling": "shoveling", + "shrivelled": "shriveled", + "shrivelling": "shriveling", + "signalise": "signalize", + "signalised": "signalized", + "signalises": "signalizes", + "signalising": "signalizing", + "signalled": "signaled", + "signalling": "signaling", + "smoulder": "smolder", + "smouldered": "smoldered", + "smouldering": "smoldering", + "smoulders": "smolders", + "snivelled": "sniveled", + "snivelling": "sniveling", + "snorkelled": "snorkeled", + "snorkelling": "snorkeling", + "snowplough": "snowplow", + "snowploughs": "snowplow", + "socialisation": "socialization", + "socialise": "socialize", + "socialised": "socialized", + "socialises": "socializes", + "socialising": "socializing", + "sodomise": "sodomize", + "sodomised": "sodomized", + "sodomises": "sodomizes", + "sodomising": "sodomizing", + "solemnise": "solemnize", + "solemnised": "solemnized", + "solemnises": "solemnizes", + "solemnising": "solemnizing", + "sombre": "somber", + "specialisation": "specialization", + "specialisations": "specializations", + "specialise": "specialize", + "specialised": "specialized", + "specialises": "specializes", + "specialising": "specializing", + "spectre": "specter", + "spectres": "specters", + "spiralled": "spiraled", + "spiralling": "spiraling", + "splendour": "splendor", + "splendours": "splendors", + "squirrelled": "squirreled", + "squirrelling": "squirreling", + "stabilisation": "stabilization", + "stabilise": "stabilize", + "stabilised": "stabilized", + "stabiliser": "stabilizer", + "stabilisers": "stabilizers", + "stabilises": "stabilizes", + "stabilising": "stabilizing", + "standardisation": "standardization", + "standardise": "standardize", + "standardised": "standardized", + "standardises": "standardizes", + "standardising": "standardizing", + "stencilled": "stenciled", + "stencilling": "stenciling", + "sterilisation": "sterilization", + "sterilisations": "sterilizations", + "sterilise": "sterilize", + "sterilised": "sterilized", + "steriliser": "sterilizer", + "sterilisers": "sterilizers", + "sterilises": "sterilizes", + "sterilising": "sterilizing", + "stigmatisation": "stigmatization", + "stigmatise": "stigmatize", + "stigmatised": "stigmatized", + "stigmatises": "stigmatizes", + "stigmatising": "stigmatizing", + "storey": "story", + "storeys": "stories", + "subsidisation": "subsidization", + "subsidise": "subsidize", + "subsidised": "subsidized", + "subsidiser": "subsidizer", + "subsidisers": "subsidizers", + "subsidises": "subsidizes", + "subsidising": "subsidizing", + "succour": "succor", + "succoured": "succored", + "succouring": "succoring", + "succours": "succors", + "sulphate": "sulfate", + "sulphates": "sulfates", + "sulphide": "sulfide", + "sulphides": "sulfides", + "sulphur": "sulfur", + "sulphurous": "sulfurous", + "summarise": "summarize", + "summarised": "summarized", + "summarises": "summarizes", + "summarising": "summarizing", + "swivelled": "swiveled", + "swivelling": "swiveling", + "symbolise": "symbolize", + "symbolised": "symbolized", + "symbolises": "symbolizes", + "symbolising": "symbolizing", + "sympathise": "sympathize", + "sympathised": "sympathized", + "sympathiser": "sympathizer", + "sympathisers": "sympathizers", + "sympathises": "sympathizes", + "sympathising": "sympathizing", + "synchronisation": "synchronization", + "synchronise": "synchronize", + "synchronised": "synchronized", + "synchronises": "synchronizes", + "synchronising": "synchronizing", + "synthesise": "synthesize", + "synthesised": "synthesized", + "synthesiser": "synthesizer", + "synthesisers": "synthesizers", + "synthesises": "synthesizes", + "synthesising": "synthesizing", + "syphon": "siphon", + "syphoned": "siphoned", + "syphoning": "siphoning", + "syphons": "siphons", + "systematisation": "systematization", + "systematise": "systematize", + "systematised": "systematized", + "systematises": "systematizes", + "systematising": "systematizing", + "tantalise": "tantalize", + "tantalised": "tantalized", + "tantalises": "tantalizes", + "tantalising": "tantalizing", + "tantalisingly": "tantalizingly", + "tasselled": "tasseled", + "technicolour": "technicolor", + "temporise": "temporize", + "temporised": "temporized", + "temporises": "temporizes", + "temporising": "temporizing", + "tenderise": "tenderize", + "tenderised": "tenderized", + "tenderises": "tenderizes", + "tenderising": "tenderizing", + "terrorise": "terrorize", + "terrorised": "terrorized", + "terrorises": "terrorizes", + "terrorising": "terrorizing", + "theatre": "theater", + "theatregoer": "theatergoer", + "theatregoers": "theatergoers", + "theatres": "theaters", + "theorise": "theorize", + "theorised": "theorized", + "theorises": "theorizes", + "theorising": "theorizing", + "tonne": "ton", + "tonnes": "tons", + "towelled": "toweled", + "towelling": "toweling", + "toxaemia": "toxemia", + "tranquillise": "tranquilize", + "tranquillised": "tranquilized", + "tranquilliser": "tranquilizer", + "tranquillisers": "tranquilizers", + "tranquillises": "tranquilizes", + "tranquillising": "tranquilizing", + "tranquillity": "tranquility", + "tranquillize": "tranquilize", + "tranquillized": "tranquilized", + "tranquillizer": "tranquilizer", + "tranquillizers": "tranquilizers", + "tranquillizes": "tranquilizes", + "tranquillizing": "tranquilizing", + "tranquilly": "tranquility", + "transistorised": "transistorized", + "traumatise": "traumatize", + "traumatised": "traumatized", + "traumatises": "traumatizes", + "traumatising": "traumatizing", + "travelled": "traveled", + "traveller": "traveler", + "travellers": "travelers", + "travelling": "traveling", + "travelog": "travelogue", + "travelogs": "travelogues", + "trialled": "trialed", + "trialling": "trialing", + "tricolour": "tricolor", + "tricolours": "tricolors", + "trivialise": "trivialize", + "trivialised": "trivialized", + "trivialises": "trivializes", + "trivialising": "trivializing", + "tumour": "tumor", + "tumours": "tumors", + "tunnelled": "tunneled", + "tunnelling": "tunneling", + "tyrannise": "tyrannize", + "tyrannised": "tyrannized", + "tyrannises": "tyrannizes", + "tyrannising": "tyrannizing", + "tyre": "tire", + "tyres": "tires", + "unauthorised": "unauthorized", + "uncivilised": "uncivilized", + "underutilised": "underutilized", + "unequalled": "unequaled", + "unfavourable": "unfavorable", + "unfavourably": "unfavorably", + "unionisation": "unionization", + "unionise": "unionize", + "unionised": "unionized", + "unionises": "unionizes", + "unionising": "unionizing", + "unorganised": "unorganized", + "unravelled": "unraveled", + "unravelling": "unraveling", + "unrecognisable": "unrecognizable", + "unrecognised": "unrecognized", + "unrivalled": "unrivaled", + "unsavoury": "unsavory", + "untrammelled": "untrammeled", + "urbanisation": "urbanization", + "urbanise": "urbanize", + "urbanised": "urbanized", + "urbanises": "urbanizes", + "urbanising": "urbanizing", + "utilisable": "utilizable", + "utilisation": "utilization", + "utilise": "utilize", + "utilised": "utilized", + "utilises": "utilizes", + "utilising": "utilizing", + "valour": "valor", + "vandalise": "vandalize", + "vandalised": "vandalized", + "vandalises": "vandalizes", + "vandalising": "vandalizing", + "vaporisation": "vaporization", + "vaporise": "vaporize", + "vaporised": "vaporized", + "vaporises": "vaporizes", + "vaporising": "vaporizing", + "vapour": "vapor", + "vapours": "vapors", + "verbalise": "verbalize", + "verbalised": "verbalized", + "verbalises": "verbalizes", + "verbalising": "verbalizing", + "victimisation": "victimization", + "victimise": "victimize", + "victimised": "victimized", + "victimises": "victimizes", + "victimising": "victimizing", + "videodisc": "videodisk", + "videodiscs": "videodisks", + "vigour": "vigor", + "visualisation": "visualization", + "visualisations": "visualizations", + "visualise": "visualize", + "visualised": "visualized", + "visualises": "visualizes", + "visualising": "visualizing", + "vocalisation": "vocalization", + "vocalisations": "vocalizations", + "vocalise": "vocalize", + "vocalised": "vocalized", + "vocalises": "vocalizes", + "vocalising": "vocalizing", + "vulcanised": "vulcanized", + "vulgarisation": "vulgarization", + "vulgarise": "vulgarize", + "vulgarised": "vulgarized", + "vulgarises": "vulgarizes", + "vulgarising": "vulgarizing", + "waggon": "wagon", + "waggons": "wagons", + "watercolour": "watercolor", + "watercolours": "watercolors", + "weaselled": "weaseled", + "weaselling": "weaseling", + "westernisation": "westernization", + "westernise": "westernize", + "westernised": "westernized", + "westernises": "westernizes", + "westernising": "westernizing", + "womanise": "womanize", + "womanised": "womanized", + "womaniser": "womanizer", + "womanisers": "womanizers", + "womanises": "womanizes", + "womanising": "womanizing", + "woollen": "woolen", + "woollens": "woolens", + "woollies": "woolies", + "woolly": "wooly", + "worshipped": "worshiped", + "worshipping": "worshiping", + "worshipper": "worshiper", + "yodelled": "yodeled", + "yodelling": "yodeling", + "yoghourt": "yogurt", + "yoghourts": "yogurts", + "yoghurt": "yogurt", + "yoghurts": "yogurts", + "mhm": "hmm", + "mm": "hmm", + "mmm": "hmm" +} \ No newline at end of file diff --git a/examples/industrial_data_pretraining/minmo/utils/parse_options.sh b/examples/industrial_data_pretraining/minmo/utils/parse_options.sh new file mode 100755 index 000000000..71fb9e5ea --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/parse_options.sh @@ -0,0 +1,97 @@ +#!/usr/bin/env bash + +# Copyright 2012 Johns Hopkins University (Author: Daniel Povey); +# Arnab Ghoshal, Karel Vesely + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +# WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +# MERCHANTABLITY OR NON-INFRINGEMENT. +# See the Apache 2 License for the specific language governing permissions and +# limitations under the License. + + +# Parse command-line options. +# To be sourced by another script (as in ". parse_options.sh"). +# Option format is: --option-name arg +# and shell variable "option_name" gets set to value "arg." +# The exception is --help, which takes no arguments, but prints the +# $help_message variable (if defined). + + +### +### The --config file options have lower priority to command line +### options, so we need to import them first... +### + +# Now import all the configs specified by command-line, in left-to-right order +for ((argpos=1; argpos<$#; argpos++)); do + if [ "${!argpos}" == "--config" ]; then + argpos_plus1=$((argpos+1)) + config=${!argpos_plus1} + [ ! -r $config ] && echo "$0: missing config '$config'" && exit 1 + . $config # source the config file. + fi +done + + +### +### Now we process the command line options +### +while true; do + [ -z "${1:-}" ] && break; # break if there are no arguments + case "$1" in + # If the enclosing script is called with --help option, print the help + # message and exit. Scripts should put help messages in $help_message + --help|-h) if [ -z "$help_message" ]; then echo "No help found." 1>&2; + else printf "$help_message\n" 1>&2 ; fi; + exit 0 ;; + --*=*) echo "$0: options to scripts must be of the form --name value, got '$1'" + exit 1 ;; + # If the first command-line argument begins with "--" (e.g. --foo-bar), + # then work out the variable name as $name, which will equal "foo_bar". + --*) name=`echo "$1" | sed s/^--// | sed s/-/_/g`; + # Next we test whether the variable in question is undefned-- if so it's + # an invalid option and we die. Note: $0 evaluates to the name of the + # enclosing script. + # The test [ -z ${foo_bar+xxx} ] will return true if the variable foo_bar + # is undefined. We then have to wrap this test inside "eval" because + # foo_bar is itself inside a variable ($name). + eval '[ -z "${'$name'+xxx}" ]' && echo "$0: invalid option $1" 1>&2 && exit 1; + + oldval="`eval echo \\$$name`"; + # Work out whether we seem to be expecting a Boolean argument. + if [ "$oldval" == "true" ] || [ "$oldval" == "false" ]; then + was_bool=true; + else + was_bool=false; + fi + + # Set the variable to the right value-- the escaped quotes make it work if + # the option had spaces, like --cmd "queue.pl -sync y" + eval $name=\"$2\"; + + # Check that Boolean-valued arguments are really Boolean. + if $was_bool && [[ "$2" != "true" && "$2" != "false" ]]; then + echo "$0: expected \"true\" or \"false\": $1 $2" 1>&2 + exit 1; + fi + shift 2; + ;; + *) break; + esac +done + + +# Check for an empty argument to the --cmd option, which can easily occur as a +# result of scripting errors. +[ ! -z "${cmd+xxx}" ] && [ -z "$cmd" ] && echo "$0: empty argument to --cmd option" 1>&2 && exit 1; + + +true; # so this script returns exit code 0. diff --git a/examples/industrial_data_pretraining/minmo/utils/res4char.py b/examples/industrial_data_pretraining/minmo/utils/res4char.py new file mode 100644 index 000000000..42a5cccbb --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/res4char.py @@ -0,0 +1,92 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/python +# Author: weijuan +import sys, re + + +def scoreformat(name, line): + newline = "" + for i in range(0, len(line)): + curr = line[i] + currEn = False + if curr == "": + continue + if curr.upper() >= "A" and curr.upper() <= "Z" or curr == "'": + currEn = True + if i == 0: + newline = newline + curr.lower() + else: + if lastEn == True and currEn == True: + newline = newline + curr.lower() + else: + newline = newline + " " + curr.lower() + lastEn = currEn + ret = re.sub("[ ]{1,}", " ", newline) + ret = ret + ret = name + "\t" + ret + return ret + + +def recoformat(line): + newline = "" + en_flag = 0 # 0: no-english 1 : english 2: former + for i in range(0, len(line)): + word = line[i] + if ord(word) == 32: + if en_flag == 0: + continue + else: + en_flag = 0 + newline += " " + if (word >= "\u4e00" and word <= "\u9fa5") or (word >= "\u0030" and word <= "\u0039"): + if en_flag == 1: + newline += " " + word + else: + newline += word + en_flag = 0 + elif ( + (word >= "\u0041" and word <= "\u005a") + or (word >= "\u0061" and word <= "\u007a") + or word == "'" + ): + if en_flag == 0: + newline += " " + word + else: + newline += word + en_flag = 1 + else: + newline += " " + newline = newline + newline = re.sub("[ ]{1,}", " ", newline) + newline = newline.strip() + newline = newline + return newline + + +if __name__ == "__main__": + if len(sys.argv[1:]) < 1: + sys.stderr.write("Usage:\n .py reco.result\n") + sys.stderr.write(" reco.result: idrecoresult; delimiter: \\t , blank \n") + sys.exit(1) + f = open(sys.argv[1]) + for line in f.readlines(): + if not line: + continue + line = line.rstrip() + if "\t" in line: + tmp = line.split("\t") + elif "," in line: + tmp = line.split(",", 1) + else: + tmp = line.split(" ", 1) + if len(tmp) < 2: + continue + name = tmp[0] + content = tmp[1] + name = re.sub("\.pcm$", "", name) + name = re.sub("\.wav$", "", name) + # name=re.sub("wav[0-9]{3,}_[0-9]{4,}_","",name) + content = recoformat(content) + content = scoreformat(name, content) + print(content) + f.close() diff --git a/examples/industrial_data_pretraining/minmo/utils/score.py b/examples/industrial_data_pretraining/minmo/utils/score.py new file mode 100644 index 000000000..a6c673bec --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/score.py @@ -0,0 +1,125 @@ +import sys +from sklearn.metrics import classification_report + + +aed_ref = sys.argv[1] +aed_hyp = sys.argv[2] +select_emo = sys.argv[3] +# select_emo = "happy,sad,angry,neutral" #参与打分的情感 +emo_list = select_emo.split(",") + +ref, hyp = {}, {} +all_key = set() +mix_map = {} + +with open(aed_ref, "r") as f: + for line in f: + id, event = line.strip().split(" ", 1)[0], line.strip().split(" ", 1)[1] + ref[id] = event + +with open(aed_hyp, "r") as f: + for line in f: + if len(line.strip().split(" ", 1)) != 2: + continue + id, event = line.strip().split(" ", 1) + hyp[id] = event + + +ref_list = [] +hyp_list = [] + + +emo_dict = {} + + +def get_emo(s): + if "Happy" in s or "开心" in s: + return "happy" + if "Sad" in s or "难过" in s: + return "sad" + if "Angry" in s or "生气" in s: + return "angry" + if "Neutral" in s or "平静" in s: + return "neutral" + if "Fearful" in s or "害怕" in s: + return "fearful" + if "Surprised" in s or "吃惊" in s: + return "surprised" + if "Disgusted" in s or "厌恶" in s: + return "disgusted" + return "other" + + +for key in hyp: + if key not in ref: + continue + + ref_emo = get_emo(ref[key]) + hyp_emo = get_emo(hyp[key]) + + print(key, ref_emo, hyp_emo) + + if get_emo(ref[key]) not in emo_list or get_emo(hyp[key]) not in select_emo: + continue + + ref_list.append(get_emo(ref[key])) + hyp_list.append(get_emo(hyp[key])) + + if ref_emo not in emo_dict: + emo_dict[ref_emo] = {} + if hyp_emo not in emo_dict[ref_emo]: + emo_dict[ref_emo][hyp_emo] = 0 + emo_dict[ref_emo][hyp_emo] += 1 + + +head_line = "*" * 10 +hyp_emo_set = set(hyp_list) + +for hyp_emo in hyp_emo_set: + head_line += f"\t{hyp_emo:10}" +print(head_line) +for ref_emo in emo_list: + if ref_emo not in emo_dict: + continue + show_str = [f"{ref_emo:10}"] + for hyp_emo in hyp_emo_set: + hyp_num = f"{emo_dict[ref_emo].get(hyp_emo, 0)}" + show_str.append(f"\t{hyp_num:10}") + print("".join(show_str)) + +if len(ref_list) > 0: + print(classification_report(ref_list, hyp_list, digits=3)) + +# 使用方法: +# >>> python3 score.py path/to/ref path/to/hyp happy,sad,angry,neutral + +# # ref和hyp格式与wav.scp相似: wav_id emotion +# # wav_1 happy +# # wav_2 sad + +# 结果示例, +# ********** angry disgusted fearful happy neutral sad surprised +# angry 138 3 1 54 88 20 15 +# disgusted 25 2 1 16 16 4 3 +# fearful 12 0 1 12 16 6 2 +# happy 48 1 0 208 79 18 8 +# neutral 147 2 12 298 590 80 17 +# sad 41 1 1 32 52 61 9 +# surprised 53 1 2 54 85 8 44 +# happy: 208/353 recall: 0.589235 acc: 0.351351 +# sad: 61/186 recall: 0.327957 acc: 0.340782 +# angry: 138/300 recall: 0.460000 acc: 0.368984 +# neutral: 590/1115 recall: 0.529148 acc: 0.729295 +# UA:0.476585, WA: 0.510235 + +# ========== +# precision recall f1-score support + +# angry 0.369 0.460 0.409 300 +# happy 0.351 0.589 0.440 353 +# neutral 0.729 0.529 0.613 1115 +# sad 0.341 0.328 0.334 186 + +# accuracy 0.510 1954 +# macro avg 0.448 0.477 0.449 1954 <--------以这两行为准 +# weighted avg 0.569 0.510 0.524 1954 <--------以这两行为准 diff --git a/examples/industrial_data_pretraining/minmo/utils/split_merge_res.py b/examples/industrial_data_pretraining/minmo/utils/split_merge_res.py new file mode 100644 index 000000000..83573d8a0 --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/split_merge_res.py @@ -0,0 +1,34 @@ +import sys + + +input_file = sys.argv[1] +key_file = sys.argv[2] +output_file = sys.argv[3] + + +key_dct = {} +with open(key_file, "r") as f: + for line in f: + content = line.strip().split(" ", 1) + if len(content) == 2: + key, trans = content[0], content[1] + else: + key = content[0] + trans = "" + key_dct[key] = trans + +fout = open(output_file, "w") + +repeat_lst = [] + +with open(input_file, "r") as f: + for line in f: + content = line.strip().split(" ", 1) + if len(content) == 2: + key, trans = content[0], content[1] + else: + key = content[0] + trans = "" + if key in key_dct and key not in repeat_lst: + repeat_lst.append(key) + fout.writelines(key + " " + trans + "\n") diff --git a/examples/industrial_data_pretraining/minmo/utils/text_normalize/add_space_for_zh.py b/examples/industrial_data_pretraining/minmo/utils/text_normalize/add_space_for_zh.py new file mode 100644 index 000000000..5b99fee32 --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/text_normalize/add_space_for_zh.py @@ -0,0 +1,18 @@ +import sys + +input_file = sys.argv[1] +output_file = sys.argv[2] + +with open(input_file) as f: + lines = f.readlines() + +with open(output_file, "w") as wf: + for line in lines: + parts = line.strip().split(maxsplit=1) + if len(parts) == 2: + key, text = parts + else: + key, parts = parts[0], " " + text = [t for t in text.replace(" ", "")] + text = " ".join(text) + wf.write(key + " " + text + "\n") diff --git a/examples/industrial_data_pretraining/minmo/utils/text_normalize/whisper_basic_normalize.py b/examples/industrial_data_pretraining/minmo/utils/text_normalize/whisper_basic_normalize.py new file mode 100644 index 000000000..f1782755c --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/text_normalize/whisper_basic_normalize.py @@ -0,0 +1,24 @@ +import sys +from whisper_normalizer.basic import BasicTextNormalizer + +basic_normalizer = BasicTextNormalizer() + + +def normalize_text(srcfn, dstfn): + with open(srcfn, "r") as f_read, open(dstfn, "w") as f_write: + all_lines = f_read.readlines() + for line in all_lines: + line = line.strip() + line_arr = line.split() + if len(line_arr) < 2: + continue + key = line_arr[0] + conts = " ".join(line_arr[1:]) + normalized_conts = basic_normalizer(conts) + f_write.write("{0}\t{1}\n".format(key, normalized_conts)) + + +if __name__ == "__main__": + srcfn = sys.argv[1] + dstfn = sys.argv[2] + normalize_text(srcfn, dstfn) diff --git a/examples/industrial_data_pretraining/minmo/utils/text_normalize/whisper_english_normalize.py b/examples/industrial_data_pretraining/minmo/utils/text_normalize/whisper_english_normalize.py new file mode 100644 index 000000000..cb2650bba --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/text_normalize/whisper_english_normalize.py @@ -0,0 +1,22 @@ +import sys +from whisper_normalizer.english import EnglishTextNormalizer + +english_normalizer = EnglishTextNormalizer() + + +def normalize_text(srcfn, dstfn): + with open(srcfn, "r") as f_read, open(dstfn, "w") as f_write: + all_lines = f_read.readlines() + for line in all_lines: + line = line.strip() + line_arr = line.split() + key = line_arr[0] + conts = " ".join(line_arr[1:]) + normalized_conts = english_normalizer(conts) + f_write.write("{0}\t{1}\n".format(key, normalized_conts)) + + +if __name__ == "__main__": + srcfn = sys.argv[1] + dstfn = sys.argv[2] + normalize_text(srcfn, dstfn) diff --git a/examples/industrial_data_pretraining/minmo/utils/text_normalize/zh_hant2zh_cn_process.py b/examples/industrial_data_pretraining/minmo/utils/text_normalize/zh_hant2zh_cn_process.py new file mode 100644 index 000000000..b8005ebe9 --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/text_normalize/zh_hant2zh_cn_process.py @@ -0,0 +1,32 @@ +import zhconv +import argparse +import codecs + + +def convert_hant2cn(input_file, output_file): + fout = codecs.open(output_file, "w") + with codecs.open(input_file, "r") as fin: + for line in fin: + if "\t" in line: + content = line.strip().split("\t", 1) + else: + content = line.strip().split(" ", 1) + if len(content) == 2: + idx, res = content[0], content[1] + else: + idx = content[0] + res = "" + convert_res = zhconv.convert(res, "zh-cn") + # print(idx, res, convert_res) + fout.writelines(idx + "\t" + convert_res + "\n") + + fout.close() + + +parser = argparse.ArgumentParser(description="manual to this script") +parser.add_argument("--input_file", type=str, default=None) +parser.add_argument("--output_file", type=str, default=None) +args = parser.parse_args() + +if __name__ == "__main__": + convert_hant2cn(args.input_file, args.output_file) diff --git a/examples/industrial_data_pretraining/minmo/utils/whisper_english_normalize.py b/examples/industrial_data_pretraining/minmo/utils/whisper_english_normalize.py new file mode 100644 index 000000000..fc18b32bc --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/whisper_english_normalize.py @@ -0,0 +1,25 @@ +import sys +import os +import re +import string +from whisper_normalizer.english import EnglishTextNormalizer + +english_normalizer = EnglishTextNormalizer() + + +def normalize_text(srcfn, dstfn): + with open(srcfn, "r") as f_read, open(dstfn, "w") as f_write: + all_lines = f_read.readlines() + for line in all_lines: + line = line.strip() + line_arr = line.split() + key = line_arr[0] + conts = " ".join(line_arr[1:]) + normalized_conts = english_normalizer(conts) + f_write.write("{0}\t{1}\n".format(key, normalized_conts)) + + +if __name__ == "__main__": + srcfn = sys.argv[1] + dstfn = sys.argv[2] + normalize_text(srcfn, dstfn) diff --git a/examples/industrial_data_pretraining/minmo/utils/zh_hant2zh_cn_process.py b/examples/industrial_data_pretraining/minmo/utils/zh_hant2zh_cn_process.py new file mode 100644 index 000000000..57abeb02b --- /dev/null +++ b/examples/industrial_data_pretraining/minmo/utils/zh_hant2zh_cn_process.py @@ -0,0 +1,28 @@ +import zhconv +import argparse +import codecs + + +def convert_hant2cn(input_file, output_file): + fout = codecs.open(output_file, "w") + with codecs.open(input_file, "r") as fin: + for line in fin: + content = line.strip().split(" ", 1) + if len(content) == 2: + idx, res = content[0], content[1] + else: + idx = content[0] + res = "" + convert_res = zhconv.convert(res, "zh-cn") + fout.writelines(idx + "\t" + convert_res + "\n") + + fout.close() + + +parser = argparse.ArgumentParser(description="manual to this script") +parser.add_argument("--input_file", type=str, default=None) +parser.add_argument("--output_file", type=str, default=None) +args = parser.parse_args() + +if __name__ == "__main__": + convert_hant2cn(args.input_file, args.output_file) diff --git a/examples/industrial_data_pretraining/sense_voice/demo.py b/examples/industrial_data_pretraining/sense_voice/demo.py index 530399955..786a3df36 100644 --- a/examples/industrial_data_pretraining/sense_voice/demo.py +++ b/examples/industrial_data_pretraining/sense_voice/demo.py @@ -9,6 +9,7 @@ model = AutoModel( model="/Users/zhifu/Downloads/modelscope_models/SenseVoiceModelscope", # vad_model="iic/speech_fsmn_vad_zh-cn-16k-common-pytorch", # vad_kwargs={"max_single_segment_time": 30000}, + device="cuda:0", ) diff --git a/examples/industrial_data_pretraining/sense_voice/demo_ctc.py b/examples/industrial_data_pretraining/sense_voice/demo_ctc.py index a8ba7f941..cc876e354 100644 --- a/examples/industrial_data_pretraining/sense_voice/demo_ctc.py +++ b/examples/industrial_data_pretraining/sense_voice/demo_ctc.py @@ -13,6 +13,7 @@ input_file = ( model = AutoModel( model=model_dir, + device="cuda:0", ) res = model.generate( diff --git a/examples/industrial_data_pretraining/sense_voice/demo_fsmn.py b/examples/industrial_data_pretraining/sense_voice/demo_fsmn.py index ce4bdf8a2..c6abde869 100644 --- a/examples/industrial_data_pretraining/sense_voice/demo_fsmn.py +++ b/examples/industrial_data_pretraining/sense_voice/demo_fsmn.py @@ -9,6 +9,7 @@ model = AutoModel( model="/Users/zhifu/Downloads/modelscope_models/SenseVoiceModelscopeFSMN", vad_model="iic/speech_fsmn_vad_zh-cn-16k-common-pytorch", vad_kwargs={"max_single_segment_time": 30000}, + device="cuda:0", ) diff --git a/funasr/models/llm_asr/model.py b/funasr/models/llm_asr/model.py index c452f286b..9563b412e 100644 --- a/funasr/models/llm_asr/model.py +++ b/funasr/models/llm_asr/model.py @@ -860,7 +860,7 @@ class LLMASR3(LLMASR2): return encoder_out, encoder_out_lens -@tables.register("model_classes", "LLMASR4") +# @tables.register("model_classes", "LLMASR4") class LLMASR4(nn.Module): """ """ @@ -1339,7 +1339,7 @@ class LLMASR4(nn.Module): # audio encoder speech = batch["speech"] - + if len(speech) > 0: if "audio_embedding" in kwargs and "audio_embedding_lens" in kwargs: encoder_out = kwargs["audio_embedding"] @@ -2303,6 +2303,7 @@ class LLMASRXvecSlotTTS(nn.Module): vocoder_conf = kwargs.get("vocoder_conf", None) self.vocoder = self.build_vocoder(name=vocoder_name, conf=vocoder_conf).to(torch.float32) import lameenc + self.mp3_encoder = lameenc.Encoder() self.mp3_encoder.set_bit_rate(128) self.mp3_encoder.set_in_sample_rate(22050) @@ -3023,7 +3024,8 @@ class LLMASRXvecSlotTTS(nn.Module): @torch.no_grad() def generate_speech_one_step( self, - text: str, preds: str, + text: str, + preds: str, last_t_size, llm_cur_kv_cache, llm_cur_kv_cache_len, @@ -3051,14 +3053,14 @@ class LLMASRXvecSlotTTS(nn.Module): preds = self.split_characters_and_words(normed_preds[:str_idx]) idx = len(preds) preds.append(normed_preds[str_idx]) - preds.extend(self.split_characters_and_words(normed_preds[str_idx+1:])) + preds.extend(self.split_characters_and_words(normed_preds[str_idx + 1 :])) break _text = f"<|endofprompt|><|sil|>{text+normed_preds}" + ("<|sil|>" if is_last else "") para_end = False if idx > -1 and not is_last: - pre_part = "".join(preds[:idx+1]) - if len(self.tts_tokenizer_warpper(text+pre_part)) >= para_phone_len: + pre_part = "".join(preds[: idx + 1]) + if len(self.tts_tokenizer_warpper(text + pre_part)) >= para_phone_len: _text = f"<|endofprompt|><|sil|>{text+pre_part}<|sil|>" para_end = True @@ -3109,7 +3111,7 @@ class LLMASRXvecSlotTTS(nn.Module): last_t_size = t_size if para_end: - text = "".join(preds[idx + 1:]) + text = "".join(preds[idx + 1 :]) last_t_size = 0 prompt_token, prompt_audio = [None, None], [None, None] wav = torch.cat([wav, torch.zeros([1, 2205]).to(wav)], dim=1) @@ -3121,17 +3123,18 @@ class LLMASRXvecSlotTTS(nn.Module): @torch.no_grad() def simple_generate_speech_one_step( - self, - text: str, preds: str, - last_t_size, - llm_cur_kv_cache, - llm_cur_kv_cache_len, - prompt_token, - prompt_audio, - tts_text_chunk_size, - chunk_idx, - is_last, - para_phone_len=200, + self, + text: str, + preds: str, + last_t_size, + llm_cur_kv_cache, + llm_cur_kv_cache_len, + prompt_token, + prompt_audio, + tts_text_chunk_size, + chunk_idx, + is_last, + para_phone_len=200, ): device = llm_cur_kv_cache.device _text = f"<|endofprompt|><|sil|>{text}" + ("<|sil|>" if is_last else "") @@ -3194,6 +3197,7 @@ class LLMASRXvecSlotTTS(nn.Module): if is_last: mp3_data += self.mp3_encoder.flush() import lameenc + self.mp3_encoder = lameenc.Encoder() self.mp3_encoder.set_bit_rate(128) self.mp3_encoder.set_in_sample_rate(22050) @@ -3227,7 +3231,8 @@ class LLMASRXvecSlotTTS(nn.Module): # new_text = new_text + _resp rt_value, states = self.generate_speech_one_step( - new_text, _resp, + new_text, + _resp, last_t_size, llm_cur_kv_cache, llm_cur_kv_cache_len, @@ -3288,7 +3293,8 @@ class LLMASRXvecSlotTTS(nn.Module): # new_text = new_text + preds with torch.cuda.amp.autocast(enabled=False, dtype=torch.float32): rt_value, states_ret = self.generate_speech_one_step( - new_text, preds, + new_text, + preds, last_t_size, llm_cur_kv_cache, llm_cur_kv_cache_len, @@ -3311,14 +3317,14 @@ class LLMASRXvecSlotTTS(nn.Module): return cur_token, feat, wav def simple_streaming_generate_speech( - self, - preds, - states, - llm_cur_kv_cache, - llm_cur_kv_cache_len, - is_last=False, - text_chunk_size=8, - format="mp3", + self, + preds, + states, + llm_cur_kv_cache, + llm_cur_kv_cache_len, + is_last=False, + text_chunk_size=8, + format="mp3", ): new_text, last_t_size, prompt_token, prompt_audio, chunk_idx = ( @@ -3331,7 +3337,8 @@ class LLMASRXvecSlotTTS(nn.Module): # new_text = new_text + preds with torch.cuda.amp.autocast(enabled=False, dtype=torch.float32): rt_value, states_ret = self.simple_generate_speech_one_step( - preds, "", + preds, + "", last_t_size, llm_cur_kv_cache, llm_cur_kv_cache_len, diff --git a/funasr/models/minmo/model.py b/funasr/models/minmo/model.py index 7c8053956..837e7224d 100644 --- a/funasr/models/minmo/model.py +++ b/funasr/models/minmo/model.py @@ -691,3 +691,637 @@ class LayerNorm(nn.LayerNorm): self.eps, ) return output.type_as(input) + + +@tables.register("model_classes", "LLMASR4") +@tables.register("model_classes", "MinMo_S2T") +class MinMo_S2T(nn.Module): + """ """ + + def __init__( + self, + audio_encoder: str = None, + audio_encoder_conf: dict = None, + audio_adaptor: str = None, + audio_adaptor_conf: dict = None, + llm: str = None, + llm_conf: dict = None, + input_size: int = 80, + length_normalized_loss: bool = False, + **kwargs, + ): + + super().__init__() + + # audio encoder + hub = audio_encoder_conf.get("hub", None) + self.audio_encoder_activation_checkpoint = audio_encoder_conf.get( + "activation_checkpoint", False + ) + if hub == "ms": + from funasr import AutoModel + + model = AutoModel(model=audio_encoder, model_revision="master") + audio_encoder_output_size = ( + model.model.encoder_output_size + if hasattr(model.model, "encoder_output_size") + else -1 + ) + + audio_encoder = ( + model.model.model.encoder if hasattr(model.model, "model") else model.model.encoder + ) + + # self.frontend = frontend + + elif hub == "hf": + pass + else: + encoder_class = tables.encoder_classes.get(audio_encoder) + audio_encoder = encoder_class(input_size=input_size, **audio_encoder_conf) + audio_encoder_output_size = audio_encoder.output_size() + freeze = audio_encoder_conf.get("freeze", True) + freeze_layer_num = int(audio_encoder_conf.get("freeze_layer_num", -1)) + # if freeze_layer_num > 0: + # freeze_layer_num = range(freeze_layer_num) + + if freeze: + for name, param in audio_encoder.named_parameters(): + if freeze_layer_num > 0: + idx = re.search(r"\.\d+\.", name) + if idx is not None: + beg, end = idx.regs[0] + layer_id = int(name[beg + 1 : end - 1]) + if layer_id < freeze_layer_num: + param.requires_grad = False + elif "ln_post." not in name: + param.requires_grad = False + else: + param.requires_grad = False + + audio_encoder.eval() + + self.audio_encoder = audio_encoder + + # llm + self.llm = None + + from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig + + init_param_path = llm_conf.get("init_param_path", "vicuna-7b-v1.5") + llm_load_kwargs = llm_conf.get("load_kwargs", {}) + + if not llm_conf.get("low_cpu", False): + model = AutoModelForCausalLM.from_pretrained( + init_param_path, + load_in_8bit=None, + device_map=None, + use_cache=None, + **llm_load_kwargs, + ) + else: + import os + + if int(os.environ.get("RANK", 0)) == 0: + model = AutoModelForCausalLM.from_pretrained( + init_param_path, + load_in_8bit=None, + device_map="cpu", + use_cache=None, + **llm_load_kwargs, + ) + else: + llm_config = AutoConfig.from_pretrained(init_param_path) + model = AutoModelForCausalLM.from_config(llm_config) + + freeze = llm_conf.get("freeze", True) + if freeze: + for name, param in model.named_parameters(): + param.requires_grad = False + model.eval() + + logging.info(f"use_lora: {llm_conf.get('use_lora', False)}") + if llm_conf.get("use_lora", False): + from omegaconf import OmegaConf, DictConfig + + lora_conf = llm_conf.get("lora_conf", {}) + if isinstance(lora_conf, (OmegaConf, DictConfig)): + lora_conf = OmegaConf.to_container(lora_conf, resolve=True) + from peft import get_peft_model, LoraConfig, TaskType, PeftConfig, PeftModel + + lora_init_param_path = lora_conf.get("init_param_path", None) + if lora_init_param_path is not None: + logging.info(f"lora_init_param_path: {lora_init_param_path}") + model = PeftModel.from_pretrained(model, lora_init_param_path) + for name, param in model.named_parameters(): + if not lora_conf.get("freeze_lora", False): + if "lora_" in name: + param.requires_grad = True + else: + peft_config = LoraConfig(**lora_conf) + model = get_peft_model(model, peft_config) + + model.print_trainable_parameters() + + if llm_conf.get("activation_checkpoint", False): + model.gradient_checkpointing_enable() + + self.llm_dtype = llm_conf.get("llm_dtype", "fp32") + self.llm = model.to(dtype_map[self.llm_dtype]) + llm_dim = model.get_input_embeddings().weight.shape[-1] + + # adaptor + adaptor_class = tables.adaptor_classes.get(audio_adaptor) + if audio_encoder_output_size > 0: + audio_adaptor_conf["encoder_dim"] = audio_encoder_output_size + audio_adaptor_conf["llm_dim"] = llm_dim + audio_adaptor = adaptor_class(**audio_adaptor_conf) + init_param_path = audio_adaptor_conf.get("init_param_path", None) + if init_param_path is not None: + src_state = torch.load(init_param_path, map_location="cpu") + flag = audio_adaptor.load_state_dict(src_state, strict=False) + logging.info(f"Loading audio_adaptor ckpt: {init_param_path}, status: {flag}") + freeze = audio_adaptor_conf.get("freeze", False) + if freeze: + for name, param in audio_adaptor.named_parameters(): + param.requires_grad = False + audio_adaptor.eval() + + self.audio_adaptor = audio_adaptor + + self.error_calculator = None + + self.length_normalized_loss = length_normalized_loss + self.beam_search = None + import os + + rank = int(os.environ.get("RANK", 0)) + logging.info(f"rank: {rank}, model is builded.") + + def forward( + self, + speech: torch.Tensor = None, + speech_lengths: torch.Tensor = None, + input_ids: torch.Tensor = None, + attention_mask: torch.Tensor = None, + labels_ids: torch.Tensor = None, + fbank_beg: torch.Tensor = None, + fbank_mask: torch.Tensor = None, + **kwargs, + ): + """Encoder + Decoder + Calc loss + Args: + speech: (Batch, Length, ...) + speech_lengths: (Batch, ) + text: (Batch, Length) + text_lengths: (Batch,) + """ + # import pdb + # + # pdb.set_trace() + batch_size, token_num = input_ids.shape + stats = {} + input_ids[input_ids < 0] = 0 + inputs_embeds = self.llm.model.get_input_embeddings()(input_ids) + if speech is not None: + if len(speech_lengths.size()) > 1: + speech_lengths = speech_lengths[:, 0] + + batch_size_speech, frames, _ = speech.shape + + # audio encoder + if self.audio_encoder_activation_checkpoint: + from torch.utils.checkpoint import checkpoint + + encoder_out, encoder_out_lens = checkpoint( + self.encode, speech, speech_lengths, use_reentrant=False + ) + else: + encoder_out, encoder_out_lens = self.encode(speech, speech_lengths) + + # audio_adaptor + encoder_out, encoder_out_lens = self.audio_adaptor(encoder_out, encoder_out_lens) + + batch_size, token_num, dims = inputs_embeds.shape + fake_token_len = kwargs.get("fake_token_len") + fake_token_len[fake_token_len < 0] = 0 + fbank_beg[fbank_beg < 0] = 0 + + speech_idx = 0 + for batch_idx in range(batch_size): + + for turn_id in range(fbank_beg.shape[1]): + fbank_beg_idx = fbank_beg[batch_idx, turn_id].item() + if fbank_beg_idx > 0: + speech_token_len = fake_token_len[batch_idx, turn_id] + speech_token = encoder_out[speech_idx, :speech_token_len, :] + + try: + inputs_embeds[ + batch_idx, fbank_beg_idx : fbank_beg_idx + speech_token_len, : + ] = speech_token + except Exception as e: + # + logging.error(f"{str(e)}, {traceback.format_exc()}") + logging.info( + f"batch_idx: {batch_idx}, inputs_embeds: {inputs_embeds.shape}, fbank_beg_idx: {fbank_beg_idx}, speech_token_len: {speech_token_len}, encoder_out: {encoder_out.shape}, encoder_out_lens: {encoder_out_lens}, fake_token_len: {fake_token_len}, speech_lengths: {speech_lengths}" + ) + # import pdb; + # pdb.set_trace() + speech_token_len = encoder_out_lens[speech_idx].item() + speech_token = encoder_out[speech_idx, :speech_token_len, :] + inputs_embeds[ + batch_idx, fbank_beg_idx : fbank_beg_idx + speech_token_len, : + ] = speech_token + + speech_idx += 1 + + stats["batch_size_speech"] = batch_size_speech + stats["batch_size_x_frames"] = frames * batch_size_speech + stats["batch_size_real_frames"] = speech_lengths.sum().item() + stats["padding_frames"] = stats["batch_size_x_frames"] - stats["batch_size_real_frames"] + + with torch.cuda.amp.autocast( + enabled=True if self.llm_dtype != "fp32" else False, dtype=dtype_map[self.llm_dtype] + ): + labels_ids[labels_ids == -1] = -100 + attention_mask[attention_mask < 0] = 0 + model_outputs = self.llm( + inputs_embeds=inputs_embeds.to(dtype_map[self.llm_dtype]), + attention_mask=attention_mask, + labels=labels_ids, + ) + loss = model_outputs.loss + + with torch.no_grad(): + preds = torch.argmax(model_outputs.logits, -1) + acc_att = compute_accuracy(preds[:, :-1], labels_ids[:, 1:], ignore_label=-100) + stats["acc"] = acc_att + + stats["loss"] = torch.clone(loss.detach()) + stats["batch_size"] = batch_size + + stats["batch_size_x_tokens"] = token_num * batch_size + stats["batch_size_real_tokens"] = attention_mask.sum().item() + stats["padding_tokens"] = stats["batch_size_x_tokens"] - stats["batch_size_real_tokens"] + + dialog_turns = (fbank_beg > 0).sum(-1) + dialog_turns_max = torch.max(dialog_turns).int().item() + dialog_turns_avg = dialog_turns.sum().item() / batch_size + stats["dialog_turns_max"] = dialog_turns_max + stats["dialog_turns_avg"] = dialog_turns_avg + + # force_gatherable: to-device and to-tensor if scalar for DataParallel + if self.length_normalized_loss: + batch_size = int((labels_ids > 0 + 1).sum()) + loss, stats, weight = force_gatherable((loss, stats, batch_size), loss.device) + return loss, stats, weight + + def encode(self, speech, speech_lengths): + # audio encoder + encoder_out, encoder_out_lens = self.audio_encoder(speech.permute(0, 2, 1), speech_lengths) + + return encoder_out, encoder_out_lens + + def data_template(self, data): + system, user, assistant = [], [], [] + for i, item in enumerate(data): + role = item["role"] + content = item["content"] + if role == "system": + system.append(content) + elif role == "user": + if "audio" in item: + audio = item["audio"] + content = [content, audio] + user.append(content) + elif role == "assistant": + assistant.append(content) + + system = system * len(user) + + contents = { + "system": system, + "user": user, + "assistant": assistant, + } + + return contents + + def data_load_speech(self, contents: dict, tokenizer, frontend, meta_data={}, **kwargs): + + system = contents["system"] + user = contents["user"] + assistant = contents["assistant"] + pattern = re.compile(r"(<\|startofspeech\|>.*?<\|endofspeech\|>)") + + input_ids, labels, fbank, fbank_lens, fbank_mask, fbank_beg, fake_token_len = ( + [], + [], + [], + [], + [], + [], + [], + ) + input_source_ids = [] + for i, (system_prompt, user_prompt, target_out) in enumerate(zip(system, user, assistant)): + if i >= kwargs.get("multiturn_num_max", 5): + break + if len(input_ids) > kwargs.get("max_token_length", 1500): + break + if isinstance(user_prompt, (list, tuple)): + user_prompt, audio = user_prompt + if i == 0: + if kwargs.get("infer_with_assistant_input", False): + source_input = f"<|im_start|>system\n{system_prompt}<|im_end|>\n<|im_start|>user\n{user_prompt}" + else: + source_input = f"<|im_start|>system\n{system_prompt}<|im_end|>\n<|im_start|>user\n{user_prompt}<|im_end|>\n<|im_start|>assistant\n" + else: + if kwargs.get("infer_with_assistant_input", False): + source_input = f"<|im_start|>user\n{user_prompt}" + else: + source_input = ( + f"<|im_start|>user\n{user_prompt}<|im_end|>\n<|im_start|>assistant\n" + ) + + splits = pattern.split(source_input) + source_ids = [] + fbank_i = [] + fbank_mask_i = [] + fake_token_len_i = 0 + fbank_beg_i = -1 + fbank_lens_i = [] + speech, speech_lengths = [], [] + for k, sub_str in enumerate(splits): + if not sub_str.startswith("<|startofspeech|>"): + sub_token = tokenizer.encode(sub_str) + source_ids += sub_token + fbank_mask_i += [0] * len(sub_token) + else: + sub_str = sub_str.replace("<|startofspeech|>", "").replace( + "<|endofspeech|>", "" + ) + if sub_str.startswith("!"): + sub_str = sub_str[1:] + if sub_str.startswith("!"): # !!: audio sample point + sub_str = audio + try: + time1 = time.perf_counter() + data_src = load_audio_text_image_video(sub_str, fs=frontend.fs) + time2 = time.perf_counter() + meta_data["load_data"] = f"{time2 - time1:0.3f}" + except Exception as e: + logging.error(f"Loading wav failed! {str(e)}, {traceback.format_exc()}") + + speech, speech_lengths = extract_fbank( + data_src, + data_type=kwargs.get("data_type", "sound"), + frontend=frontend, + is_final=True, + ) # speech: [b, T, d] + + time3 = time.perf_counter() + meta_data["extract_feat"] = f"{time3 - time2:0.3f}" + meta_data["batch_data_time"] = ( + speech_lengths.sum().item() + * frontend.frame_shift + * frontend.lfr_n + / 1000 + ) + + if kwargs.get("permute", True): + speech = speech.permute(0, 2, 1) + if speech_lengths > kwargs.get("max_source_length", 5500): + # logging.info( + # f"speech_lengths > max_source_length: {speech_lengths}>{self.max_source_length}, {item}" + # ) + badcase_flag = True + + olens = 1 + (speech_lengths[0].item() - 3 + 2 * 1) // 2 + olens = 1 + (olens - 3 + 2 * 1) // 2 + fake_token_len_i = (olens - 1) // 2 + 1 + fake_token = [0] * fake_token_len_i + fbank_beg_i = len(source_ids) + source_ids += fake_token + fbank_mask_i += [1] * len(fake_token) + + fbank_beg += [fbank_beg_i + len(input_ids)] + fake_token_len += [fake_token_len_i] + source_mask = [-100] * len(source_ids) + target_out = f"{target_out}<|im_end|>" + target_ids = tokenizer.encode(target_out) + input_source_ids = input_ids + source_ids + input_ids += source_ids + target_ids + labels += source_mask + target_ids + fbank_mask += fbank_mask_i + if len(speech) > 0: + fbank.append(speech[0, :, :]) + fbank_lens.append(speech_lengths) + + input_ids = torch.tensor(input_ids, dtype=torch.int64) # [: self.max_token_length] + attention_mask = torch.tensor([1] * len(input_ids), dtype=torch.int32) + labels = torch.tensor(labels, dtype=torch.int64) # [: self.max_token_length] + + # fbank = speech[0, :, :] + # fbank_lens = torch.tensor(fbank_lens, dtype=torch.int32) + fbank_mask = torch.tensor(fbank_mask, dtype=torch.float32) + fbank_beg = torch.tensor(fbank_beg, dtype=torch.int32) + fake_token_len = torch.tensor(fake_token_len, dtype=torch.int32) + source_ids = torch.tensor(input_source_ids, dtype=torch.int64) + target_ids = torch.tensor(target_ids, dtype=torch.int64) + + if len(fbank) > 0: + speech = torch.nn.utils.rnn.pad_sequence(fbank, batch_first=True, padding_value=0.0) + speech_lengths = torch.nn.utils.rnn.pad_sequence( + fbank_lens, batch_first=True, padding_value=-1 + ) + else: + speech = [] + speech_lengths = [] + output = { + "speech": speech, + "speech_lengths": speech_lengths, + "fbank_mask": fbank_mask[None, :], + "fbank_beg": fbank_beg[None,], + "fake_token_len": fake_token_len[None, :], + "input_ids": input_ids[None,], + "attention_mask": attention_mask[None,], + "labels_ids": labels, + "source_ids": source_ids[None, :], + "target_ids": target_ids[None, :], + } + + return output + + def inference_prepare( + self, + data_in, + data_lengths=None, + key: list = None, + tokenizer=None, + frontend=None, + **kwargs, + ): + + meta_data = {} + prompt = kwargs.get("prompt", None) + + if kwargs.get("batch_size", 1) > 1: + raise NotImplementedError("batch decoding is not implemented") + + contents = self.data_template(data_in[0]) + output = self.data_load_speech(contents, tokenizer, frontend, meta_data=meta_data, **kwargs) + batch = to_device(output, kwargs["device"]) + + # audio encoder + speech = batch["speech"] + + if len(speech) > 0: + if "audio_embedding" in kwargs and "audio_embedding_lens" in kwargs: + encoder_out = kwargs["audio_embedding"] + encoder_out_lens = kwargs["audio_embedding_lens"] + else: + speech_lengths = batch["speech_lengths"][:, 0] + # fp16 + if kwargs.get("fp16", False): + speech = speech.to(torch.float16) + elif kwargs.get("bf16", False): + speech = speech.to(torch.bfloat16) + # audio encoder + encoder_out, encoder_out_lens = self.encode(speech, speech_lengths) + + # audio_adaptor + encoder_out, encoder_out_lens = self.audio_adaptor(encoder_out, encoder_out_lens) + + meta_data["audio_adaptor_out"] = encoder_out + meta_data["audio_adaptor_out_lens"] = encoder_out_lens + + input_ids = batch["input_ids"] + source_ids = batch["source_ids"] + fbank_beg = batch["fbank_beg"] + fake_token_len = batch["fake_token_len"] + + if not kwargs.get("tearchforing", False): + input_ids = source_ids + + input_ids[input_ids < 0] = 0 + inputs_embeds = self.llm.model.get_input_embeddings()(input_ids) + + batch_size, token_num, dims = inputs_embeds.shape + + fake_token_len[fake_token_len < 0] = 0 + fbank_beg[fbank_beg < 0] = 0 + + speech_idx = 0 + for batch_idx in range(batch_size): + + for turn_id in range(fbank_beg.shape[1]): + fbank_beg_idx = fbank_beg[batch_idx, turn_id].item() + if fbank_beg_idx > 0: + speech_token_len = fake_token_len[batch_idx, turn_id] + speech_token = encoder_out[speech_idx, :speech_token_len, :] + + try: + inputs_embeds[ + batch_idx, fbank_beg_idx : fbank_beg_idx + speech_token_len, : + ] = speech_token + except Exception as e: + # + logging.error(f"{str(e)}, {traceback.format_exc()}") + logging.info( + f"batch_idx: {batch_idx}, inputs_embeds: {inputs_embeds.shape}, fbank_beg_idx: {fbank_beg_idx}, speech_token_len: {speech_token_len}, encoder_out: {encoder_out.shape}, encoder_out_lens: {encoder_out_lens}, fake_token_len: {fake_token_len}, speech_lengths: {speech_lengths}" + ) + # import pdb; + # pdb.set_trace() + speech_token_len = encoder_out_lens[speech_idx].item() + speech_token = encoder_out[speech_idx, :speech_token_len, :] + inputs_embeds[ + batch_idx, fbank_beg_idx : fbank_beg_idx + speech_token_len, : + ] = speech_token + + speech_idx += 1 + return inputs_embeds, contents, batch, source_ids, meta_data + + def inference( + self, + data_in, + data_lengths=None, + key: list = None, + tokenizer=None, + frontend=None, + **kwargs, + ): + + inputs_embeds, contents, batch, source_ids, meta_data = self.inference_prepare( + data_in, data_lengths, key, tokenizer, frontend, **kwargs + ) + + llm_dtype = kwargs.get("llm_dtype", "fp32") + if llm_dtype == "fp32": + llm_dtype = "fp16" if kwargs.get("fp16", False) else llm_dtype + llm_dtype = "bf16" if kwargs.get("bf16", False) else llm_dtype + + with torch.cuda.amp.autocast( + enabled=True if llm_dtype != "fp32" else False, dtype=dtype_map[llm_dtype] + ): + label = contents["assistant"][-1] + self.llm = self.llm.to(dtype_map[llm_dtype]) + inputs_embeds = inputs_embeds.to(dtype_map[llm_dtype]) + llm_kwargs = kwargs.get("llm_kwargs", {}) + if not kwargs.get("tearchforing", False): + + generated_ids = self.llm.generate( + inputs_embeds=inputs_embeds, + max_new_tokens=kwargs.get("max_length", 512), + **llm_kwargs, + ) + # generated_ids = [ + # output_ids[len(input_id) :] + # for input_id, output_ids in zip(input_ids, generated_ids) + # ] + response = tokenizer.batch_decode( + generated_ids, skip_special_tokens=kwargs.get("skip_special_tokens", True) + )[0] + + loss = None + else: + + labels_ids = batch["labels_ids"] + labels_ids[labels_ids == -1] = -100 + attention_mask = batch.get("attention_mask", None) + # attention_mask = attention_mask.to(dtype_map[llm_dtype]) + model_outputs = self.llm( + inputs_embeds=inputs_embeds, + attention_mask=attention_mask, + labels=labels_ids, + **llm_kwargs, + ) + + preds = torch.argmax(model_outputs.logits, -1)[:, source_ids.shape[1] :] + response = tokenizer.batch_decode( + preds, + add_special_tokens=False, + skip_special_tokens=kwargs.get("skip_special_tokens", True), + )[0] + loss = model_outputs.loss.item() + + ibest_writer = None + if kwargs.get("output_dir") is not None: + if not hasattr(self, "writer"): + self.writer = DatadirWriter(kwargs.get("output_dir")) + ibest_writer = self.writer[f"{0 + 1}best_recog"] + + results = [] + response_clean = re.sub(r"[^\w\s\u3000\u4e00-\u9fff]+", "", response) + result_i = {"key": key[0], "text": response, "text_tn": response_clean, "label": label} + if loss is not None: + result_i["loss"] = loss + results.append(result_i) + + if ibest_writer is not None: + ibest_writer["text"][key[0]] = response.replace("\n", " ") + ibest_writer["label"][key[0]] = label.replace("\n", " ") + ibest_writer["text_tn"][key[0]] = response_clean + + return results, meta_data