5个版本
| 0.2.2 | 2023年4月9日 |
|---|---|
| 0.2.1 | 2023年4月9日 |
| 0.2.0 | 2023年4月9日 |
| 0.1.1 | 2023年4月8日 |
| 0.1.0 | 2023年4月8日 |
1011 在 文本处理 中
每月35次下载
17KB
202 行
charmap
一个用于一对一(无/一/多)字符映射的Rust库。其主要用例是预处理、转写和清理自然语言文本。
使用方法
要使用charmap与libstd的映射类型(HashMap和BTreeMap),请将以下内容添加到您的Cargo.toml
[dependencies]
charmap = "0.2"
这还应该允许您使用rustc-hash的FxHashMap,因为它属于libstd的HashMap。
charmap还支持hashbrown的HashMap和phf的Map和OrderedMap类型。您可以通过分别设置"hashbrown"和"phf"功能来启用这些功能。例如,要使用charmap与phf,请将以下内容添加到您的Cargo.toml
[dependencies]
charmap = {version = "0.2", features = ["phf"]}
您还可以通过设置default-features = false来禁用no_std构建对libstd的支持。例如
[dependencies]
charmap = {version = "0.2", default-features = false, features = ["phf"]}
示例
以下是如何使用charmap与libstd的HashMap的示例
use std::collections::HashMap;
use charmap::*;
// We first define our action map that will tell our CharMapper what to do
// when it sees a particular character.
let actions = HashMap::from([
('!', CharMapAction::Delete), // Delete instances of '!'
('l', CharMapAction::SubStr("LLL")), // Substitute instances of 'l' with 'LLL'
]);
// This is the string we want to charmap.
let start_str = "Hello, world!";
// Create a character mapper using the previously defined actions while
// allowing all other character to be output as they are.
let mapper = CharMapper::new(&actions, CharMapAction::Pass);
// Use mapper to charmap start_str
let mapped_str: String = start_str.map_chars(&mapper).collect();
// Output should be: HeLLLLLLo, worLLLd
println!("{}", mapped_str);
许可证
CharMap在MIT许可证和Apache许可证(版本2.0)的条款下分发。
有关详细信息,请参阅LICENSE-MIT和LICENSE-APACHE。
依赖项
~0–435KB