Ferrous-opencc
A pure Rust implementation of the OpenCC project, dedicated to providing high-performance and reliable conversion between Traditional and Simplified Chinese.
Features
- High-Performance: Utilizes
FST(Finite State Transducers) for efficient dictionary lookups, significantly outperforming HashMap-based implementations. - Pure Rust: No C++ dependencies. Implemented entirely in Rust.
- Extensible: Supports loading custom OpenCC configuration files and dictionaries.
- Comprehensive Tooling: Includes a command-line tool to compile text dictionaries into an efficient
.ocbbinary format.
Quick Start
Add ferrous-opencc to your Cargo.toml:
[]
= "0.2"
Directory Structure
This library loads dictionaries and configuration files from the local filesystem. You can use the provided set of dictionary files, or compile your own and place them in the assets/dictionaries/ folder.
your-project/
├── assets/
│ ├── dictionaries/
│ │ ├── STPhrases.txt
│ │ ├── STCharacters.txt
│ │ ├── TPhrases.txt
│ │ └── ... (other .txt dictionary files)
│ └── s2t.json
└── src/
└── main.rs
You can obtain these dictionary and configuration files from the official OpenCC repository.
Examples
Method 1: Initialize with Configuration Name (Recommended)
Create an OpenCC instance using built-in configuration names, no external files required:
use ;
Supported Built-in Configuration Names:
| Configuration Name | Conversion Direction |
|---|---|
BuiltinConfig::S2t |
Simplified → Traditional |
BuiltinConfig::T2s |
Traditional → Simplified |
BuiltinConfig::S2tw |
Simplified → Traditional Chinese (Taiwan) |
BuiltinConfig::Tw2s |
Traditional Chinese (Taiwan) → Simplified |
BuiltinConfig::S2hk |
Simplified → Traditional Chinese (Hong Kong) |
BuiltinConfig::Hk2s |
Traditional Chinese (Hong Kong) → Simplified |
BuiltinConfig::S2twp |
Simplified → Traditional Chinese (Taiwan) (with Taiwan-specific vocabulary) |
BuiltinConfig::Tw2sp |
Traditional Chinese (Taiwan) (with Taiwan-specific vocabulary) → Simplified |
BuiltinConfig::T2tw |
Traditional → Traditional Chinese (Taiwan) |
BuiltinConfig::Tw2t |
Traditional Chinese (Taiwan) → Traditional |
BuiltinConfig::T2hk |
Traditional → Traditional Chinese (Hong Kong) |
BuiltinConfig::Hk2t |
Traditional Chinese (Hong Kong) → Traditional |
BuiltinConfig::Jp2t |
Japanese Shinjitai → Traditional |
BuiltinConfig::T2jp |
Traditional → Japanese Shinjitai |
Bold entries indicate the most commonly used configurations.
Method 2: Initialize with Configuration File
If you need to use custom configurations or external configuration files, here is a basic example of converting Simplified Chinese to Traditional Chinese:
use ;
Command-Line Tool
This library provides a dictionary compilation tool that can compile text dictionaries into binary .ocb format.
You can run this binary target directly through Cargo.
This will generate an STPhrases.ocb file in the same directory.
Using Custom Dictionaries
While this library comes with all standard dictionaries embedded, you might need to load your own dictionary files in certain scenarios. For instance, you may have just compiled an .ocb file using the opencc-dict-compiler tool, or you might want to load dictionaries dynamically at runtime.
This requires you to create a conversion configuration manually, rather than relying on the built-in configurations.
How It Works
- Write a Custom Config File: Create a
my_config.jsonfile to define your conversion pipeline. This config file must explicitly specify the paths to your dictionary files. - Create the Converter: In your Rust code, directly create the
OpenCCconverter using the configuration file path.
Example
Let's assume you have generated my_dicts/my_s2t_phrases.ocb and my_dicts/my_s2t_chars.ocb using the compiler tool.
1. Create my_config.json
Create a file named my_config.json in your project's root directory with the following content:
Note:
- Use
"type": "ocd2"to inform the library that this is a binary dictionary file. Although our extension is.ocb, its format is compatible with OpenCC v2's.ocd2. - The path in the
filefield is relative to the current working directory where your executable is run.
2. Use the Config in Rust
Now, you can write Rust code to use this configuration file.
use ;
License
This project is licensed under the Apache-2.0 license.