博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
中文分词评测方法
阅读量:3787 次
发布时间:2019-05-22

本文共 3727 字,大约阅读时间需要 12 分钟。

目录

中文分词评测方法

对于分词,目前有很多开源的程序,包括hanlp、jieba、哈工大分词等。为了确定哪种分词结果比较好,通常有两种方式,一种是调用接口,对特定的句子分词,通过感觉对分词结果进行对比,但这种分词结果却带有了很大的主观色彩。在网上也是博客在介绍。另一种则通过测试集对分词结果与标准的分词进行分析,得出准确率、召回率等。

步骤

  1. 开放的测试集选取

    一般测试集产生的来源主要源于竞赛,但是,我在搜索过程中,只找到了一个,,虽然竞赛是2005年公开的,但是却没找到别的,大家如果有其他更好的,麻烦留言交流。
    接下来我们就以这个测试集对分词结果进行分析。

  2. 下载数据集

    ,大家尽量跳转到进行下载。

  3. 目录结构

    doc  nshort_seg.out  testing  training  gold  README   scripts

    其中:

    • gold: 包含测试数据和训练数据单词列表。
    • scripts: 对比分析效果的脚本。
    • testing: 里面包含了未分词的文本,其标准分词在gold的目录的对应文件中。
    • training: 包含了训练数据集。
    • doc: 包含了竞赛的说明文档。
  4. 评分方法

    script文件夹中,包含了score脚本,脚本执行的时候需要三个参数,训练集、标准分词、自己分词。
    执行脚本如下,可根据自己的需求更换对应的参数

    perl scripts/score gold/pku_training_words.utf8 gold/pku_test_gold.utf8 gold/pku_test.out
  5. 结果展示

    结果中包含了单个文本片段,以及最后总的评分结果,这里我们仅展示总的评分结果。

    === 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值,值越高越好。

  6. 部分代码片段

    为了方便开发测试,以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/

你可能感兴趣的文章
格网编码查询方案在项目运用上的进一步探索
查看>>
BUAA-OO-2019 第三单元总结
查看>>
Matlab策略模式
查看>>
架构整洁之道
查看>>
支付渠道路由系统进化史
查看>>
行为型模式:解释器模式
查看>>
深入理解设计模式(22):享元模式
查看>>
spring boot
查看>>
Angular框架
查看>>
行为型模式:模板方法
查看>>
spring cloud之Feign的使用
查看>>
Codeforces Round #617 (Div. 3) String Coloring(E1.E2)
查看>>
LeetCode刷题 --杂篇 --数组,链表,栈,队列
查看>>
840. 模拟哈希表(模板)
查看>>
《算法》笔记 17 - 数据压缩
查看>>
Qt Installer Framework翻译(5-2)
查看>>
Java+Selenium+Testng自动化测试学习(三)— 断言
查看>>
PAT乙级1012
查看>>
银行业务队列简单模拟(队列queue)
查看>>
MySql中的数据查询语言(DQL)三:连接查询
查看>>