OpenCCNET 0.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package OpenCCNET --version 0.1.1
NuGet\Install-Package OpenCCNET -Version 0.1.1
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="OpenCCNET" Version="0.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpenCCNET --version 0.1.1
#r "nuget: OpenCCNET, 0.1.1"
#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.
// Install OpenCCNET as a Cake Addin
#addin nuget:?package=OpenCCNET&version=0.1.1

// Install OpenCCNET as a Cake Tool
#tool nuget:?package=OpenCCNET&version=0.1.1

OpenCC.NET

介绍

OpenCC.NET是OpenCC(Open Chinese Convert, 开放中文转换)的C#非官方版本,支持中文简繁体之间词汇级别的转换,同时还支持地域间异体字以及词汇的转换。

特点

  • 严格区分「一简对多繁」和「一简对多异」
  • 完全兼容异体字,可以实现动态替换
  • 严格审校一简对多繁词条,原则为「能分则不合」
  • 支持港/台异体字转换,以及大陆/台湾常用词汇转换
  • 完全兼容OpenCC原生词库,且词库和函数库完全分离,可以自由修改、导入、扩展
  • 基于.Net Standard 2.0,同时支持.NET Framework 4.6.1和.NET Core 2.0及以上版本

开始

获取

Nuget搜索OpenCCNET并安装,在项目代码中引入命名空间OpenCCNET。下载仓库中的Dictionary(字典文件)和JiebaResource(Jieba.NET运行所需的词典及其它数据文件)文件夹放入项目程序所在目录。

API

OpenCC.NET提供了两种风格的API。

ZhConverter静态类
方法 简介 备注
HansToHant(string) 简体中文=>繁体中文(OpenCC标准)
HansToTW(string, bool=false) 简体中文=>繁体中文(台湾) bool参数决定是否转换为台湾地区常用词汇
HansToHK(string) 简体中文=>繁体中文(香港)
HantToHans(string) 繁体中文=>简体中文
HantToTW(string, bool=false) 繁体中文=>繁体中文(台湾) bool参数决定是否转换为台湾地区常用词汇
HantToHK(string) 繁体中文=>繁体中文(香港)
TWToHans(string, bool=false) 繁体中文(台湾)=>简体中文 bool参数决定是否转换为大陆地区常用词汇
HKToHans(string) 繁体中文(香港)=>简体中文
var input = "为我的电脑换了内存,开启电脑后感觉网络速度更快了";
// 爲我的電腦換了內存,開啓電腦後感覺網絡速度更快了
Console.WriteLine(ZhConverter.HansToHant(input));
// 為我的電腦換了記憶體,開啟電腦後感覺網路速度更快了
Console.WriteLine(ZhConverter.HansToTW(input, true));
// 為我的電腦換了內存,開啓電腦後感覺網絡速度更快了
Console.WriteLine(ZhConverter.HansToHK(input));
string类扩展方法
方法 简介 备注
ToHantFromHans() 简体中文=>繁体中文(OpenCC标准)
ToTWFromHans(bool=false) 简体中文=>繁体中文(台湾) bool参数决定是否转换为台湾地区常用词汇
ToHKFromHans() 简体中文=>繁体中文(香港)
ToHansFromHant() 繁体中文=>简体中文
ToTWFromHant(bool=false) 繁体中文=>繁体中文(台湾) bool参数决定是否转换为台湾地区常用词汇
ToHKFromHant(string) 繁体中文=>繁体中文(香港)
ToHansFromTW(bool=false) 繁体中文(台湾)=>简体中文 bool参数决定是否转换为大陆地区常用词汇
ToHansFromHK(string) 繁体中文(香港)=>简体中文
var input = "為我的電腦換了記憶體,開啟電腦后感覺網路速度更快了";
// 爲我的電腦換了內存,開啓電腦后感覺網絡速度更快了
Console.WriteLine(input.ToHantFromTW(true));
// 为我的电脑换了记忆体,开启电脑后感觉网路速度更快了
Console.WriteLine(input.ToHansFromTW());
// 为我的电脑换了内存,开启电脑后感觉网络速度更快了
Console.WriteLine(input.ToHansFromTW(true));

自定义

分词

OpenCC.NET默认使用jieba.NET实现分词,因此第一次调用API时需要加载所需文件,可能会花费少许时间。项目中使用了静态的JiebaSegmenter

public static JiebaSegmenter Jieba = new JiebaSegmenter();

因此可以通过ZhUtil.Jieba进行自定义设置,详情请见jieba.NET

同时OpenCC.NET支持自定义分词实现:

ZhUtil.Segment = input =>
{
    // 输入为string,输出为IEnumerable<string>
    return new List<string>();
};

调用ResetSegment()可重新指定使用Jieba分词并且重置Jieba参数

引用

OpenCC

OpenCC提供词库。

jieba.NET

jieba.NET提供分词。

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. 
.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.0.2 17,702 5/5/2022
1.0.1 450 4/26/2022
1.0.0 419 4/25/2022
0.2.2 450 1/18/2022
0.2.1 439 1/18/2022
0.2.0 519 12/3/2021
0.1.4 281 11/29/2021
0.1.3 259 11/23/2021
0.1.2 246 11/23/2021
0.1.1 929 6/10/2021
0.1.0 342 6/10/2021