使用ICTCLAS2015进行分词

使用ICTCLAS2015进行分词

在今年的Imagine Cup中使用到了语义分析的部分,其中需要分词作为基础,我是用的是中科院的ICTCLA2015,本篇博客我来讲讲如何使用ICTCLAS2015进行分词

ICTCLAS2015简介

中文词法分析是中文信息处理的基础与关键。中国科学院计算技术研究所在多年研究工作积累的基础上,研制出了汉语词法分析系统ICTCLAS(Institute of Computing Technology, Chinese Lexical Analysis System),主要功能包括中文分词;词性标注;命名实体识别;新词识别;同时支持用户词典。先后精心打造五年,内核升级6次,目前已经升级到了ICTCLAS3.0。ICTCLAS3.0分词速度单机996KB/s,分词精度98.45%,API不超过200KB,各种词典数据压缩后不到3M,,是当前世界上最好的汉语词法分析器。

下载地址

使用ICTCLAS2015进行开发本文所采用开发平台开发实例准备

复制Data文件夹和NLPIR.dll至开发目录

下载JNA类库, jna-platform-4.1.0.jar

使用JNA调用C++接口 {//建立实例CLibrary Instance = (CLibrary)Native.loadLibrary(“./libs/NLPIR”, CLibrary.class);(byte[] sDataPath, int encoding,byte[] sLicenceCode);//段落处理public String NLPIR_ParagraphProcess(String sSrc, int bPOSTagged);//获取关键词public String NLPIR_GetKeyWords(String sLine, int nMaxKeyLimit,boolean bWeightOut);();(String sSourceFilename,String sResultFilename,int bPOStagged);(String sFilename,Boolean bOverwrite);(String sWords);}对一段文字进行分词,返回标注词性的分词结果 /*** 对一段文字进行分词,返回标注词性的分词结果** @param fileName* @return words* @throws Exception*/public static String[] Segment(String fileName) throws Exception{//保存分词结果String result[]={“”,””};String sourceString = “”;//从文件中读入文本try {String encoding=”UTF-8″;File file=new File(fileName);if(file.isFile() && file.exists()){//判断文件是否存在String temp = null;InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);BufferedReader bufferedReader = new BufferedReader(read);while((temp = bufferedReader.readLine()) != null){sourceString += temp;}read.close();}else{System.out.println(“找不到指定的文件”);}} catch (Exception e) {System.out.println(“读取文件内容出错”);e.printStackTrace();}//进行分词,对NLPIR初始化String argu = “”;String system_charset = “UTF-8”;int charset_type = 1;int init_flag = CLibrary.Instance.NLPIR_Init(argu.getBytes(system_charset), charset_type, “1”.getBytes(system_charset));AddUserWords(“dic/dic.txt”);if(0 == init_flag){System.out.println(“init fail!”);return null;}//保存分词结果String nativeBytes = null;//保存关键词String nativeByte = null;try{//分词nativeBytes = CLibrary.Instance.NLPIR_ParagraphProcess(sourceString, 1);//获取关键词nativeByte = CLibrary.Instance.NLPIR_GetKeyWords(sourceString, 5, true);}catch(Exception e){e.printStackTrace();}result[0] = nativeBytes;result[1] = nativeByte;//返回分词结果return result;}添加用户词典并进行词性标注 /*** 添加用户词典并进行词性标注* @param filePath*/(String filePath){try{String encoding = “UTF-8”;File file = new File(filePath);if(file.isFile()&&file.exists()){InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);BufferedReader bufferReader = new BufferedReader(read);String lineText = “”;while((lineText = bufferReader.readLine()) != null){CLibrary.Instance.NLPIR_AddUserWord(lineText);}}else{System.out.println(“未找到文件!”);}}catch(Exception e){e.printStackTrace();}}

当一个人真正觉悟的一刻,他放弃追寻外在世界的财富,而开始追寻他内心世界的真正财富

使用ICTCLAS2015进行分词

相关文章:

你感兴趣的文章:

标签云: