为 AWS Manila Community Day 的 Tagalog 卡片构建语法与发音增强流水线
Tagalog 学习应用已经有文章页面和句子卡片。下一个开发挑战是内容增强:在不手动编辑数百个重复 HTML 块的情况下,为每个额外示例加入更好的语法拆解和发音指南。Python 脚本展示了一种实用的批处理模式。
文章现已发布于 AWS 官方 Builder Center。你可以前往官方文章,或关闭此弹窗并留在当前页面继续阅读。
Tagalog 学习应用已经有文章页面和句子卡片。下一个开发挑战是内容增强:在不手动编辑数百个重复 HTML 块的情况下,为每个额外示例加入更好的语法拆解和发音指南。Python 脚本展示了一种实用的批处理模式。
Tagalog 学习应用已经有文章页面和句子卡片。下一个开发挑战是内容增强:在不手动编辑数百个重复 HTML 块的情况下,为每个额外示例加入更好的语法拆解和发音指南。Python 脚本展示了一种实用的批处理模式。
用途:这个静态应用是用于语言练习和开发者分享的教育原型,帮助学习者为 AWS Manila Community Day 准备礼貌的 Tagalog 短语。
非商业用途:本项目没有付费功能、广告、注册要求或商业目的,仅用于学习、实验和社区准备。
不作保证:生成的语言内容可能包含错误。Tagalog 翻译、语法解释、发音指南和文化说明在生产使用前应由母语者复核。
范围:此应用不是 AWS 官方产品,不是官方翻译工具,也不能替代真人语言教学。它是一个带有实用学习内容的技术 Demo。
社区尊重:应用应避免刻板印象,并谨慎讲解礼貌用语。po、opo、kayo 和 ninyo 等词应被解释为表达尊重的工具,而不是装饰。
Paki-check po kung pumasok ang bayad.
逐词发音可以读作:
pah-kee-chehk poh koong poo-mah-sohk ahng bah-yahd.
核心增强任务:
div.extra-example 块span 读取 Tagalog 句子这一部分解释为什么增强工作要拆分到多个脚本中,而不是写成一个巨大的文件。
这一部分展示脚本如何使用字典条目,为初学者提供友好的语法含义和本地借词解释。
这一部分解释发音映射、词元处理、元音回退规则,以及面向初学者的分块输出。
这一部分展示一种代码模式:找到现有章节,并只替换目标标题之后的内容。
这一部分解释为什么脚本需要打印额外示例、发音短语、本地提示和语法拆解的数量。
在不手动编辑每张卡片的情况下,改进 24 个文章页面中的语法和发音内容。
关键技能是受控批处理。每个脚本负责一小组文章文件,并配有针对该组内容调优的词汇表。
files = [
"article-22-manila-daily-home-laundry-bills-and-errands.html",
"article-23-manila-daily-work-study-and-social-plans.html",
"article-24-manila-daily-health-safety-weather-and-money.html",
]
这种模式比一个超大脚本更容易审查,因为每个批次都可以携带主题相关词汇。Community Day 页面需要 registration、workshop、badge、volunteer 等词;Manila Daily 页面需要 laundry、delivery、battery、cash、clinic、medicine 等词。
For each article group, update every extra example.
Keep the existing Tagalog sentence.
Replace the grammar breakdown with beginner-friendly word meanings.
Replace the pronunciation guide with word-by-word pronunciation.
Write an updated HTML file and print a summary.
项目获得了一条可重复的增强工作流:
article group
-> topic glossary
-> visible Tagalog sentence
-> grammar list
-> pronunciation guide
-> updated HTML
-> sanity check
把每个脚本变成一个小而可审查的语言辅助工具。
脚本把字典当作本地知识库。像 po 或 saan 这样的词会得到稳定的初学者解释;未知词则回退到通用的本地用法说明。
defs = {
"po": "Respect marker used for polite speech.",
"saan": "Means where.",
"workshop": "English loanword used locally; means workshop.",
"badge": "English loanword used locally; means badge.",
}
def get_def(word):
key = token_key(word)
if key in defs:
return defs[key]
return f'English loanword or useful word used locally; means "{word}" in this context.'
这种方法不是完整的语法解析器,但对静态学习原型很有用。学习者能看到一致的含义,开发者也可以在某个解释需要改进时只更新一个字典条目。
Sentence:
Saan po ang registration area?
Generated breakdown:
- Saan: Means where.
- po: Respect marker used for polite speech.
- ang: Focus marker placed before the main noun or idea.
- registration: English loanword used locally; means registration.
- area: English loanword or useful word used locally; means "area" in this context.
即使不是每个词都在发音映射表中,也为每个额外示例提供可朗读的发音指南。
脚本结合了两种策略:
Known word:
use a curated pronunciation and optional syllable chunks.
Unknown word:
use a simple vowel fallback so the learner still gets a readable guide.
pron = {
"salamat": ("sah-lah-maht", [("sa", "sah"), ("la", "lah"), ("mat", "maht")]),
"kayo": ("kah-yoh", [("ka", "kah"), ("yo", "yoh")]),
"bayad": ("bah-yahd", [("ba", "bah"), ("yad", "yahd")]),
}
vmap = {"a": "ah", "e": "eh", "i": "ee", "o": "oh", "u": "oo"}
def fallback_pron(word):
output = []
for character in word.lower():
if character in vmap:
output.append(vmap[character])
elif character.isalpha():
output.append(character)
return "".join(output) or word
Tagalog:
Uminom po kayo ng tubig dahil mainit.
Pronunciation:
oo-mee-nohm poh kah-yoh ngah too-beeg dah-heel mah-ee-neet.
Chunks:
- Uminom: oo-mee-nohm.
- po: poh.
- kayo: kah-yoh.
- tubig: too-beeg.
- dahil: dah-heel.
- mainit: mah-ee-neet.
在不重写整个文章页面的情况下更新生成的 HTML。
增强脚本使用 BeautifulSoup 解析页面,找到每个 div.extra-example,读取 Tagalog 的 span,然后替换特定标题之后的内容。
for fname in files:
soup = BeautifulSoup(Path(fname).read_text(encoding="utf-8"), "html.parser")
divs = soup.find_all("div", class_="extra-example")
for div in divs:
span = div.find("span", lang="tl")
if not span:
continue
sentence = " ".join(span.get_text(" ", strip=True).split())
replace_after_heading(div, "Grammatical Breakdown:", [make_breakdown_ul(soup, sentence)])
replace_after_heading(div, "Pronunciation Guide:", make_pronunciation(soup, sentence))
辅助函数 replace_after_heading 很重要,因为它避免替换整张卡片。它只移除一个标题和下一个已知标题之间的旧内容。
def replace_after_heading(div, heading_text, new_nodes):
heading = None
for candidate in div.find_all("p", recursive=False):
strong = candidate.find("strong")
if strong and heading_text in strong.get_text():
heading = candidate
break
if not heading:
return False
sibling = heading.find_next_sibling()
while sibling:
next_sibling = sibling.find_next_sibling()
if sibling.name == "p":
strong = sibling.find("strong")
if strong and "Pronunciation Guide:" in strong.get_text():
break
sibling.extract()
sibling = next_sibling
last = heading
for node in new_nodes:
last.insert_after(node)
last = node
return True
证明批量更新确实触达了预期内容。
脚本在写入文件后打印摘要行和基本校验结果。
print("Update summary:")
for source, out, total, updated, missing in summary:
print(f"{source} -> {out}: extra_examples={total}, updated={updated}, missing={missing}")
print("Sanity check:")
for out in outputs:
soup = BeautifulSoup(Path(out).read_text(encoding="utf-8"), "html.parser")
divs = soup.find_all("div", class_="extra-example")
phrase = sum(1 for div in divs if "It is pronounced word by word as:" in div.get_text())
breakdown = sum(1 for div in divs if "Grammatical Breakdown:" in div.get_text())
print(f"{out}: extra_examples={len(divs)}, pron_phrase={phrase}, has_breakdown={breakdown}")
开发者可以把增强流水线解释为一个可衡量的过程,而不是一次手工清理。
Input:
article HTML files
Transformation:
grammar and pronunciation regeneration
Output:
updated HTML files
Evidence:
counts for extra examples, pronunciation phrases, and grammar breakdowns
背景:短语对很有用,但语法和发音会把它变成真正的学习卡片。
目标:在不改变句子卡片布局的情况下,加入可重复的学习支持。
提示词:从可见的 Tagalog 句子生成语法和发音。
结果:每个额外示例都对初学者更有帮助。
审查检查:生成的辅助内容是否解释了卡片上的真实句子?
背景:一个脚本处理全部 24 篇文章会很大,也很难调优。
目标:让每个批次贴近自己的词汇领域。
提示词:每个脚本只处理三个文章文件。
结果:Community Day、Friendship 和 Manila Daily 内容都可以拥有更贴近本地语境的词汇表。
审查检查:审查者能否从文件列表和注释中理解词汇范围?
背景:发音回退规则很有用,但它不是母语者保证。
目标:给学习者一个起点,同时让复核要求保持可见。
提示词:有整理好的发音时使用它;需要时使用简单回退。
结果:即使不是每个词都有完美发音条目,站点仍然有用。
审查检查:重要活动短语是否经过整理,而不是只依赖回退规则?
对于开发者分享来说,这条增强流水线是实用内容工程的一个强示例:
HTML article files
->
BeautifulSoup parser
->
extra-example blocks
->
Tagalog sentence extraction
->
glossary definitions
->
pronunciation map and fallback
->
section replacement
->
updated HTML files
->
sanity checks
核心经验很简单:AI 辅助的学习内容仍然需要确定性工具。小脚本可以把生成页面转化为可审查的教育材料。
语法和发音脚本展示了手工编辑与过度构建之间的实用中间路线。项目不需要数据库或语言引擎来改进每张卡片;它需要清晰的文章批次、主题词汇表、发音辅助函数、谨慎的 HTML 修补和验证输出。这样应用对学习者更有帮助,也更容易向开发者解释。