feat(lyric): implement precise pinyin filtering using xml:lang for TTML#970
feat(lyric): implement precise pinyin filtering using xml:lang for TTML#970kid141252010 wants to merge 2 commits intoimsyy:devfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在增强 TTML 歌词的“拒绝胎教”模式,通过利用 TTML XML 结构中的 xml:lang 属性,实现对汉语拼音音译的精确识别和过滤。这一改进避免了以往基于正则表达式可能出现的误判,同时修复了翻译优先级的问题,从而提供更准确、无干扰的歌词显示体验。 Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
本次 Pull Request 通过利用 xml:lang 属性,为 TTML 歌词实现了更精确的拼音过滤机制,替代了之前脆弱的声调正则表达式检测,这是一个很好的改进。同时,此变更还修复了翻译优先级判定的问题。代码改动与目标一致,整体实现良好。我主要有一个建议,即在 src/utils/lyric/parseTTML.ts 中使用正则表达式来解析 TTML 内容。虽然目前的实现很巧妙,但正则表达式处理 XML/HTML 格式的字符串通常不够健壮。我建议重构此部分代码,改用标准的 DOMParser API,以提高代码的鲁棒性和长期可维护性。
| // 无条件剔除繁体替换翻译段 <translation type="replacement" xml:lang="zh-Hant*"> | ||
| ttmlContent = ttmlContent.replace( | ||
| /<translation(?=[^>]*type="replacement")(?=[^>]*xml:lang="zh-Hant[^"]*")[^>]*>[\s\S]*?<\/translation>/g, | ||
| "", | ||
| ); | ||
| // 无条件剔除音译类型且语言声明为汉语拼音的 span (拒绝胎教 Mode) | ||
| ttmlContent = ttmlContent.replace( | ||
| /<span(?=[^>]*ttm:role="x-roman")(?=[^>]*xml:lang="zh-Hans-Latn")[^>]*>[\s\S]*?<\/span>/g, | ||
| "", | ||
| ); |
There was a problem hiding this comment.
虽然使用正则表达式处理 XML 字符串在特定场景下性能较好,但这种方法通常比较脆弱,并且难以维护。当 TTML 结构稍微复杂或属性顺序、格式有变动时,正则表达式可能会失效或产生意想不到的结果。
为了提高代码的健壮性和可读性,建议使用标准的 DOM 解析器(如 DOMParser)来处理 TTML 内容。通过操作 DOM 树来移除或修改节点,可以更安全、更清晰地实现过滤逻辑,避免潜在的解析错误。
例如,可以这样重构:
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(ttmlContent, "application/xml");
// 无条件剔除繁体替换翻译段
const hantTranslations = xmlDoc.querySelectorAll('translation[type=\"replacement\"][xml\\:lang^=\"zh-Hant\"]');
hantTranslations.forEach(node => node.remove());
// 无条件剔除汉语拼音音译
const pinyinSpans = xmlDoc.querySelectorAll('span[ttm\\:role=\"x-roman\"][xml\\:lang=\"zh-Hans-Latn\"]');
pinyinSpans.forEach(node => node.remove());
// ... 其他逻辑也可以在 DOM 上操作 ...
const serializer = new XMLSerializer();
ttmlContent = serializer.serializeToString(xmlDoc);考虑到函数内其他部分也使用了正则表达式,这可能需要对整个函数进行重构。如果这是一个有意的性能优化,可以添加注释说明。否则,为了长期的可维护性,迁移到 DOM 解析器是更优的选择。
Description
基于标准 TTML 语言标签实现的“拒绝胎教”模式增强。
主要改动:
xml:lang="zh-Hans-Latn"的音译标签进行过滤。