Skip to content

Commit a33822b

Browse files
committed
WIP capitalization changes.
1 parent 2c04c28 commit a33822b

4 files changed

Lines changed: 40 additions & 20 deletions

File tree

scriptshifter/hooks/general/__init__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@
2727
logger = getLogger(__name__)
2828

2929

30+
def capitalize_pre_assembly(ctx):
31+
"""
32+
Capitalize a not-yet-assembled result list according to user options.
33+
"""
34+
ctx.dest_ls = _capitalize(ctx.dest_ls, ctx.options.get("capitalize"))
35+
36+
37+
def capitalize_post_assembly(ctx):
38+
"""
39+
Capitalize an already assembled result string according to user options.
40+
"""
41+
dest_ls = ctx.dest.split(" ")
42+
43+
dest_ls = _capitalize(dest_ls, ctx.options.get("capitalize"))
44+
45+
return " ".join(dest_ls)
46+
47+
48+
3049
def normalize_spacing_post_assembly(ctx):
3150
"""
3251
Remove duplicate and unwanted whitespace around punctuation.
@@ -53,3 +72,18 @@ def normalize_spacing_post_assembly(ctx):
5372
# norm = NORM8_RE.sub(r"\1\2", norm)
5473

5574
return norm
75+
76+
77+
def _capitalize(src, which):
78+
"""
79+
Only capitalize first word and words preceded by space.
80+
81+
NOTE: this function is only used for capitalizing hook-generated
82+
transliterations, which are not normally processed. Double cap rules are
83+
not applicable here.
84+
"""
85+
if which == "first":
86+
ctx.dest_ls[0] = ctx.dest_ls[0].upper()
87+
88+
elif which == "all":
89+
ctx.dest_ls = [tk[0].upper() + tk[1:] for tk in ctx.dest_ls]

scriptshifter/hooks/yiddish_/__init__.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,16 @@
1616
from yiddish import detransliterate, transliterate
1717

1818
from scriptshifter.exceptions import BREAK
19-
from scriptshifter.tools import capitalize
2019

2120

2221
def s2r_post_config(ctx):
2322
"""
2423
Script to Roman.
2524
"""
26-
rom = transliterate(
25+
ctx.dest = transliterate(
2726
ctx.src, loc=True,
2827
loshn_koydesh=ctx.options.get("loshn_koydesh"))
2928

30-
if ctx.options["capitalize"] == "all":
31-
rom = capitalize(rom)
32-
elif ctx.options["capitalize"] == "first":
33-
rom = rom[0].upper() + rom[1:]
34-
35-
ctx.dest = rom
36-
3729
return BREAK
3830

3931

scriptshifter/tools.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

scriptshifter/trans.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def transliterate(src, lang, t_dir="s2r", capitalize=False, options={}):
112112
)
113113

114114
# Normalize case before post_config and rule-based normalization.
115-
if not ctx.general["case_sensitive"]:
115+
if t_dir == FEAT_R2S and not ctx.general["case_sensitive"]:
116116
ctx._src = ctx.src.lower()
117117

118118
# This hook may take over the whole transliteration process or delegate
@@ -270,7 +270,10 @@ def transliterate(src, lang, t_dir="s2r", capitalize=False, options={}):
270270
# A match is found. Stop scanning tokens, append result,
271271
# and proceed scanning the source.
272272

273-
# Capitalization.
273+
# Capitalization. This applies double capitalization
274+
# rules. The external function in
275+
# scriptshifter.tools.capitalize used for non-table
276+
# languages does not.
274277
if (
275278
(ctx.options["capitalize"] == "first" and ctx.cur == 0)
276279
or

0 commit comments

Comments
 (0)