
人工智能语音交互助手智能硬件【免费下载链接】mycroft-coreMycroft Core, the Mycroft Artificial Intelligence platform.项目地址https://gitcode.com/gh_mirrors/my/mycroft-core点击查看免费下载本篇技术指南以 mycroft-core 官方 API 文档 doc/source/mycroft.util.format.rst 为骨架深入讲解语音助手技能Skill开发中最常用的格式化工具模块mycroft.util.format。该模块的核心使命是把原始数字、秒数、时间戳转化为人话——既能拼读成 TTS 语音也能输出为屏幕显示文本并完整支持多语言本地化。读完本文你将掌握 9 个公开 API 的完整签名、参数语义、输出示例与源码级行为细节能够直接在技能中输出地道的自然语言内容。模块定位为什么 Mycroft 需要一套人性化格式化工具语音交互与屏幕文本有一个根本差异机器看到的数值163秒、13:22、1.435634与人类听/读习惯的表达two minutes forty three seconds、one twenty two、1.436完全不同。mycroft.util.format正是为解决这一问题而存在。从 mycroft/util/format.py 顶部的模块 docstring 可以看到官方定位The mycroft.util.format module provides various formatting functions for things like numbers, times, etc. The focus of these formatting functions is to create human digestible content either as speech or in display form. It is also enables localization.也就是说该模块有两条设计主线双输出形态几乎所有函数都提供speech开关True时输出可供 TTS 朗读的自然语言如one minute one secondFalse时输出适合屏幕/日志的紧凑文本如1:01本地化优先全部函数通过lang参数接受 BCP-47 语言代码默认语言来自 lingua-franca 的默认设置详见下文语言与本地化小节。与 lingua-franca 的分工理解该模块的关键在于其分层架构mycroft/util/format.py 中明确注释了来源——绝大多数格式化逻辑来自独立的 lingua-francaMycroft 层只是做包装wrap与聚合# These are the main functions we are using lingua franca to provide from lingua_franca import get_default_loc from lingua_franca.format import ( join_list, nice_date, nice_date_time, nice_number, nice_time, nice_year, pronounce_number )其中nice_duration/nice_duration_dt是仅有的两个在 Mycroft 侧自行实现的时长格式化函数源码中的TODO 21.08 - move nice_duration methods to Lingua Franca注释表明它们计划在未来迁移回 lingua-franca。因此从本项目视角看整个模块的公开 API 由 9 个函数加 1 个枚举组成API功能实现来源join_list用连接词拼接列表and/orlingua-francanice_date日期人性化lingua-francanice_date_time日期时间人性化lingua-francanice_duration秒数/timedelta → 时长表达Mycroft 自实现nice_duration_dt两个 datetime 的差值 → 时长表达Mycroft 自实现nice_number数字 → 人性化数字lingua-francanice_time时间人性化lingua-francanice_year年份人性化lingua-francapronounce_number数字 → 完整拼读lingua-francaTimeResolution时长输出精度枚举Mycroft 自实现说明mycroft.util.format还会额外导出expand_options来自 lingua-franca 的date_time_format模块它被 mycroft/dialog/dialog.py 和 mycroft/skills/skill_data.py 用于展开.dialog/.voc文件中的(a|b)可选分支语法属于技能资源渲染的基础设施。数字格式化nice_number 与 pronounce_numberpronounce_number把数字读出来pronounce_number是语音输出最核心的工具签名按 lingua-franca 0.4.x 及测试用例确认的参数为pronounce_number(number, langNone, places2, short_scaleTrue, scientificFalse)number整数、浮点数或可转换为数字的字符串langBCP-47 语言代码默认取 lingua-franca 默认语言places小数保留位数语音读法按位拼读short_scale是否使用短级数制short scale如 billion/trillionFalse时使用长级数制scientific是否以科学计数法朗读。从 test/unittests/util/test_format.py 的测试夹具可以归纳出完整行为基础整数0 → zero1 → one15 → fifteen27 → twenty seven33 → thirty three。负数统一加minus 前缀-1 → minus one-33 → minus thirty three。小数按位拼读1.234 → one point two threeplaces参数控制拼读位数——21.234在places1/0/3/4/5下分别输出twenty one point two、twenty one、twenty one point two three four注意places超过实际小数位不会报错会按实际位数截断。大数长/短级数制1456 → fourteen fifty six百位以上按口语习惯分段95505896639631893在short_scaleTrue时读作ninety five quadrillion, five hundred and five trillion, ...而short_scaleFalse时切换为长级数制ninety five thousand five hundred and five billion, ...。科学计数法scientificTrue时33 → three point three times ten to the power of one299792458 → two point nine nine times ten to the power of eight配合places6可控制有效数字位数。该能力对技能朗读光速、阿伏伽德罗常数等场景非常实用。nice_number保留可读性的口语化数字nice_number与pronounce_number的分工在于它保留数字本身的可读性只在合适时切换为分数表达。其行为可用 test/unittests/util/test_format.py 中的NUMBERS_FIXTURE_EN夹具完整说明输入speechTrue 输出说明1.4356341.436默认保留 3 位小数2/5.02/5整数值去尾零0.5a half二分之一用口语1.3331 and a third三分之一2.6662 and 2 thirds三分之二0.25a forth四分之一注意 lingua-franca 的拼写为 forth0.753 forths四分之三3.13 and a tenth十分之一0.05a twentyith二十分之一核心参数denominators允许使用的分母列表例如nice_number(5.5, denominators[1, 2, 3])输出5 and a half若传入的分母列表无法表达该小数如nice_number(2.333, denominators[1, 2])则退回普通小数2.333speechFalse时输出显示形态如nice_number(6.777, speechFalse) → 6 7/9nice_number(6.0, speechFalse) → 6lang传不支持的代码如as-fd时安全降级为原数字字符串5.5不会抛异常。在 mycroft/util/init.py 中nice_number被提升为mycroft.util包的顶层导出技能中可直接from mycroft.util import nice_number使用。时间与日期格式化nice_time、nice_date、nice_date_time、nice_year这四个函数均直接包装 lingua-franca 的同名实现负责把datetime.datetime对象转成符合人类语言习惯的表达。nice_time口语化报时nice_time(dt, langNone, speechTrue, use_24hourFalse, use_ampmFalse)use_24hour控制 24 小时制use_ampm控制 AM/PM 后缀两者可独立组合。以 test/unittests/util/test_format.py 的用例为例datetime(2017, 1, 31, 13, 22, 3)的输出矩阵参数组合输出默认speechone twenty twouse_ampmTrueone twenty two p.m.speechFalse1:22speechFalse, use_ampmTrue1:22 PMspeechFalse, use_24hourTrue13:22use_24hourTruethirteen twenty two值得注意的语言学细节整点13:00 → one oclock口语/1:00显示24 小时制整点读thirteen hundred零分钟13:02 → one oh two分钟个位前补 oh24 小时制下则读thirteen zero two午夜00:02 → twelve oh two a.m.显示为12:02 AM24 小时制显示00:02特殊时刻12:15 → quarter past twelve一刻过5:30 → half past five a.m.半点1:45 → quarter to two差一刻。这些规则说明该函数不是简单翻译数字而是遵循英语报时的惯用表达。nice_date / nice_date_time / nice_yearnice_date(dt, langNone, nowNone) nice_date_time(dt, langNone, nowNone, use_24hourFalse, use_ampmFalse) nice_year(dt, langNone, bcFalse)now参数用于相对日期表达如今天/昨天传入参考时间点测试中通过nowNone表示以当下为锚点nice_date_time额外接收use_24hour/use_ampm透传给内部的nice_timenice_year的bc参数控制公元前年份表达。这些函数的正确性由多语言测试夹具驱动test/unittests/util/test_format.py 会读取date_time_format.config_path下每种语言的date_time_test.json文件逐一断言nice_date、nice_date_time、nice_year的输出并额外做全量扫描——遍历一年中 368 天、以及 09999 年间的所有年份确保任何语言下都不会产生空输出。这套数据驱动 全量扫描的双重测试策略是这四个函数可靠性的直接证据。join_list用连接词拼列表join_list(list_to_join, connector, sepNone, langNone)把列表元素用connectorand/or连接为自然语言序列sep可自定义元素间分隔符默认逗号。测试用例test/unittests/util/test_format.py展示join_list([a, b], and) → a and bjoin_list([a, b, c], or) → a, b or c牛津逗号join_list([a, b, c], or, ;) → a; b or c空输入None/[]返回空字符串技能中一个典型用法出现在 mycroft/skills/mycroft_skill/mycroft_skill.py 的ask_yesno实现它用join_list(options, or, langself.lang) ?拼接候选选项向用户发问用pronounce_number(idx 1, self.lang)朗读编号——这正是两个格式化 API 在真实技能代码中的协同实例。时长格式化nice_duration 与 nice_duration_dt这是本模块中唯一由 Mycroft 侧完整自研实现的部分mycroft/util/format.py逻辑最复杂、参数最丰富也是技能中计算等待时间、倒计时、播放时长时的首选工具。TimeResolution输出精度的下限两个时长函数都接受resolution参数其类型为本模块定义的枚举mycroft/util/format.pyclass TimeResolution(Enum): YEARS 1 DAYS 2 HOURS 3 MINUTES 4 SECONDS 5 MILLISECONDS 6语义是输出精度的下限例如resolutionTimeResolution.HOURS时只输出小时及以上单位resolutionTimeResolution.MINUTES时小于 1 分钟的结果会显示为under a minute语音或0m显示。文档明确提示两点限制nice_duration默认不会产生毫秒只有显式传入TimeResolution.MILLISECONDS才会输出毫秒nice_duration_dt不支持TimeResolution.MILLISECONDS传入时会静默回退到TimeResolution.SECONDS。nice_duration把秒数变成人话nice_duration(duration, langNone, speechTrue, use_yearsTrue, clockFalse, resolutionTimeResolution.SECONDS)duration秒数int/float或datetime.timedeltaspeechTrue输出口语one minute one secondspeechFalse输出M:SS/H:MM:SS/Dd H:MM:SS时钟格式1:01use_yearsTrue时按年/天拆分如366 天 → 1 year 1 dayFalse时输出总天数clockTrue强制输出数字时钟样式且只对显示输出生效、对 DAYS/YEARS 精度无影响。核心输出示例均来自 test/unittests/util/test_format.py输入speechTruespeechFalse1one second0:0161one minute one second1:013600one hour1h5000one hour twenty three minutes and twenty seconds1:23:20500000five days eighteen hours fifty three minutes and twenty seconds5d 18:53:200zero seconds0:00clockTrue与resolution的组合效果nice_duration(60, resolutionTimeResolution.HOURS, clockTrue, speechFalse) → 0:01:00nice_duration(1, resolutionTimeResolution.MINUTES, clockTrue, speechFalse) → 0:01即补齐了原本会被省略的前导零位。毫秒处理是内部实现的一大亮点mycroft/util/format.py_duration_handler会把 float 秒数的小数部分拆出作为毫秒——语音模式下保留 2 位0.25s → zero point two five seconds显示模式下保留 3 位并右补零nice_duration(0.25, speechFalse, resolutionTimeResolution.MILLISECONDS) → 0:00.250。nice_duration_dt两个时刻之间的时长nice_duration_dt(date1, date2, langNone, speechTrue, use_yearsTrue, clockFalse, resolutionTimeResolution.SECONDS)接受两个datetime.datetime内部先取max/min保证顺序mycroft/util/format.py再交给共享的_duration_handler。典型结果nice_duration_dt(datetime(2019, 1, 1), date2datetime(2018, 1, 1)) → one yearnice_duration_dt(..., use_yearsFalse) → three hundred and sixty five daysnice_duration_dt(datetime(2019, 12, 25, 20, 30), date2datetime(2019, 10, 31, 8, 00), speechFalse) → 55d 12h 30m该函数有两个值得注意的边界行为mycroft/util/format.py类型必须一致date1与date2类型不同会抛出Exception测试test_nice_duration_dt用pytest.raises(Exception)验证非 datetime 的第二参数被忽略传入数值型第二参数时发出UserWarning并忽略之测试用pytest.warns(UserWarning)验证自动降低精度当两个 datetime 的秒、分、时均为 0 时会逐级把resolution上调为 MINUTES/HOURS/DAYS避免输出一长串无意义的00。此外两个 datetime 相差整年时还会通过calendar.leapdays校正闰年影响mycroft/util/format.py例如 2018-01-01 到 2019-01-01 在use_yearsTrue下输出one year而非365 days。语言与本地化lang 参数与默认语言设置所有格式化函数都接受langBCP-47 语言代码如en-us、es-mx不传时使用get_default_loc()的返回值mycroft/util/format.py。默认语言由 lingua-franca 的全局状态决定Mycroft 通过两个途径维护它配置启动mycroft/skills/main.py 在技能进程启动时读取Configuration中的lang字段默认en-us并调用load_languages([lang_code, en-us])加载对应语言包运行期切换mycroft/configuration/locale.py 提供set_default_lf_lang(lang_code)例如 mycroft/skills/intent_service.py 在收到消息时会按消息携带的语言调用它实现多语言会话的动态切换。因此在技能代码中你可以不传lang跟随全局也可以显式传入self.lang技能实例的语言保证与技能当前语言一致——mycroft/skills/mycroft_skill/mycroft_skill.py 正是这样做的。技能开发实战格式化 API 的典型组合结合上文一个播放进度播报类的技能可以这样组合使用from datetime import datetime, timedelta from mycroft.util.format import nice_duration, nice_time, join_list # 1) 把播放进度秒播报成口语时长 elapsed nice_duration(5000) # one hour twenty three minutes and twenty seconds remaining nice_duration(timedelta(seconds3660), speechFalse) # 1:01:00 # 2) 报时 now_str nice_time(datetime.now(), use_ampmTrue) # one twenty two p.m. # 3) 用连接词拼装候选列表对应 ask_yesno 的实现方式 choices join_list([volume up, volume down, stop], or, langen-us) # volume up, volume down or stop判断要不要用的简单经验法则需要朗读数字 →pronounce_number/nice_number(speechTrue)需要显示数字 →nice_number(speechFalse)分数形式或pronounce_number(speech 场景除外)秒数转时长 →nice_duration两个时间点差 →nice_duration_dt报时/报日期/报年份 →nice_time/nice_date/nice_date_time/nice_year拼接选项列表 →join_list。质量保障测试如何锁定格式化行为mycroft.util.format的全部行为都被 test/unittests/util/test_format.py 以单元测试形式固化是阅读与验证本文所有示例的第一手依据TestNiceNumberFormat用NUMBERS_FIXTURE_EN字典20 余组输入/期望输出对覆盖nice_number的分数口语化、denominators过滤与speechFalse形态并验证未知语言降级TestPronounceNumber覆盖整数、负数、小数按位拼读、places截断、长/短级数制、科学计数法甚至包含95505896639631893量级的超大数据TestNiceDateFormat以 lingua-franca 各语言的date_time_test.json驱动nice_date/nice_date_time/nice_year的多语言断言并对一年 368 天、09999 年做全量空输出扫描TestNiceDurationFuncs系统验证nice_duration/nice_duration_dt的 speech/clock/resolution/use_years 组合矩阵以及类型错误抛异常、第二参数被忽略告警等边界行为TestJoinList验证连接词、分隔符、空输入等场景。这些测试与 mycroft/util/format.py 的实现一一对应如果你要扩展该模块或迁移到 lingua-franca这套用例就是现成的回归保障。小结mycroft.util.format是 Mycroft 技能开发中把数据变成语言的标准化出口9 个公开函数覆盖数字拼读、口语化数字、报时、报日期、报年份、时长表达与列表拼接统一遵循speech双形态输出与lang本地化两条设计主线。其中nice_duration/nice_duration_dt由 Mycroft 侧基于TimeResolution精度枚举自主实现毫秒处理、闰年校正、精度自动降级等细节值得深入阅读 mycroft/util/format.py其余函数包装自 lingua-franca其行为已被 test/unittests/util/test_format.py 的多语言数据驱动测试完整锁定。技能作者只需要记住一条准则凡是最终要说或显示给用户的数字与时间都交给本模块处理。赞分享人工智能语音交互助手智能硬件【免费下载链接】mycroft-coreMycroft Core, the Mycroft Artificial Intelligence platform.项目地址https://gitcode.com/gh_mirrors/my/mycroft-core点击查看免费下载相关推荐BitCPM4-CANN-8B量化感知训练(QAT)技术详解仅5%训练开销的奥秘BitCPM4 CANN 8B量化感知训练 QAT 技术详解仅5%训练开销的奥秘 你是否想过能否在几乎不损失性能的前提下将大语言模型的存储和计算需求降低6Leptos数字格式化数字、日期、时间格式化Leptos数字格式化数字、日期、时间格式化 引言 在现代Web应用开发中数据格式化是一个至关重要的环节。无论是展示货币金额、日期时间还是处理用户输入的数前端后端Web框架SSR旧Mac还能装新版macOS吗OpenCore Legacy Patcher 三阶段完整指南旧Mac还能装新版macOS吗OpenCore Legacy Patcher 三阶段完整指南 升级弹窗弹出这台 Mac 太旧无法安装此版本 macOS时操作系统固件驱动开发创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考