开发工具【免费下载链接】HumanizerHumanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities项目地址https://gitcode.com/gh_mirrors/hu/Humanizer点击查看免费下载导读Humanizer 是 .NET 生态中处理字符串、枚举、日期、时间与数量的常用库而Vocabularies类正是其全部英语单复数Inflection功能的总入口。本指南以版本 3.0.1 的 Humanizer.Vocabularies API 文档 为骨架结合源码与测试深入讲解Vocabularies.Default静态属性的定位、内置规则的构成以及通过Vocabulary注册自定义复数、单数、不可数名词与缩写词Acronym的完整方法。读完本文你将能精确理解Pluralize()/Singularize()/Humanize()背后的词库工作机制并能在自己的项目中安全地扩展或覆盖默认规则。一、Vocabularies 类单一静态容器在 Humanizer 中所有屈折规则都集中在一个静态容器中。3.0.1 版文档将其定义为public static class Vocabularies从 src/Humanizer/Inflections/Vocabularies.cs 的源码可以看到它通过LazyVocabulary延迟构建默认词库并以只读静态属性暴露给整个进程static readonly LazyVocabulary Instance new(BuildDefault, LazyThreadSafetyMode.PublicationOnly); public static Vocabulary Default Instance.Value;关键设计点静态全局单例Default属性返回进程级的唯一Vocabulary实例任何线程调用Pluralize()/Singularize()都会读取该实例中注册的规则。延迟初始化词库在第一次访问Default时才构建未访问过则不会产生构建开销同时ApplyAcronyms/NormalizeAcronyms两个内部方法通过Instance.IsValueCreated判断在词库未构建时直接返回原输入避免无谓的锁竞争。单一词库限制文档明确说明At this time, multiple vocabularies and removing existing rules are not supported当前不支持多词库也不支持移除已有规则。从源码可见Vocabulary的构造函数是internal外部只能通过Vocabularies.Default操作这一个实例RemoveAcronym虽然是internal方法仅用于测试中的清理场景。二、Vocabularies.Default 属性默认词库的定位2.1 属性签名与职责文档给出的属性签名为public static Humanizer.Vocabulary Default { get; }对应源码 src/Humanizer/Inflections/Vocabularies.cs。它的职责有三提供singular/plural 不规则形式irregularities的匹配规则支持自定义缩写词的大小写保护custom acronym casing作为Pluralize()、Singularize()与Humanize()三大扩展方法的底层规则来源。例如 src/Humanizer/InflectorExtensions.cs 中Pluralize()直接转发到默认词库public static string? Pluralize(this string? word, bool inputIsKnownToBeSingular true) Vocabularies.Default.Pluralize(word, inputIsKnownToBeSingular);这意味着你向Vocabularies.Default添加的任何规则会立即对所有调用方生效——这正是它能进程级自定义的原因。2.2 内置词库的组成BuildDefault()方法Vocabularies.cs为默认词库预置了三类规则构建完成后通过MarkRulesAsBuiltIn()标记① 复数化规则AddPlural约 20 条正则覆盖常见英语复数形态例如s$ → s cat → cats (ax|test)is$ → $1es analysis → analyses ([^aeiouy]|qu)y$ → $1ies body → bodies (x|ch|ss|sh)$ → $1es box → boxes (^[m|l])ouse$ → $1ice mouse → mice ^(ox)$ → $1en ox → oxen (quiz)$ → $1zes quiz → quizzes② 单数化规则AddSingular约 30 条正则与复数规则互为逆映射例如s$ → cats → cat、(n)ews$ → $1ews保证 news 不被改写、([dti])a$ → $1umdata → datum、(^[m|l])ice$ → $1ousemice → mouse等。③ 不规则词AddIrregular如person/people、man/men、child/children、goose/geese、foot/feet、tooth/teeth、curriculum/curricula、zombie/zombies等。其中部分词以matchEnding: false注册如is/are、was/were、this/these、bus/buses、die/dice表示只在整词匹配时生效不参与词尾匹配。④ 不可数名词AddUncountable约 50 个如staff、information、equipment、fish、sheep、deer、series、species、metadata对应 issue 1132 的修复、以及计量单位oz、tsp、tbsp、ml、l等。这些词在单复数转换时原样返回。三、通过 Default 注册自定义规则3.1 添加复数 / 单数正则规则Vocabulary暴露了与内置规则同级的注册 API见 src/Humanizer/Inflections/Vocabulary.cspublic void AddPlural(string rule, string replacement) public void AddSingular(string rule, string replacement)规则为正则表达式大小写不敏感例如对 bus → buses 这类不规则词Vocabularies.Default.AddPlural((bus)es$, $1);自定义规则会追加到规则列表尾部。从ApplyRules的实现Vocabulary.cs看匹配从列表末尾向前扫描因此后注册的自定义规则优先于内置规则——这正是自定义覆盖默认行为的基础。测试 tests/Humanizer.Tests/InflectorTests.cs 验证了这一点向独立Vocabulary注册AddPlural(meter per second, meter rate units)后Pluralize(meter per second)返回自定义结果而非默认的 meters per second。3.2 添加不可数名词public void AddUncountable(string word)见 Vocabulary.cs。不可数词被存放在HashSetstring大小写不敏感比较中在ApplyRules第一步即短路返回原词Vocabulary.cs。因此向Vocabularies.Default.AddUncountable(metadata)后metadata.Pluralize()与metadata.Singularize()均返回 metadata。测试 InflectorTests.cs 还验证了整条短语如 meter per second也能注册为不可数。3.3 添加不规则词public void AddIrregular(string singular, string plural, bool matchEnding true)见 Vocabulary.cs。实现细节值得注意matchEnding: true默认时取首字符后的子串拼成后缀正则例如AddIrregular(person, people)等价于注册(p)erson$ → $1eople与(p)eople$ → $1erson即person 出现在长词末尾时也会被变换matchEnding: false时使用^singular$与^plural$整词锚定如内置的is → are、was → were避免把 this/his 等词误伤。3.4 添加缩写词Acronympublic void AddAcronym(string acronym)见 Vocabulary.cs。它注册一个规范大小写的缩写词使Humanize()在拆分字符串时保留其官方写法。参数约束严格不能为null抛出ArgumentNullException不能为空串或包含非字母字符抛出ArgumentException。测试 tests/Humanizer.Tests/StringHumanizeTests.cs 验证了null、空串与 HS2含数字均会被拒绝。典型用法与效果同文件 StringHumanizeTests.csVocabularies.Default.AddAcronym(iOS); Vocabularies.Default.AddAcronym(API); IOS.Humanize() // iOS iOSSettings.Humanize() // iOS settings iOSAPISettings.Humanize() // iOS API settings iOS5.Humanize() // iOS 5注意缩写词规则同样作用于 Humanize 之前的内部分词TheHTMLLanguage.Humanize()输出 The HTML languageHTML5.Humanize()输出 HTML 5见 StringHumanizeTests.cs。注册的缩写词使用RegexOptions.IgnoreCase匹配因此hsAccess.Humanize()在注册 HS 后会输出 HS access。四、扩展方法如何消费词库4.1 Pluralize / Singularize两者的完整签名InflectorExtensions.cspublic static string? Pluralize(this string? word, bool inputIsKnownToBeSingular true) public static string Singularize(this string word, bool inputIsKnownToBePlural true, bool skipSimpleWords false)inputIsKnownToBeSingular/Plural当不确定输入的单复数时传false方法会先尝试反向变换再正向验证避免双重变形的误判。测试 InflectorTests.cs 覆盖了该分支。skipSimpleWords为true时跳过仅以 s 结尾的简单词防止 tires 被错误单数化为 tire见 InflectorTests.cs。大小写保持MatchUpperCaseVocabulary.cs保证全大写输入得到全大写输出PERSON → PEOPLE首字母大写输入保留首字母大写Foot per Second → Feet per Second且使用 invariant culture见 InflectorTests.cs 的 tr-TR 文化测试。复合比率词形如 X per Y 的复合词只变形分子、保留分母meter per second → meters per second实现位于CompoundHeadLengthVocabulary.cs相关用例见 InflectorTests.cs。4.2 Humanize 与缩写词Humanize()内部通过Vocabularies.Default.ApplyAcronyms/NormalizeAcronymsVocabularies.cs识别并保留注册的缩写词写法Vocabulary.ApplyAcronyms的实现见 Vocabulary.cs采用lock保证并发注册/匹配安全对应测试 StringHumanizeTests.cs 的并行注册用例。五、使用边界与注意事项进程级副作用Vocabularies.Default是全局单例注册规则影响整个应用域的所有 Humanize/Pluralize/Singularize 调用。建议在应用启动阶段一次性注册避免运行时反复增删。只支持单一词库当前版本不提供多词库隔离或删除规则的能力RemoveAcronym为 internal仅供测试清理。若需隔离实验可如测试那样直接new Vocabulary()构造独立实例构造函数为 internal测试通过InternalsVisibleTo访问。正则规则是追加而非替换自定义规则追加在列表尾部并在匹配时优先扫描利用这一点可覆盖内置行为但注意规则数量增长会线性影响每次转换的性能不宜高频动态添加。缩写词仅限字母AddAcronym拒绝含数字的输入如 HS2 会抛ArgumentException数字与字母混合场景由内置分词逻辑另行处理。六、深入阅读类定义与内置规则src/Humanizer/Inflections/Vocabularies.cs规则存储与匹配引擎src/Humanizer/Inflections/Vocabulary.cs扩展方法入口src/Humanizer/InflectorExtensions.cs测试覆盖tests/Humanizer.Tests/InflectorTests.cs、tests/Humanizer.Tests/StringHumanizeTests.cs配套 API 文档Humanizer.Vocabulary结语Vocabularies是 Humanizer 屈折体系的单一入口Vocabularies.Default承载了一套精心维护的美式英语规则库并允许开发者以追加方式注册正则规则、不规则词、不可数名词与缩写词从而精确控制Pluralize()、Singularize()与Humanize()的行为。理解它的延迟构建、逆序匹配与大小写保持机制就能在真实项目中安全、高效地定制词库而不至于陷入规则不生效或全局污染的坑。赞分享开发工具【免费下载链接】HumanizerHumanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities项目地址https://gitcode.com/gh_mirrors/hu/Humanizer点击查看免费下载相关推荐Humanizer 词汇表机制详解Vocabularies 与 Vocabularies.Default 的规则注册与应用Humanizer 词汇表机制详解Vocabularies 与 Vocabularies.Default 的规则注册与应用 Vocabularies 是 Hu开发工具Humanizer 屈折变换指南深入解析 Humanizer.Inflections 的 Vocabularies 与 Vocabulary 词表体系Humanizer 屈折变换指南深入解析 Humanizer.Inflections 的 Vocabularies 与 Vocabulary 词表体系 导读开发工具Humanizer Vocabularies 全面解析掌握 .NET 单复数与缩写词规则的注册与定制Humanizer Vocabularies 全面解析掌握 .NET 单复数与缩写词规则的注册与定制 本文聚焦 Humanizer 的 Vocabularie开发工具上一篇Scrapy 接入 Crawlab 完整教程3 步搞定 Pipeline 配置与抓取结果回传实战下一篇5步解锁游戏无限可能BepInEx插件框架完全指南创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考