Chinese Academy of Sciences word breaker (NLPIR) Java Easy Tutorial (2014-11-12)

Source: Internet
Author: User

This document is mainly obtained from the official documents, the API has been translated, and based on the personal experience of the use of some supplement, given the limited capacity, low level, there must be a lot of shortcomings in the document, please forgive me.

If you want to use Java for Nlpir, you need to call Nlpir's function through JNA. Here's a concrete example of how to do this:

First, create a new interface in the Java project, inherit the JNA library interface, and define the implementation of the NLPIR series of functions in the interface (see header file NLPIR.h in the Include folder for specific APIs).

Importcom.sun.jna.Library; Public InterfaceClibraryextendslibrary{//Initialize     Public intNlpir_init (String Sdatapath,intencoding, String slicencecode); //word breaker on a string     PublicString Nlpir_paragraphprocess (String sSrc,intbpostagged); //word breaker for TXT file contents     Public DoubleNlpir_fileprocess (String ssourcefilename,string sresultfilename,intbpostagged); //extracting keywords from a string     PublicString Nlpir_getkeywords (String sline,intNmaxkeylimit,Booleanbweightout); //extracting keywords from txt files     PublicString Nlpir_getfilekeywords (String sline,intNmaxkeylimit,Booleanbweightout); //Add a single user dictionary     Public intNlpir_adduserword (String sWord); //Delete a single user dictionary     Public intNlpir_delusrword (String sWord); //Import a user dictionary from a txt file     Public intnlpir_importuserdict (String sfilename); //Saving a user dictionary to a hard disk     Public intNlpir_savetheusrdic (); //get new words from a string     PublicString Nlpir_getnewwords (String sline,intNmaxkeylimit,Booleanbweightout); //get new words from TXT file     PublicString Nlpir_getfilenewwords (String stextfile,intNmaxkeylimit,Booleanbweightout); //gets the thumbprint value of a string     Public Longnlpir_fingerprint (String sline); //set the POS map to use     Public intNlpir_setposmap (intNposmap); //get the error log     PublicString nlpir_getlasterrormsg (); //Exit     Public voidNlpir_exit (); }

After you create a new class and instantiate the above interface, you can invoke the Nlpir functions.

ImportJava.math.BigDecimal;ImportwordSegmentation.study.CLibrary;Importcom.sun.jna.Native; Public classDemo { Public Static voidMain (string[] args)throwsException {//InitializeClibrary instance = (clibrary) native.loadlibrary (System.getproperty ("User.dir") + "\\source\\NLPIR", clibrary.class); intInit_flag = instance. Nlpir_init ("", 1, "0"); String resultstring=NULL; if(0 = =Init_flag) {resultstring=instance.            Nlpir_getlasterrormsg (); System.err.println ("Initialization failed!" \ n "+resultstring); return; } String sInput= "Hey ~ that diamond ring size is too poor, before heavy light, Zoquan right narrow, he put on to very uncomfortable," + "all night insomnia will implicate me, he is a monkey, but you can not do to him ah, the official know will say I abuse animals," + "talking about the King Kong Circle, ah ~ Last year I met a blacksmith in Chen Cun, he is exquisite handmade, price and fair, fair trade, "+" simply I introduce you to customize a bar! "; Try{resultstring= instance. Nlpir_paragraphprocess (SInput, 1); System.out.println ("Participle result is: \ n" +resultstring); Instance. Nlpir_adduserword ("Diamond Ring"); Instance. Nlpir_adduserword ("Zoquan Right Narrow"); Resultstring= instance. Nlpir_paragraphprocess (SInput, 1); System.out.println ("Add user dictionary after word breaker result: \ n" +resultstring); Instance. Nlpir_delusrword ("Zoquan Right Narrow"); Resultstring= instance. Nlpir_paragraphprocess (SInput, 1); System.out.println ("Delete User dictionary after word result is: \ n" +resultstring); Instance. Nlpir_importuserdict (System.getproperty ("User.dir") + "\\source\\userdic.txt"); Resultstring= instance. Nlpir_paragraphprocess (SInput, 1); System.out.println ("After importing user dictionary files, the word-breaker result is: \ n" +resultstring); Resultstring= instance. Nlpir_getkeywords (sinput,10,false); System.out.println ("Keywords extracted from paragraph: \ n" +resultstring); Resultstring= instance. Nlpir_getnewwords (SInput, 10,false); System.out.println ("New word extraction result: \ n" +resultstring); Double D= instance. Nlpir_fileprocess ("D:\\1.txt", "D:\\2.txt", 1); System.out.println ("The speed at which word breakers are run on file content is:" ); if(D.isinfinite ()) System.out.println ("No results"); Else{BigDecimal b=NewBigDecimal (d); System.out.println (B.divide (NewBigDecimal (+), 2, bigdecimal.round_half_up) + "seconds"); } resultstring= instance. Nlpir_getfilekeywords ("D:\\3.txt", 10,false); System.out.println ("The result of extracting a keyword from a file is: \ n" +resultstring); Instance.        Nlpir_exit (); } Catch(Exception e) {System.out.println ("Error message:");        E.printstacktrace (); }    }

Chinese Academy of Sciences word breaker (NLPIR) Java Easy Tutorial (2014-11-12)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.