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

Commit

Permalink
Hack around Zhuyin issue; refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jdlorimer committed Aug 5, 2019
1 parent f879cba commit 7a88d51
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 106 deletions.
75 changes: 17 additions & 58 deletions chinese/behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,80 +109,39 @@ def fill_transcript(hanzi, note):
n_filled = 0
separated = split_hanzi(hanzi)

for key, target, func, type_ in [
('pinyin', 'pinyin', format_pinyin, 'simp'),
('pinyinTaiwan', 'pinyin_tw', format_taiwan_pinyin, 'trad'),
('cantonese', 'jyutping', format_cantonese, 'trad'),
for key, target, type_ in [
('bopomofo', 'bopomofo', 'trad'),
('cantonese', 'jyutping', 'trad'),
('pinyin', 'pinyin', 'simp'),
('pinyinTaiwan', 'pinyin_tw', 'trad'),
]:
if get_first(config['fields'][key], note) == '':
trans = colorize(transcribe(separated, target, type_))
trans = colorize(transcribe(separated, target, type_), target)
trans = hide(trans, no_tone(trans))
set_all(config['fields'][key], note, to=trans)
n_filled += 1
else:
func(note)
reformat_transcript(note, key, target)

n_filled += fill_bopomofo(hanzi, note)
return n_filled


def format_pinyin(note):
t = colorize(
accentuate(
split_transcript(
cleanup(get_first(config['fields']['pinyin'], note)),
'pinyin',
grouped=True,
),
'pinyin',
)
)
t = hide(t, no_tone(t))
set_all(config['fields']['pinyin'], note, to=t)

def reformat_transcript(note, group, target):
if target == 'bopomofo':
return

def format_taiwan_pinyin(note):
t = colorize(
accentuate(
split_transcript(
cleanup(get_first(config['fields']['pinyinTaiwan'], note)),
'pinyin',
cleanup(get_first(config['fields'][group], note)),
target,
grouped=True,
),
'pinyin',
target,
)
)
t = hide(t, no_tone(t))
set_all(config['fields']['pinyinTaiwan'], note, to=t)


def format_cantonese(note):
t = colorize(
split_transcript(
cleanup(get_first(config['fields']['cantonese'], note)),
'jyutping',
grouped=True,
)
)
t = hide(t, no_tone(t))
set_all(config['fields']['cantonese'], note, to=t)


def fill_bopomofo(hanzi, note):
field = get_first(config['fields']['bopomofo'], note)

if field:
syllables = cleanup(field).split()
n_filled = 0
else:
syllables = transcribe(split_hanzi(hanzi), 'bopomofo', 'trad')
n_filled = 1

text = colorize(syllables, 'bopomofo')
text = hide(text, no_tone(text))
set_all(config['fields']['bopomofo'], note, to=text)

return n_filled
set_all(config['fields'][group], note, to=t)


def fill_color(hanzi, note):
Expand Down Expand Up @@ -315,11 +274,11 @@ def update_fields(note, focus_field, fields):
else:
erase_fields(copy)
elif focus_field in config['fields']['pinyin']:
format_pinyin(copy)
reformat_transcript(copy, 'pinyin', 'pinyin')
elif focus_field in config['fields']['pinyinTaiwan']:
format_taiwan_pinyin(copy)
reformat_transcript(copy, 'pinyinTaiwan', 'pinyin_tw')
elif focus_field in config['fields']['cantonese']:
format_cantonese(copy)
reformat_transcript(copy, 'cantonese', 'jyutping')

updated = False

Expand Down
1 change: 1 addition & 0 deletions chinese/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def _repl(p):

d = {
'pinyin': PINYIN_REGEX,
'pinyin_tw': PINYIN_REGEX,
'jyutping': JYUTPING_REGEX,
'bopomofo': BOPOMOFO_REGEX,
}
Expand Down
5 changes: 1 addition & 4 deletions chinese/fill.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from .behavior import (
fill_all_defs,
fill_all_rubies,
fill_bopomofo,
fill_classifier,
fill_color,
fill_silhouette,
Expand Down Expand Up @@ -183,7 +182,6 @@ def bulk_fill_transcript():

hanzi = get_first(config['fields']['hanzi'], copy)
results = fill_transcript(hanzi, copy)
results += fill_bopomofo(hanzi, copy)

if results > 0:
d_added_pinyin += 1
Expand All @@ -205,8 +203,7 @@ def bulk_fill_transcript():

def bulk_fill_defs():
prompt = PROMPT_TEMPLATE.format(
field_names='<i>definition</i> and <i>alternative</i>',
extra_info='',
field_names='<i>definition</i> and <i>alternative</i>', extra_info=''
)

progress_msg_template = '''
Expand Down
50 changes: 43 additions & 7 deletions tests/test_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,31 @@
fill_all_defs,
fill_all_rubies,
fill_classifier,
fill_bopomofo,
fill_color,
fill_simp,
fill_sound,
fill_trad,
fill_transcript,
reformat_transcript,
update_fields,
)
from tests import Base


class FormatPinyin(Base):
def test_issue_78(self):
note = {'Hanzi': '壮观', 'Pinyin': 'zhuàngguān'}
expected = (
'<span class="tone4">zhuàng</span>'
'<span class="tone1">guān</span> '
'<!-- zhuang guan -->'
)
reformat_transcript(note, 'pinyin', 'pinyin')
self.assertEqual(note['Pinyin'], expected)
reformat_transcript(note, 'pinyin', 'pinyin')
self.assertEqual(note['Pinyin'], expected)


class FillSound(Base):
def test_missing_sound(self):
note = dict.fromkeys(['Sound (Mandarin)', 'Sound (Cantonese)'], '')
Expand Down Expand Up @@ -104,6 +118,7 @@ def test_words(self):
['Bopomofo', 'Cantonese', 'Pinyin (Taiwan)', 'Pinyin'], ''
)
self.assertEqual(fill_transcript('上海人', note), 4)
self.assertEqual(fill_transcript('上海人', note), 0)
self.assertEqual(
note['Bopomofo'],
(
Expand Down Expand Up @@ -152,6 +167,27 @@ def test_issue_7(self):
'<!-- fen xiang -->',
)

def test_issue_78(self):
note = {'Hanzi': '壮观', 'Pinyin': '', 'Bopomofo': ''}
expected_pinyin = (
'<span class="tone4">zhuàng</span>'
'<span class="tone1">guān</span> '
'<!-- zhuang guan -->'
)
expected_bopomofo = (
'<span class="tone4">ㄓㄨㄤˋ</span>'
'<span class="tone1">ㄍㄨㄢ</span> '
'<!-- ㄓㄨㄤˋㄍㄨㄢ -->'
)

fill_transcript(note['Hanzi'], note)
self.assertEqual(note['Pinyin'], expected_pinyin)
self.assertEqual(note['Bopomofo'], expected_bopomofo)

fill_transcript(note['Hanzi'], note)
self.assertEqual(note['Pinyin'], expected_pinyin)
self.assertEqual(note['Bopomofo'], expected_bopomofo)

def test_issue_81(self):
note = dict.fromkeys(['Pinyin', 'Bopomofo'], '')
fill_transcript('不言而喻', note)
Expand All @@ -173,7 +209,7 @@ def test_issue_81(self):
)


class FillBopomofo(Base):
def FillTranscriptBopomofo(Base):
expected = (
'<span class="tone2">ㄇㄟˊ</span>'
'<span class="tone3">ㄧㄡˇ</span> '
Expand All @@ -194,17 +230,17 @@ class FillBopomofo(Base):

def test_ungrouped_chars(self):
note = dict.fromkeys(['Bopomofo'], '')
fill_bopomofo('没有,是我第一次来上海旅游。', note)
fill_transcript('没有,是我第一次来上海旅游。', note)
self.assertEqual(note['Bopomofo'], self.expected)

def test_grouped_chars(self):
note = dict.fromkeys(['Bopomofo'], '')
fill_bopomofo('没有, 是 我 第一次 来 上海 旅游。', note)
fill_transcript('没有, 是 我 第一次 来 上海 旅游。', note)
self.assertEqual(note['Bopomofo'], self.expected)

def test_grouped_pinyin(self):
note = {'Bopomofo': '', 'Pinyin': 'shényùn'}
fill_bopomofo('神韻', note)
fill_transcript('神韻', note)
self.assertEqual(
note['Bopomofo'],
'<span class="tone2">ㄕㄣˊ</span>'
Expand All @@ -214,7 +250,7 @@ def test_grouped_pinyin(self):

def test_ungrouped_pinyin(self):
note = {'Bopomofo': '', 'Pinyin': 'shen4 yun4'}
fill_bopomofo('神韻', note)
fill_transcript('神韻', note)
self.assertEqual(
note['Bopomofo'],
'<span class="tone2">ㄕㄣˊ</span>'
Expand All @@ -224,7 +260,7 @@ def test_ungrouped_pinyin(self):

def test_issue_79(self):
note = {'Bopomofo': ''}
fill_bopomofo('狭隘', note)
fill_transcript('狭隘', note)
self.assertEqual(
note['Bopomofo'],
'<span class="tone2">ㄒㄧㄚˊ</span>'
Expand Down
Loading

0 comments on commit 7a88d51

Please sign in to comment.