本文共 3727 字,大约阅读时间需要 12 分钟。
对于分词,目前有很多开源的程序,包括hanlp、jieba、哈工大分词等。为了确定哪种分词结果比较好,通常有两种方式,一种是调用接口,对特定的句子分词,通过感觉对分词结果进行对比,但这种分词结果却带有了很大的主观色彩。在网上也是博客在介绍。另一种则通过测试集对分词结果与标准的分词进行分析,得出准确率、召回率等。
开放的测试集选取
一般测试集产生的来源主要源于竞赛,但是,我在搜索过程中,只找到了一个,,虽然竞赛是2005年公开的,但是却没找到别的,大家如果有其他更好的,麻烦留言交流。 接下来我们就以这个测试集对分词结果进行分析。下载数据集
,大家尽量跳转到进行下载。目录结构
doc nshort_seg.out testing training gold README scripts
其中:
评分方法
script文件夹中,包含了score脚本,脚本执行的时候需要三个参数,训练集、标准分词、自己分词。 执行脚本如下,可根据自己的需求更换对应的参数perl scripts/score gold/pku_training_words.utf8 gold/pku_test_gold.utf8 gold/pku_test.out
结果展示
结果中包含了单个文本片段,以及最后总的评分结果,这里我们仅展示总的评分结果。=== TOTAL INSERTIONS: 993=== TOTAL DELETIONS: 7143=== TOTAL SUBSTITUTIONS: 7617=== TOTAL NCHANGE: 15753=== TOTAL TRUE WORD COUNT: 104372=== TOTAL TEST WORD COUNT: 98222=== TOTAL TRUE WORDS RECALL: 0.859=== TOTAL TEST WORDS PRECISION: 0.912=== F MEASURE: 0.885=== OOV Rate: 0.058=== OOV Recall Rate: 0.708=== IV Recall Rate: 0.868
在结果中,RECALL、PRECISION、F MEASURE:分别为召回率、精度和F值,值越高越好。
部分代码片段
为了方便开发测试,以hanlp为例,贴出部分生成segment文件的代码public class Util { /** * 将wordlist转化为string * @param sentence segment生成的List* @param posflag 是否输出标签,在测试时输入为false * @return */ public static String wordlist2String(List sentence, boolean posflag){ StringBuffer result = new StringBuffer(); if(sentence.size()>=1){ for(Term w:sentence){ if(w.getWord()!=null && !" ".equals(w.getWord())){//去掉begin和end result.append(w.getWord()); if(true==posflag){ result.append("/"+w.getNature().toString()); } if (w.getWord().equals("\r\n")){ continue; } result.append(" "); } } } return result.toString(); } /** * 读取根路径下的文件 * * @param filePath 文件路径 * * @return byte[] * */ public static String readSystemFileToByte(String filePath) { InputStream inStream = null; byte data[] = null; try { inStream = new FileInputStream(filePath);; ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = inStream.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } data = swapStream.toByteArray(); inStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String dataString = null; try { dataString = new String(data, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return dataString; } /** * 将分词结果输出到文件内容 * @param content 分析结果 * @param path 保存的路径 */ public static void write(String content, String path){ try{ BufferedWriter bw = new BufferedWriter(new FileWriter(path)); bw.write(content); bw.newLine(); bw.close(); }catch (Exception e){ System.out.println(e.getMessage()); } System.out.println("保存成功"); }}
public void test(){String inputPath = "";String outPath="";//读取文件内容String content = Transform.readSystemFileToByte(inputPath);//分词并保存结果String outContent = Transform.wordlist2String(HanLP.segment(content),false); Transform.write(outContent,outPath);}
转载地址:http://vfktn.baihongyu.com/