16个版本
0.8.4 | 2021年8月19日 |
---|---|
0.8.3 | 2021年6月5日 |
0.8.0 | 2020年5月19日 |
0.7.1 | 2018年9月25日 |
0.4.1 | 2016年3月18日 |
#97 in 文本处理
10,297 每月下载量
在 35 个crates (12 直接) 中使用
3.5MB
1.5K SLoC
hyphenation
支持多种语言UTF-8字符串的连字符分割。
[dependencies]
hyphenation = "0.8.3"
提供两种策略
- 标准Knuth–Liang连字符分割,字典由TeX UTF-8模式构建。
- 扩展(非标准)的连字符分割,基于László Németh的OpenOffice.org中的自动非标准连字符分割,字典由Libre/OpenOffice模式构建。
文档
使用
快速入门
hyphenation
库依赖于连字符分割字典,这些外部文件必须加载到内存中。但是,最初可以将它们嵌入到编译后的工件中,这可能会更方便。
[dependencies]
hyphenation = { version = "0.8.3", features = ["embed_all"] }
hyphenation
的最高模块提供了一个小的前言,可以导入以公开最常用的功能。
use hyphenation::*;
// Retrieve the embedded American English dictionary for `Standard` Knuth-Liang hyphenation.
let en_us = Standard::from_embedded(Language::EnglishUS) ?;
// Identify valid breaks in the given word.
let hyphenated = en_us.hyphenate("hyphenation");
// Word breaks are represented as byte indices into the string.
let break_indices = &hyphenated.breaks;
assert_eq!(break_indices, &[2, 6, 7]);
// The segments of a hyphenated word can be iterated over, marked or unmarked.
let marked = hyphenated.iter();
let collected : Vec<String> = marked.collect();
assert_eq!(collected, vec!["hy-", "phen-", "a-", "tion"]);
let unmarked = hyphenated.iter().segments();
let collected : Vec<&str> = unmarked.collect();
assert_eq!(collected, vec!["hy", "phen", "a", "tion"]);
// `hyphenate()` is case-insensitive.
let uppercase : Vec<_> = en_us.hyphenate("CAPITAL").into_iter().segments().collect();
assert_eq!(uppercase, vec!["CAP", "I", "TAL"]);
运行时加载字典
当前可用的字典总量约为2.8MB的数据。虽然嵌入它们是一个选项,但大多数应用程序应更倾向于在运行时加载单个字典,如下所示
let path_to_dict = "/path/to/en-us.bincode";
let english_us = Standard::from_path(Language::EnglishUS, path_to_dict) ?;
hyphenation
捆绑的字典可以从target
下的构建文件夹中检索,并按需与最终应用程序打包。
$ find target -name "dictionaries"
target/debug/build/hyphenation-33034db3e3b5f3ce/out/dictionaries
分割
可以将字典与文本分割结合使用,以分割文本运行中的单词。以下简短示例使用unicode-segmentation
crate进行未经定制的Unicode分割。
use unicode_segmentation::UnicodeSegmentation;
let hyphenate_text = |text : &str| -> String {
// Split the text on word boundaries—
text.split_word_bounds()
// —and hyphenate each word individually.
.flat_map(|word| en_us.hyphenate(word).into_iter())
.collect()
};
let excerpt = "I know noble accents / And lucid, inescapable rhythms; […]";
assert_eq!("I know no-ble ac-cents / And lu-cid, in-escapable rhythms; […]"
, hyphenate_text(excerpt));
规范化
受规范化影响的语言的连字符模式有时会覆盖多个形式,由其作者决定,但通常不会。如果您要求hyphenation
严格在已知规范化形式的字符串上操作,如Unicode标准附录#15所述,并由unicode-normalization
crate提供,您可以在Cargo清单中指定它,如下所示
[dependencies.hyphenation]
version = "0.8.3"
features = ["nfc"]
features
字段可以包含以下规范化选项之一
"nfc"
,用于规范组合;"nfd"
,用于规范分解;"nfkc"
,用于兼容组合;"nfkd"
,用于兼容分解。
如果启用了规范化,您可能希望以发布模式构建 hyphenation
,因为捆绑的连字符模式需要重新处理成词典。
许可证
hyphenation
© 2016 tapeinosyne,根据以下任一许可证的双重授权:
- Apache许可证2.0版
- MIT许可证
hyph-utf8
连字符模式版权属于各自的拥有者;有关许可信息,请参阅它们的主文件。
patterns/hyph-hu.ext.txt
(扩展匈牙利连字符模式)根据以下许可证授权:
- MPL 1.1(参考
patterns/hyph-hu.ext.lic.txt
)
patterns/hyph-ca.ext.txt
(扩展加泰罗尼亚连字符模式)根据以下许可证授权:
- LGPL v.3.0或更高版本(参考
patterns/hyph-ca.ext.lic.txt
)
依赖关系
~3MB
~36K SLoC