Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
Add support for pinyin field with tone numbers
Browse files Browse the repository at this point in the history
resolves #56
  • Loading branch information
PandaScience committed May 23, 2022
1 parent c45c286 commit cea2243
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions chinese/behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
sanitize_transcript,
split_transcript,
transcribe,
get_tone_number_pinyin,
)
from .translate import translate
from .util import (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions chinese/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
"法語",
"法语"
],
"toneNumbers": [
"Tone Numbers",
"Tones",
"NumPinyin"
],
"pinyin": [
"Pinyin",
"Reading",
Expand Down
3 changes: 3 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_get_fields_no_arg(self):
'Hanzi (Color)',
'Hanzi',
'Measure Word',
"NumPinyin",
'Pinyin (Taiwan)',
'Pinyin',
'Reading',
Expand All @@ -55,6 +56,8 @@ def test_get_fields_no_arg(self):
'Sound (Mandarin)',
'Sound',
'Spoken',
'Tone Numbers',
'Tones',
'Trad',
'Trad.',
'TradColor',
Expand Down

0 comments on commit cea2243

Please sign in to comment.