Rust中文词典构建库lindera-cc-cedict-builder的使用,支持CC-CEDICT词典的高效解析与词库生成
Lindera CC-CEDICT Builder
CC-CEDICT 词典构建器,用于 Lindera 项目。
词典格式
参考 unidic-mecab 词典格式和词性标签的手册获取详细信息。
| 索引 | 名称(中文) | 名称(英文) | 备注 | 
|---|---|---|---|
| 0 | 表面形式 | Surface | |
| 1 | 左语境ID | Left context ID | |
| 2 | 右语境ID | Right context ID | |
| 3 | 成本 | Cost | |
| 4 | 词类 | Major POS classification | |
| 5 | 词类1 | Middle POS classification | |
| 6 | 词类2 | Small POS classification | |
| 7 | 词类3 | Fine POS classification | |
| 8 | 併音 | pinyin | |
| 9 | 繁体字 | traditional | |
| 10 | 简体字 | simplified | |
| 11 | 定义 | definition | 
用户词典格式(CSV)
简单版本
| 索引 | 名称(日文) | 名称(英文) | 备注 | 
|---|---|---|---|
| 0 | 表面形式 | Surface | |
| 1 | 词类 | Major POS classification | |
| 2 | 併音 | pinyin | 
详细版本
| 索引 | 名称(日文) | 名称(英文) | 备注 | 
|---|---|---|---|
| 0 | 表面形式 | Surface | |
| 1 | 左语境ID | Left context ID | |
| 2 | 右语境ID | Right context ID | |
| 3 | 成本 | Cost | |
| 4 | 词类 | POS | |
| 5 | 词类1 | POS subcategory 1 | |
| 6 | 词类2 | POS subcategory 2 | |
| 7 | 词类3 | POS subcategory 3 | |
| 8 | 併音 | pinyin | |
| 9 | 繁体字 | traditional | |
| 10 | 简体字 | simplified | |
| 11 | 定义 | definition | |
| 12 | - | - | 12之后可以自由扩展 | 
完整示例代码
use lindera_cc_cedict_builder::build;
use std::path::Path;
fn main() {
    // 输入CC-CEDICT词典文件路径
    let input_path = Path::new("./cc-cedict.txt");
    
    // 输出Lindera词典目录路径
    let output_dir = Path::new("./lindera-dict");
    
    // 构建词典
    build(input_path, output_dir).unwrap();
    
    println!("词典构建完成!");
}
完整示例demo
下面是一个更完整的示例,展示如何从CC-CEDICT数据构建词典并使用:
use lindera_cc_cedict_builder::build;
use std::path::Path;
use std::fs::File;
use std::io::Write;
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 创建示例CC-CEDICT文件
    let cc_cedict_content = r#"# CC-CEDICT example
中國 中国 [Zhong1 guo2] /China/
你好 你好 [ni3 hao3] /hello/"#;
    
    // 写入临时文件
    let input_path = Path::new("cc-cedict-example.txt");
    let mut file = File::create(input_path)?;
    file.write_all(cc_cedict_content.as_bytes())?;
    
    // 输出目录
    let output_dir = Path::new("lindera-dict-example");
    
    // 构建词典
    build(input_path, output_dir)?;
    
    println!("词典构建完成!输出目录: {:?}", output_dir);
    
    // 清理临时文件
    std::fs::remove_file(input_path)?;
    
    Ok(())
}
安装
在项目目录中运行以下Cargo命令:
cargo add lindera-cc-cedict-builder
或者在Cargo.toml中添加以下行:
lindera-cc-cedict-builder = "0.32.3"
        
          1 回复
        
      
      
        Rust中文词典构建库lindera-cc-cedict-builder使用指南
简介
lindera-cc-cedict-builder是一个用于高效解析和构建CC-CEDICT中文词典的Rust库。它是Lindera分词器生态系统的一部分,专门用于处理CC-CEDICT格式的中英词典数据。
CC-CEDICT是一个流行的免费中文-英文词典项目,包含了大量现代汉语词汇及其英文翻译、拼音和词性信息。
主要功能
- 解析原始CC-CEDICT词典文件
 - 构建优化的二进制词典格式
 - 支持自定义词典构建选项
 - 高效处理大规模词典数据
 
安装
在Cargo.toml中添加依赖:
[dependencies]
lindera-cc-cedict-builder = "0.1"
基本使用方法
1. 从原始文件构建词典
use lindera_cc_cedict_builder::builder::LinderaCcCedictBuilder;
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 设置输入和输出路径
    let input_path = "path/to/cc-cedict.txt";  // 原始CC-CEDICT文件
    let output_dir = "path/to/output";         // 输出目录
    
    // 创建构建器实例
    let builder = LinderaCcCedictBuilder::new();
    
    // 构建词典
    builder.build(input_path, output_dir)?;
    
    Ok(())
}
2. 自定义构建选项
use lindera_cc_cedict_builder::builder::LinderaCcCedictBuilder;
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let builder = LinderaCcCedictBuilder::new()
        .normalize(true)        // 启用Unicode标准化
        .simplify(true);        // 将繁体转换为简体
    
    builder.build("cc-cedict.txt", "output")?;
    
    Ok(())
}
高级用法
处理构建结果
use std::fs::File;
use std::io::BufReader;
use lindera_core::dictionary::Dictionary;
fn load_and_use_dictionary() -> Result<(), Box<dyn std::error::Error>> {
    // 构建词典
    let builder = LinderaCcCedictBuilder::new();
    builder.build("cc-cedict.txt", "output")?;
    
    // 加载构建后的词典
    let dict = Dictionary::load_from_file("output/cc-cedict")?;
    
    // 使用词典...
    println!("词典包含 {} 个词条", dict.words_count());
    
    Ok(())
}
与Lindera分词器集成
use lindera::tokenizer::Tokenizer;
use lindera::mode::Mode;
fn use_with_lindera() -> Result<(), Box<dyn std::error::Error>> {
    // 先构建词典
    let builder = LinderaCcCedictBuilder::new();
    builder.build("cc-cedict.txt", "output")?;
    
    // 使用构建的词典初始化分词器
    let tokenizer = Tokenizer::with_dictionary("output/cc-cedict", Mode::Normal)?;
    
    // 分词示例
    let tokens = tokenizer.tokenize("这是一个测试句子")?;
    for token in tokens {
        println!("{:?}", token);
    }
    
    Ok(())
}
输入文件格式
CC-CEDICT原始文件格式示例:
# CC-CEDICT
# Community maintained free Chinese-English dictionary.
...
中國 中国 [Zhong1 guo2] /China/
你好 你好 [ni3 hao3] /hello/hi/
...
输出格式
构建后的词典是二进制格式,包含以下信息:
- 词条文本
 - 词频信息
 - 词性标注
 - 拼音信息
 - 英文翻译
 
性能提示
对于大型词典文件:
- 在构建时启用并行处理
 - 确保有足够的内存
 - 考虑将构建过程分成多个阶段处理
 
use lindera_cc_cedict_builder::builder::LinderaCcCedictBuilder;
fn build_large_dict() -> Result<(), Box<dyn std::error::Error>> {
    let builder = LinderaCcCedictBuilder::new()
        .parallel(true);  // 启用并行处理
    
    builder.build("large-cc-cedict.txt", "output")?;
    
    Ok(())
}
注意事项
- 确保原始CC-CEDICT文件使用UTF-8编码
 - 构建过程可能需要几分钟时间,取决于词典大小
 - 输出目录需要有写入权限
 - 构建后的词典文件与Lindera分词器兼容
 
这个库为中文文本处理提供了强大的词典支持,特别适合需要高性能中文分词和词性标注的应用场景。
完整示例代码
下面是一个完整的示例,展示如何构建词典并使用它进行中文分词:
use lindera_cc_cedict_builder::builder::LinderaCcCedictBuilder;
use lindera::tokenizer::Tokenizer;
use lindera::mode::Mode;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
    // 1. 构建词典
    println!("开始构建词典...");
    let builder = LinderaCcCedictBuilder::new()
        .normalize(true)    // 启用Unicode标准化
        .simplify(true)     // 将繁体转换为简体
        .parallel(true);    // 启用并行处理
    
    builder.build("cc-cedict.txt", "dict_output")?;
    println!("词典构建完成!");
    // 2. 使用构建的词典初始化分词器
    println!("初始化分词器...");
    let tokenizer = Tokenizer::with_dictionary("dict_output/cc-cedict", Mode::Normal)?;
    // 3. 分词示例
    let text = "这是一个Rust中文词典构建库的使用示例";
    println!("分词文本: {}", text);
    
    let tokens = tokenizer.tokenize(text)?;
    for token in tokens {
        println!("{:?}", token);
    }
    Ok(())
}
示例说明
- 首先使用
LinderaCcCedictBuilder构建词典,启用了Unicode标准化、繁简转换和并行处理 - 然后使用构建好的词典初始化Lindera分词器
 - 最后对示例中文文本进行分词处理并输出结果
 
这个完整示例展示了从词典构建到实际使用的完整流程,适合作为项目模板使用。
        
      
                    
                  
                    
