OpenccJiebaLib 1.3.0

dotnet add package OpenccJiebaLib --version 1.3.0
                    
NuGet\Install-Package OpenccJiebaLib -Version 1.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenccJiebaLib" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenccJiebaLib" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="OpenccJiebaLib" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpenccJiebaLib --version 1.3.0
                    
#r "nuget: OpenccJiebaLib, 1.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OpenccJiebaLib@1.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpenccJiebaLib&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=OpenccJiebaLib&version=1.3.0
                    
Install as a Cake Tool

OpenccJiebaLib

NuGet NuGet Downloads License

A .NET Standard 2.0 library providing a managed C# wrapper for the Rust-based OpenCC and Jieba C API, enabling efficient Chinese text conversion (Simplified/Traditional), segmentation, and keyword extraction in .NET applications.

Features

  • Chinese Text Conversion: Convert between Simplified, Traditional, and other Chinese variants using OpenCC.
  • Word Segmentation: Segment Chinese text into words using Jieba.
  • Keyword Extraction: Extract keywords using TF-IDF or TextRank algorithms.
  • Native Performance: Leverages native OpenCC/Jieba libraries for high performance.

Supported OpenCC Configurations

s2t, t2s, s2tw, tw2s, s2twp, tw2sp, s2hk, hk2s, t2tw, t2twp, t2hk, tw2t, tw2tp, hk2t, t2jp, jp2t, s2hkp, hk2sp, t2hkp, hk2tp

The phrase-aware Hong Kong configurations added in native v0.8.0 are:

  • s2hkp: Simplified Chinese to Hong Kong Traditional Chinese with phrase conversion.
  • hk2sp: Hong Kong Traditional Chinese to Simplified Chinese with phrase conversion.
  • t2hkp: Traditional Chinese to Hong Kong Traditional Chinese with phrase conversion.
  • hk2tp: Hong Kong Traditional Chinese to Traditional Chinese with phrase conversion.

Getting Started

Prerequisites

  • .NET Standard 2.0 or higher (.NET Framework, .NET Core/5+/6+, Mono, Xamarin, etc.).
  • .NET 6.0 or later recommended.
  • Native opencc_jieba_capi library (must be available to the runtime).

Installation

Option 1 — Project Reference
  • Add a project reference to OpenccJiebaLib in your solution.
  • Ensure the native binary is available at runtime in the standard layout:
runtimes/<RID>/native/

Expected filenames:

  • Windows: opencc_jieba_capi.dll
  • Linux: libopencc_jieba_capi.so
  • macOS: libopencc_jieba_capi.dylib

🧪 Unit tests (MSTest/xUnit/nUnit) also need the native binaries in the test project’s output folder. Use the same copy strategy (copy runtimes/** into bin/…) or add an MSBuild Target to auto-copy natives after build.


Option 2 — From NuGet
dotnet add package OpenccJiebaLib
  • The NuGet package includes platform-specific native runtimes under:
runtimes/<RID>/native/

Shipped RIDs: win-x64, linux-arm64, linux-x64, osx-arm64

When publishing with -r <RID>, dotnet publish copies only the matching native runtime into the publishing output.


Custom native runtimes (drop-in)

OpenccJiebaLib loads the native library from the standard NuGet layout:

runtimes/<RID>/native/<library>

To add support for another platform, drop in your own native binary:

  1. Create the directory:

    runtimes/<RID>/native/
    
  2. Copy the native library using the expected filename:

  • Windows: opencc_jieba_capi.dll
  • Linux: libopencc_jieba_capi.so
  • macOS: libopencc_jieba_capi.dylib

Example (Intel macOS):

runtimes/osx-x64/native/libopencc_jieba_capi.dylib

The loader will pick it up automatically at runtime.


Usage

using OpenccJiebaLib;

using (var openccJieba = new OpenccJieba())
{
    string traditional = openccJieba.Convert("汉字转换测试", OpenccConfig.S2T);
    string hongKong = openccJieba.Convert("鼠标", OpenccConfig.S2HKP);

    string[] searchTokens = openccJieba.Segment(
        "我来到北京清华大学",
        SegmentMode.Search,
        hmm: true);

    string tagged = openccJieba.SegmentJoin(
        "我来到北京清华大学",
        SegmentMode.Tag,
        hmm: true,
        delimiter: " ",
        separator: "/");

    string[] keywords = openccJieba.JiebaKeywordExtract(
        "这是一个用于关键词提取的测试文本",
        5,
        JiebaKeywordAlgorithm.Tfidf);

    var (kw, weights) = openccJieba.JiebaExtractKeywordsWeights(
        "这是一个用于关键词提取的测试文本",
        5,
        JiebaKeywordAlgorithm.TextRank);

    int script = openccJieba.ZhoCheck("汉字转换测试");
}

String-based overloads are still available for configuration names such as "s2t" and keyword methods such as "textrank", but the enum-based APIs are recommended for new code.

Error Handling

Common exception types:

  • DllNotFoundException: the native OpenCC-Jieba library cannot be found.
  • EntryPointNotFoundException: the native library does not export a required function.
  • BadImageFormatException: the native library does not match the current process architecture.
  • InvalidOperationException: native initialization or native call failures after the library has loaded.
  • ArgumentOutOfRangeException: invalid enum values passed to OpenccConfig, SegmentMode, or JiebaKeywordAlgorithm based APIs.
  • ArgumentException: unsupported keyword method names. Invalid string configuration names passed to Convert(string, string, bool) intentionally fall back to s2t; use OpenccConfigExtensions.Parse for strict validation.
  • ArgumentNullException: null keyword method passed to JiebaExtractKeywordsWeights(..., string method, ...).

API Overview

Conversion

  • Convert(string input, string config, bool punctuation = false)
  • Convert(string input, OpenccConfig configId, bool punctuation = false)
  • OpenccConfig
  • OpenccConfigExtensions.IsValidConfig(string name)
  • OpenccConfigExtensions.Parse(string name)
  • OpenccConfigExtensions.ToCanonicalName(this OpenccConfig config)

Segmentation & Tagging

  • Segment(string input, SegmentMode mode, bool hmm = true)
  • SegmentJoin(string input, SegmentMode mode, bool hmm = true, string delimiter = " ", string separator = "/")
  • ZhoCheck(string input)

Legacy (deprecated):

  • JiebaCutAndJoin(string input, bool hmm, string delimiter)

Low-level methods:

  • JiebaCut(string input, bool hmm)
  • JiebaCutForSearch(string input, bool hmm)
  • JiebaCutAll(string input)
  • JiebaTag(string input, bool hmm)
  • JiebaTagAsString(string input, bool hmm, string separator = "/")

Keyword Extraction

  • JiebaKeywordAlgorithm
  • KeywordAlgorithmExtensions.Parse(string value)
  • KeywordAlgorithmExtensions.ToNativeMethod(this JiebaKeywordAlgorithm algorithm)
  • JiebaKeywordExtract(string input, int topK, JiebaKeywordAlgorithm algorithm, string allowedPos = "")
  • JiebaKeywordExtractTfidf(string input, int topK, string allowedPos = "")
  • JiebaKeywordExtractTextRank(string input, int topK, string allowedPos = "")

Weighted results:

  • JiebaExtractKeywordsWeights(string input, int topK, string method, string allowedPos = "")
  • JiebaExtractKeywordsWeights(string input, int topK, JiebaKeywordAlgorithm algorithm, string allowedPos = "")
  • JiebaKeywordExtractTfidfWeights(string input, int topK, string allowedPos = "")
  • JiebaKeywordExtractTextRankWeights(string input, int topK, string allowedPos = "")

Utilities

  • GetNativeAbiNumber()
  • GetNativeVersionString()

Troubleshooting

DllNotFoundException

  • Ensure the native file exists under runtimes/<RID>/native/.
  • If using NuGet, clean and rebuild the project.

BadImageFormatException

  • Architecture mismatch (x86 vs x64).

Platform Notes

  • Linux: may require LD_LIBRARY_PATH.
  • macOS: remove Gatekeeper quarantine flags:
xattr -dr com.apple.quarantine libopencc_jieba_capi.dylib

License

MIT License. See LICENSE.

Acknowledgements

  • OpenCC
  • Jieba
  • opencc-jieba-rs
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.0 120 8/1/2026
1.2.2 131 4/21/2026
1.2.1 127 3/23/2026
1.2.0 127 3/23/2026
1.1.1 272 10/28/2025
1.1.0 263 10/7/2025
1.0.1 298 8/28/2025
1.0.0 281 8/27/2025

v1.3.0
           
           - Updated native opencc-jieba-capi to v0.8.0
           - Added phrase-aware Hong Kong configs: s2hkp, hk2sp, t2hkp, and hk2tp