Rust文本断字库hyphenation_commons的使用,支持多语言连字符处理和自动断字功能

hyphenation commons

hyphenation_commonshyphenation的前置代码库。主要用于内部使用,稍微有些随意,但基本可靠。

安装

在项目目录中运行以下Cargo命令:

cargo add hyphenation_commons

或者在Cargo.toml中添加以下行:

hyphenation_commons = "0.8.4"

示例代码

以下是一个使用hyphenation_commons进行多语言文本断字的完整示例:

use hyphenation_commons::Hyphenation;

fn main() {
    // 创建一个英语断字器
    let language = "en_US";
    let dictionary = hyphenation_commons::load(language).unwrap();
    
    // 需要断字的文本
    let text = "hyphenation";
    
    // 进行断字处理
    let hyphenated = dictionary.hyphenate(text);
    
    // 输出结果
    println!("Original: {}", text);
    println!("Hyphenated: {}", hyphenated);
}

完整示例代码

use hyphenation_commons::{Hyphenation, Language};

fn main() {
    // 加载英语(美国)词典
    let language = Language::from("en_US");
    let dictionary = hyphenation_commons::load(language)
        .expect("Failed to load hyphenation dictionary");
    
    // 需要断字的文本
    let words = vec![
        "hyphenation",
        "algorithm",
        "beautiful",
        "international",
        "unbelievable"
    ];
    
    // 对每个单词进行断字处理并输出结果
    for word in words {
        let hyphenated = dictionary.hyphenate(word);
        println!("{} → {}", word, hyphenated);
    }
    
    // 演示包含空格的文本处理
    let phrase = "Hello world with hyphenation";
    println!("\n处理包含空格的文本:");
    for word in phrase.split_whitespace() {
        let hyphenated = dictionary.hyphenate(word);
        println!("{} → {}", word, hyphenated);
    }
}

元数据

  • 版本: 0.8.4
  • 发布时间: 大约4年前
  • 许可证: Apache-2.0 OR MIT
  • 大小: 6.82 KiB

文档

可以在docs.rs上查看完整的文档。

仓库

项目仓库位于GitHub。

所有者

  • Andrew (tapeinosyne)

1 回复

Rust文本断字库hyphenation_commons使用指南

完整示例Demo

以下是基于内容中提供的示例代码整合的完整Demo:

use hyphenation::{Hyphenator, Language, Load, Standard};
use lazy_static::lazy_static;
use std::path::Path;

// 使用lazy_static全局缓存英语断字器
lazy_static! {
    static ref EN_HYPHENATOR: Standard = hyphenation::Language::EnglishUS
        .dictionary()
        .unwrap()
        .hyphenator();
}

fn main() {
    // 示例1: 基本断字功能
    let hyphenated_word = EN_HYPHENATOR.hyphenate("hyphenation");
    println!("基本断字示例: {}", hyphenated_word); // 输出: hy-phen-ation

    // 示例2: 多语言支持
    let de = hyphenation::Language::German
        .dictionary()
        .unwrap();
    let de_hyphenator = de.hyphenator();
    let german_word = de_hyphenator.hyphenate("Zusammenarbeit");
    println!("德语断字示例: {}", german_word); // 输出: Zu-sam-men-ar-beit

    // 示例3: 自定义最小断字长度
    let options = hyphenation::HyphenationOptions {
        min_word_length: 6,
        ..hyphenation::HyphenationOptions::default()
    };
    let custom_hyphenator = hyphenation::Language::EnglishUS
        .dictionary()
        .unwrap()
        .configured(options);
    let short_word = custom_hyphenator.hyphenate("example");
    println!("自定义长度示例: {}", short_word); // 输出: example (不会被断字)

    // 示例4: 处理整段文本
    let text = "This is an example of hyphenation in a paragraph";
    let hyphenated_text = hyphenate_text(text, &EN_HYPHENATOR);
    println!("段落断字示例:\n{}", hyphenated_text);

    // 示例5: 从自定义路径加载字典
    if let Ok(dictionary) = hyphenation::Dictionary::from_path(
        Language::EnglishUS, 
        Path::new("path/to/custom.dic")
    ) {
        let custom_hyphenator = dictionary.hyphenator();
        println!("自定义字典加载成功");
    } else {
        println!("无法加载自定义字典,使用默认字典");
    }
}

// 处理整段文本的函数
fn hyphenate_text(text: &str, hyphenator: &Hyphenator) -> String {
    text.split_whitespace()
        .map(|word| hyphenator.hyphenate(word).to_string())
        .collect::<Vec<_>>()
        .join(" ")
}

示例说明

  1. 全局缓存:使用lazy_static宏创建一个全局的英语断字器实例,避免重复加载字典
  2. 多语言支持:演示了如何加载德语字典并进行断字处理
  3. 自定义配置:展示了如何设置最小断字长度为6个字符
  4. 段落处理:提供了处理整段文本的函数实现
  5. 自定义字典:演示了从自定义路径加载字典文件的方法

要运行此示例,需要在Cargo.toml中添加以下依赖:

[dependencies]
hyphenation = "0.8"
lazy_static = "1.4"

这个完整示例涵盖了内容中提到的所有主要功能点,可以直接运行测试hyphenation_commons库的各种用法。

回到顶部