Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call HanLP in CSharp

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to call HanLP in CSharp". In daily operation, I believe many people have doubts about how to call HanLP in CSharp. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how to call HanLP in CSharp". Next, please follow the editor to study!

Introduction to IKVM

IKVM.NET is an open source Java virtual machine that can convert the hanlp.jar class library into dll and be called directly by C # programs, or when JVM allows hanlp.jar to run on the .NET platform. Please download IKVM yourself. Ikvm-7.2.4630.5 is used in this article.

Introduction to HanLP

HanLP is an open source Java toolkit dedicated to popularizing NLP technology to the production environment, supporting Chinese word segmentation (N-shortest path segmentation, CRF word segmentation, index segmentation, user-defined dictionary, part of speech tagging), named entity recognition (Chinese name, transliteration, Japanese name, place name, entity name recognition), keyword extraction, automatic summarization, phrase extraction, pinyin conversion, simple and complex conversion, text recommendation. Dependency parsing (MaxEnt dependency parsing, neural network dependency parsing).

HanLP consists of three parts: the class library hanlp.jar package, the model data package and the configuration file hanlp.properties. Please go to the project home page to download the latest version: https://github.com/hankcs/HanLP/releases

After downloading, you need to edit the root in the first line of the configuration file to point to the parent directory of data, as detailed in the documentation.

Run hanlp.jar preparation in IKVM

In the case of Native JVM, we call hanlp.jar like this, create a new directory (assuming C:\ hanlp), and put hanlp.jar and hanlp.properties in it:

Then cd to this directory, a command line:

Java-cp.; hanlp-1.2.7.jar com.hankcs.hanlp.summary.TextRankSentence

It will run successfully:

[infinite algorithms arise because of uncertain definition of termination conditions, such algorithms terminate in a limited time, finite uncertain algorithms]

Here classpath is specified as the current directory with the-cp option so that HanLP can find hanlp.properties.

In addition, in the new version of HanLP, all main methods have been removed, so exceptions to the main method may not be reported. The new version of users do not have to worry about the command line call, just write code to call it.

IKVM Command Line

To use IKVM, simply replace java with ikvm:

C:\ ikvm-7.2.4630.5\ bin\ ikvm- cp.; hanlp-1.2.7.jar com.hankcs.hanlp.summary.TextRankSentence

The effect is the same:

Call HanLP in C # to convert hanlp.jar to hanlp.dll

CSharp cannot call the jar package directly, it needs to be converted to dll. IKVM provides a conversion tool, ikvmc, which can be used as follows:

C:\ hanlp > C:\ ikvm-7.2.4630.5\ bin\ ikvmc-target:library C:\ hanlp\ hanlp-1.2.7.jar

Ikvmc outputs a message:

IKVM.NET Compiler version 7.2.4630.5Copyright (C) 2002-2012 Jeroen Frijters http://www.ikvm.net/ note IKVMC0002: Output file is "hanlp-1.2.7.dll"

Hanlp-1.2.7.dll is also generated in the C:\ hanlp directory.

Using hanlp.dll in Visual Studio

First create a new CSharp project:

Then reference the two dll IKVM.OpenJDK.Core.dll in the hanlp.dll and ikvm-7.2.4630.5\ bin directories that you just generated:

When you are sure, you can see these two dll in solution Explorer:

Double-clicking hanlp also takes you to the class view:

Then you can happily write code to call HanLP:

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using com.hankcs.hanlp; namespace HanLPSharp {class Program {static void Main (string [] args) {java.lang.System.getProperties () .setProperty ("java.class.path", "C:\\ hanlp"); Console.WriteLine ("Hello, welcome to call HanLP's API in CSharp!")) ; Console.ReadKey ();}

Explain,

Java.lang.System.getProperties () .setProperty ("java.class.path", "C:\\ hanlp")

The purpose of this sentence is to set classpath to the directory where hanlp.properties is located. Please feel free to adjust it according to the situation.

Compile and run, the effect is as follows:

This shows that HanLP works well in CSharp.

More rich API

Please refer to HanLP's project home page: https://github.com/hankcs/HanLP

Reference

Http://stackoverflow.com/questions/3466698/where-does-ikvm-get-its-classpath-from

Http://www.ikvm.net/uses.html

At this point, the study on "how to call HanLP in CSharp" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report