Java通过JNI调用ICTCLAS中文分词包的方法

1)从网络上下载ICTCLAS分词包的Java JNI版本

ICTCLAS官方网站为:http://ictclas.org/

ICTCLAS2011_Linux_32_jni下载地址为:http://ictclas.org/down/50/ICTCLAS50_Windows_32_JNI.rar

 

2)新建Java项目,类代码为:

public class Exec {
        public static void main(String[] args) {
                try {
                        ICTCLAS.I3S.AC.ICTCLAS50 testICTCLAS50 = new ICTCLAS.I3S.AC.ICTCLAS50();
                        // 分词所需库的路径
                        String argu = ".";
                        // 初始化
                        if (testICTCLAS50.ICTCLAS_Init(argu.getBytes("GB2312")) == false) {
                                System.out.println("Init Fail!");
                                return;
                        } else {
                                System.out.println("Init Succeed!");
                        }

                        String sInput = "我们今天试一试Java的JNI调用方法。";
                        byte nativeBytes[] = testICTCLAS50.ICTCLAS_ParagraphProcess(sInput
                                        .getBytes("GB2312"), 0, 1);
                        System.out.println(nativeBytes.length);
                        String nativeStr = new String(nativeBytes, 0, nativeBytes.length,
                                        "GB2312");
                        System.out.println("The result is :" + nativeStr);
                        testICTCLAS50.ICTCLAS_Exit();
                } catch (Exception ex) {
                }
        }
}

 


3)将压缩包中API目录中的源代码,即位于包ICTCLAS\I3S\AC中的ICTCLAS50加入项目中

 

4)将压缩包中API目录中其他所有文件直接放入当前项目根目录

 

即可运行

 

输入为:

Init Succeed!
68
The result is :我们/rr 今天/t 试/v 一/m 试/v Java/x 的/ude1 JNI/x 调用方法/n 。/wj


 

你可能感兴趣的:(java,exception,windows,String,api,jni)