![]() |
VOOZH | about |
dotnet add package OpenCCNET --version 1.1.0
NuGet\Install-Package OpenCCNET -Version 1.1.0
<PackageReference Include="OpenCCNET" Version="1.1.0" />
<PackageVersion Include="OpenCCNET" Version="1.1.0" />Directory.Packages.props
<PackageReference Include="OpenCCNET" />Project file
paket add OpenCCNET --version 1.1.0
#r "nuget: OpenCCNET, 1.1.0"
#:package OpenCCNET@1.1.0
#addin nuget:?package=OpenCCNET&version=1.1.0Install as a Cake Addin
#tool nuget:?package=OpenCCNET&version=1.1.0Install as a Cake Tool
👁 GitHub license
👁 Nuget
👁 Nuget
👁 GitHub Workflow Status
简体中文 |
OpenCC.NET 是 OpenCC (Open Chinese Convert, 开放中文转换) 的 C# 非官方版本,支持中文简繁体之间词汇级别的转换,同时还支持地域间异体字以及词汇的转换。
Nuget 搜索 OpenCCNET 并安装,在项目代码中引入命名空间 OpenCCNET。Nuget 包中自带 Dictionary(字典文件)和 JiebaResource(Jieba.NET运行所需的词典及其它数据文件),默认会复制到程序输出目录。
在使用前请调用ZhConverter.Initialize(),含四个默认参数:
dictionaryDirectory: 字典文件路径(默认为"Dictionary")jiebaResourceDirectory: Jieba.NET资源路径(默认为"JiebaResource")isParallelEnabled: 是否启用并行处理(默认为false)segmentMode: 分词模式(默认为结巴分词)// 默认初始化(使用结巴分词)
ZhConverter.Initialize();
// 或者指定分词模式(例如:OpenCC 的原版最大匹配分词算法)
ZhConverter.Initialize(segmentMode: SegmentMode.MaxMatch);
OpenCC.NET 提供了两种风格的API:
| 方法 | 简介 | 备注 |
|---|---|---|
| HansToHant(string) | 简体中文=>繁体中文(OpenCC标准) | |
| HansToTW(string, bool=false) | 简体中文=>繁体中文(台湾) | bool参数决定是否转换为台湾地区常用词汇 |
| HansToHK(string) | 简体中文=>繁体中文(香港) | |
| HantToHans(string) | 繁体中文=>简体中文 | |
| HantToTW(string, bool=false) | 繁体中文=>繁体中文(台湾) | bool参数决定是否转换为台湾地区常用词汇 |
| HantToHK(string) | 繁体中文=>繁体中文(香港) | |
| TWToHans(string, bool=false) | 繁体中文(台湾)=>简体中文 | bool参数决定是否转换为大陆地区常用词汇 |
| TWToHant(string, bool=false) | 繁体中文(台湾)=>繁体中文(OpenCC标准) | bool参数决定是否转换为大陆地区常用词汇 |
| HKToHans(string) | 繁体中文(香港)=>简体中文 | |
| HKToHant(string) | 繁体中文(香港)=>繁体中文(OpenCC标准) | |
| KyuuToShin(string) | 日语(旧字体)=>日语(新字体) | |
| ShinToKyuu(string) | 日语(新字体)=>日语(旧字体) |
var input = "为我的电脑换了新的内存,开启电脑后感觉看网络视频更加流畅了";
// 爲我的電腦換了新的內存,開啓電腦後感覺看網絡視頻更加流暢了
Console.WriteLine(ZhConverter.HansToHant(input));
// 為我的電腦換了新的內存,開啟電腦後感覺看網絡視頻更加流暢了
Console.WriteLine(ZhConverter.HansToTW(input));
// 為我的電腦換了新的記憶體,開啟電腦後感覺看網路影片更加流暢了
Console.WriteLine(ZhConverter.HansToTW(input, true));
// 為我的電腦換了新的內存,開啓電腦後感覺看網絡視頻更加流暢了
Console.WriteLine(ZhConverter.HansToHK(input));
// 沖繩縣內の學校
Console.WriteLine(ZhConverter.ShinToKyuu("沖縄県内の学校"));
| 方法 | 简介 | 备注 |
|---|---|---|
| ToHantFromHans() | 简体中文=>繁体中文(OpenCC标准) | |
| ToTWFromHans(bool=false) | 简体中文=>繁体中文(台湾) | bool参数决定是否转换为台湾地区常用词汇 |
| ToHKFromHans() | 简体中文=>繁体中文(香港) | |
| ToHansFromHant() | 繁体中文=>简体中文 | |
| ToTWFromHant(bool=false) | 繁体中文=>繁体中文(台湾) | bool参数决定是否转换为台湾地区常用词汇 |
| ToHKFromHant() | 繁体中文=>繁体中文(香港) | |
| ToHansFromTW(bool=false) | 繁体中文(台湾)=>简体中文 | bool参数决定是否转换为大陆地区常用词汇 |
| ToHantFromTW(bool=false) | 繁体中文(台湾)=>繁体中文(OpenCC标准) | bool参数决定是否转换为大陆地区常用词汇 |
| ToHansFromHK() | 繁体中文(香港)=>简体中文 | |
| ToHantFromHK() | 繁体中文(香港)=>繁体中文(OpenCC标准) | |
| ToShinFromKyuu() | 日语(旧字体)=>日语(新字体) | |
| ToKyuuFromShin() | 日语(新字体)=>日语(旧字体) |
var input = "為我的電腦換了新的記憶體,開啟電腦後感覺看網路影片更加流暢了";
// 爲我的電腦換了新的記憶體,開啓電腦後感覺看網路影片更加流暢了
Console.WriteLine(input.ToHantFromTW());
// 为我的电脑换了新的记忆体,开启电脑后感觉看网路影片更加流畅了
Console.WriteLine(input.ToHansFromTW());
// 为我的电脑换了新的内存,打开电脑后感觉看网络视频更加流畅了
Console.WriteLine(input.ToHansFromTW(true));
// 独逸連邦共和国
Console.WriteLine("獨逸聯邦共和國".ToShinFromKyuu());
OpenCC.NET 支持三种分词模式,可以根据需求灵活切换:
使用 jieba.NET 进行中文分词。默认设置 Jieba.NET 资源路径为 "JiebaResource",可以自行指定。
// 初始化时指定
ZhConverter.Initialize(segmentMode: SegmentMode.Jieba);
// 或运行时切换
ZhConverter.ZhSegment.SetMode(SegmentMode.Jieba);
使用 OpenCC 原版的最大匹配分词算法。
// 初始化时指定
ZhConverter.Initialize(segmentMode: SegmentMode.MaxMatch);
// 或运行时切换
ZhConverter.ZhSegment.SetMode(SegmentMode.MaxMatch);
使用用户自定义的分词算法,先分词一次,然后在转换链中重复使用分词结果。
// 方式1:直接设置分词委托(自动切换到 Custom 模式)(兼容老版本)
ZhConverter.ZhSegment.Segment = input =>
{
// 自定义分词逻辑,例如按空格分词
return input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
};
// 方式2:使用 SetCustomSegment 方法
ZhConverter.ZhSegment.SetCustomSegment(input =>
{
// 自定义分词逻辑,例如按字符分词
return input.Select(c => c.ToString());
});
OpenCC.NET默认使用jieba.NET实现分词,项目中使用了静态的JiebaSegmenter
public static JiebaSegmenter Jieba = new JiebaSegmenter();
因此可以通过ZhConverter.ZhSegment.Jieba进行自定义设置,详情请见jieba.NET。
调用ResetSegment()可重新指定使用Jieba分词并且重置Jieba参数。
对于大量文本的转换,可以启用并行处理来提高性能:
// 初始化时启用
ZhConverter.Initialize(isParallelEnabled: true);
// 或运行时设置
ZhConverter.IsParallelEnabled = true;
BYVoid/OpenCC 提供词库。
anderscui/jieba.NET 提供分词功能。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 1.1.0 | 10,387 | 11/15/2025 | |
| 1.0.3 | 5,381 | 3/21/2025 | |
| 1.0.2 | 179,157 | 5/5/2022 | |
| 1.0.1 | 733 | 4/26/2022 | |
| 1.0.0 | 694 | 4/25/2022 | |
| 0.2.2 | 677 | 1/18/2022 | |
| 0.2.1 | 771 | 1/18/2022 | 0.2.1 is deprecated because it has critical bugs. |
| 0.2.0 | 846 | 12/3/2021 | |
| 0.1.4 | 517 | 11/29/2021 | |
| 0.1.3 | 479 | 11/23/2021 | |
| 0.1.2 | 475 | 11/23/2021 | |
| 0.1.1 | 1,271 | 6/10/2021 | |
| 0.1.0 | 570 | 6/10/2021 |