2.将 jar 包转译为 .Net 平台的 dll ,然后引用执行
dotnet add package IKVM dotnet add package IKVM.Maven.Sdk或者直接编辑项目文件
<ItemGroup> <PackageReference Include="IKVM" Version="8.6.4" /> <PackageReference Include="IKVM.Maven.Sdk" Version="1.5.5" /> </ItemGroup>然后再项目文件里面添加 maven 依赖,直接从 mvn repository 上复制下来就完事了,非常的方便!给不熟悉 Java 的同学指个路: https://mvnrepository.com/
<ItemGroup> <MavenReference Include="hanlp"> <groupId>com.hankcs</groupId> <artifactId>hanlp</artifactId> <version>portable-1.8.4</version> </MavenReference> </ItemGroup>保存,之后IDE会自动执行操作,会自动下载 iKvm 需要的依赖,各平台的 JDK 和 runtime 之类的,并且会自动从 maven 上把 jar 包下载下来并转译成 .Net 平台的 dll。这个过程需要一段时间,请耐心等待。
dotnet restore dotnet build
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; // 堆代码 duidaima.com 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); } }测试时直接调用 Recognize 方法即可。