Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions tools/content/textbook_ipa.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ def to_textbook(ipa):
return out


def weak_article(text, display):
"""Write the article "a" as /ə/, not the letter name /eɪ/.

The reference is phonemized word by word, and espeak-ng reads "a" alone as
the letter. A capital "A" is the article only at the start of a sentence
("A man..."), not inside one ("plan A"). The display is left as it is when
its chunks do not line up one to one with the words.
"""
if not display:
return display
import phonemize

words = [(raw, phonemize.normalize_word(raw)) for raw in text.split()]
words = [(raw, word) for raw, word in words if word]
chunks = display.split()
if len(words) != len(chunks):
return display
out = []
for i, ((raw, word), chunk) in enumerate(zip(words, chunks)):
starts_sentence = i == 0 or words[i - 1][0][-1] in ".!?:"
article = word == "a" and ("a" in raw or starts_sentence)
out.append("ə" if article and chunk.strip("ˈˌ") == "eɪ" else chunk)
return " ".join(out)


def assert_textbook(ipa):
"""Raise when a rewritten string still carries an espeak-only symbol."""
leftover = TEXTBOOK_ONLY.intersection(ipa)
Expand Down Expand Up @@ -243,10 +268,12 @@ def apply_to_banks(bank_dir, records):
displays = []
for item in bank.get(list_key, []):
if folder == "dialogues":
for i in range(len(item.get("turns", []))):
displays.append(by_key.get(f'{item["id"]}#t{i}'))
for i, turn in enumerate(item.get("turns", [])):
display = by_key.get(f'{item["id"]}#t{i}')
displays.append(weak_article(turn.get("text", ""), display))
else:
displays.append(by_key.get(item.get("id")))
said = item.get("text") or " ".join(item.get("paragraphs", []))
displays.append(weak_article(said, by_key.get(item.get("id"))))
gaps += sum(1 for d in displays if not d)
new_text, count, changed = _insert_display(text, displays)
if count != len(displays):
Expand Down
26 changes: 26 additions & 0 deletions tools/content/textbook_ipa_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ def test_rewrite_is_idempotent(self):
self.assertEqual(once, textbook_ipa.to_textbook(once), ipa)


class WeakArticleTest(unittest.TestCase):
def test_the_article_is_a_schwa_not_the_letter_name(self):
self.assertEqual(
"hˈæv ə ɡˈʊd flˈaɪt",
textbook_ipa.weak_article("Have a good flight!", "hˈæv ˈeɪ ɡˈʊd flˈaɪt"),
)

def test_a_capital_a_is_the_article_only_at_the_start_of_a_sentence(self):
self.assertEqual(
"ə mˈæn ˈɪz hˈɪə",
textbook_ipa.weak_article("A man is here.", "ˈeɪ mˈæn ˈɪz hˈɪə"),
)
self.assertEqual(
"ɡˈəʊ. ə kˈæt",
textbook_ipa.weak_article("Go. A cat", "ɡˈəʊ. ˈeɪ kˈæt"),
)
self.assertEqual(
"plˈæn ˈeɪ",
textbook_ipa.weak_article("plan A", "plˈæn ˈeɪ"),
)

def test_misaligned_display_is_left_alone(self):
self.assertEqual("ˈeɪ", textbook_ipa.weak_article("a b", "ˈeɪ"))
self.assertIsNone(textbook_ipa.weak_article("a", None))


class GoldenTest(unittest.TestCase):
"""The owner's hard cases, pinned so the notation cannot drift."""

Expand Down