From 682a9737c6a8bf1953153ff44b0313425f2d79bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Wirnata?= Date: Sun, 22 May 2022 23:35:18 +0200 Subject: [PATCH] Add support for pinyin field with tone numbers resolves luoliyan/chinese-support-redux#56 --- chinese/behavior.py | 15 +++++++++++++++ chinese/config.json | 5 +++++ tests/test_config.py | 3 +++ 3 files changed, 23 insertions(+) diff --git a/chinese/behavior.py b/chinese/behavior.py index 5b5832e..81c1fbd 100644 --- a/chinese/behavior.py +++ b/chinese/behavior.py @@ -28,6 +28,7 @@ sanitize_transcript, split_transcript, transcribe, + get_tone_number_pinyin, ) from .translate import translate from .util import ( @@ -155,6 +156,19 @@ def fill_transcript(hanzi, note): return n_filled +def fill_tone_numbers(hanzi, note): + filled = False + separated = split_hanzi(hanzi, grouped=False) + + if get_first(config['fields']['toneNumbers'], note) == '': + trans = transcribe(separated, 'pinyin', 'simp') + tones = [get_tone_number_pinyin(t) for t in trans] + set_all(config['fields']['toneNumbers'], note, to=' '.join(tones)) + filled = True + + return filled + + def reformat_transcript(note, group, target): if target == 'bopomofo': return @@ -311,6 +325,7 @@ def update_fields(note, focus_field, fields): fill_all_defs(hanzi, copy) fill_classifier(hanzi, copy) fill_transcript(hanzi, copy) + fill_tone_numbers(hanzi, copy) fill_trad(hanzi, copy) fill_color(hanzi, copy) fill_sound(hanzi, copy) diff --git a/chinese/config.json b/chinese/config.json index 5147a67..5d65e67 100644 --- a/chinese/config.json +++ b/chinese/config.json @@ -34,6 +34,11 @@ "法語", "法语" ], + "toneNumbers": [ + "Tone Numbers", + "Tones", + "NumPinyin" + ], "pinyin": [ "Pinyin", "Reading", diff --git a/tests/test_config.py b/tests/test_config.py index f4c98c6..6ea9537 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -37,6 +37,7 @@ def test_get_fields_no_arg(self): 'Hanzi (Color)', 'Hanzi', 'Measure Word', + "NumPinyin", 'Pinyin (Taiwan)', 'Pinyin', 'Reading', @@ -55,6 +56,8 @@ def test_get_fields_no_arg(self): 'Sound (Mandarin)', 'Sound', 'Spoken', + 'Tone Numbers', + 'Tones', 'Trad', 'Trad.', 'TradColor',