C# uses iKvm advanced technology to seamlessly integrate into the JVM ecosystem.
C# developers using iKVM to mooch off the JVM ecosystem? It's convenient, but dependency handling still requires manual effort. Seamless integration? It's just converting JARs to DLLs, saving the trouble of migrating code. Lacking AI tools? Just invoke HanLP, but dependency management... heh.
目录7 个章节
Preface
Time flies, and before I knew it, the National Day holiday was almost over. If I didn’t update my blog soon, I’d really be slacking off...
Recently, while working on AIHub, I was looking for a Named Entity Recognition (NER) library that I could use in C#, but I couldn’t find one. In terms of the AI ecosystem, C# isn’t exactly rich—it’s still more of a Python thing. However, I didn’t want to go down the gRPC route like with LLMs; it felt a bit cumbersome.
That’s when I realized that the JVM ecosystem actually has quite a few NLP tools, like Stanford NLP and HanLP. So, I thought of IKVM that I’d seen online before—I could just borrow the JVM ecosystem directly! 😃
About iKvm
Check the official introduction
IKVM is an implementation of Java for the Microsoft .NET platform. It can be used to quickly and easily:
- Execute compiled Java code (bytecode) on .NET Framework or .NET Core
- Convert bytecode to a .NET assembly to directly access its API in a .NET project
These tasks can be done without porting source code to .NET.
There are two working methods:
- Directly call the jar package to execute in C#
- Translate the jar package into a .NET platform dll, then reference and execute it
Generally, the second method is sufficient. The first one involves dynamic invocation and essentially provides no code hints, so I don't want to consider that approach.
iKvm is actually a comprehensive system. It includes a complete JDK standard library, runtime, and more. I gave it a quick look and found it has everything from Swing, XML, to media support. It even supports JDK reflection.
Furthermore, it comes with the modern build tool Maven!
PS: I'm not sure about Gradle; I haven't tried that yet.
Regarding Dependency Handling
While iKvm offers convenient Maven support, it cannot handle dependencies within a package!
For example, if you reference the StarAI package, which depends on the Transformer library, Maven would automatically download all dependencies for the build.
However, iKvm's Maven integration cannot automatically resolve dependencies. Therefore, you must manually add both the StarAI and Transformer libraries to the configuration.
Getting Started
This article uses HanLP as an example
Prerequisites
First, add two NuGet dependencies.
dotnet add package IKVM
dotnet add package IKVM.Maven.Sdk
Or directly edit the project file.
<ItemGroup>
<PackageReference Include="IKVM" Version="8.6.4" />
<PackageReference Include="IKVM.Maven.Sdk" Version="1.5.5" />
</ItemGroup>
Then, add the Maven dependency in the project file. You can just copy it directly from the Maven repository—very convenient!
For those unfamiliar with Java, here's a link: https://mvnrepository.com/
<ItemGroup>
<MavenReference Include="hanlp">
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.8.4</version>
</MavenReference>
</ItemGroup>
After saving, the IDE will automatically perform operations. It will download the dependencies IKVM needs, the JDK and runtimes for various platforms, and will also automatically download the JAR from Maven and transpile (transpile) it into a .NET platform DLL.
This process will take some time. Please wait patiently.
If it doesn't execute automatically, run the following manually.
dotnet restore
dotnet build
Getting Started with Coding
Here we use HanLP's sentence component analysis feature as an example.
using com.hankcs.hanlp.model.crf;
using com.hankcs.hanlp.model.perceptron;
using com.hankcs.hanlp.seg;
using com.hankcs.hanlp.seg.common;
namespace AIHub.Algo.HanLP;
public class NER {
private readonly string _modelPath;
public NER(string modelPath) {
_modelPath = modelPath;
}
public void Recognize(string input) {
PerceptronLexicalAnalyzer analyzer = new PerceptronLexicalAnalyzer(
Path.Combine(_modelPath, "cws.bin"),
Path.Combine(_modelPath, "pos.bin"),
Path.Combine(_modelPath, "ner.bin")
);
var result = analyzer.analyze(input);
Console.WriteLine(result);
}
}
For testing, simply call the Recognize method.
References
_EOF_
C# uses iKvm advanced technology to seamlessly integrate into the JVM ecosystem.
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
在.NETCore(C#)中使用EPPlus-Core导出Excel文档
首先安装 直接NuGet安装就行,贼方便。 使用方法 有很多种办法导出,本文只介绍最方便,代码最少的那种。 首先定义一个实体类,例如: public class OutputData { public string Address { get; set; } public int Title { get; set; } public int Description { ge
2021-12-18 · C# · 约 1 分钟
C#移除字符串中的不可见Unicode字符
背景 最近发现某个数据采集的系统拿下来的数据,有些字段的JSON被莫名截断了,导致后续数据分析的时候解析JSON失败。 类似这样 {title: 你好 或者这样,多了个双引号啥的 {title:你好} 因为数据库是Oracle,起初以为是Oracle这老古董出问题了,结果一番折腾,把每条写入数据的SQL语句都拿出来,看起来里面的JSON格式都没问题。 这也太诡异了吧,看起来没毛病,但就为啥JSON
2023-02-02 · C# · 约 1 分钟
C#实现生成Markdown文档目录树
前言 之前我写了一篇关于C#处理Markdown文档的文章:C#解析Markdown文档,实现替换图片链接操作 算是第一次尝试使用C#处理Markdown文档,然后最近又把博客网站的前台改了一下,目前文章渲染使用Editor.md组件在前端渲染,但这个插件生成的目录树很丑,我魔改了一下换成bootstrap5-treeview组件,好看多了。详见这篇文章:魔改editormd组件,优化ToC渲染效
2022-10-26 · C# · 约 1 分钟
一次小而美的重构:使用 C# 在 Avalonia 中生成真正好看的词云
重构StarBlogPublisher中词云展示功能的过程。作者对比了两个流行的C#词云库,最终选择了功能更强大的Sdcb.WordCloud。文章详细说明了如何使用该库获取词频数据并生成词云图像,并展示了重构前后的效果对比。
2025-04-26 · C# · 约 1 分钟
讨论
共 0 条加载评论中…