CRF++词性标注

2016-02-28 分类:NLP 阅读(5558) 评论(19) 

训练和测试的语料都是人民日报98年标注语料,训练和测试比例是10:1,直接通过CRF++标注词性的准确率:0.933882。特征有一千多万个,训练时间比较长。机器cpu是48核,通过crf++,指定并线数量 -p为40,训练了大概七个小时才结束。

语料库、生成训练数据的python脚本、训练日志、模型、计算准确率脚本都上传到网盘,可以直接下载:戳我下载 CRF++词性标注,程序在centos6.5+python2.7下面运行通过,如果在win下或者ubuntu下可能会有异常,通常都是编码、路径规范等小问题,通过逐行debug脚本应该很容易找到问题,同时要确定crf++在自己机器本身编译没有问题,下面说一下每一步的过程。

文章目录 [展开]

生成训练和测试数据

生成训练和测试数据脚本:get_post_train_test_data.py,执行过程中会打印出来一些调试信息。

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#coding=utf8
 
import sys
 
#home_dir = "D:/source/NLP/people_daily//"
 
home_dir = "./"
 
def saveDataFile(trainobj,testobj,isTest,word,handle):
    if isTest:
        saveTrainFile(testobj,word,handle)
    else:
        saveTrainFile(trainobj,word,handle)
 
def saveTrainFile(fiobj,word,handle):
    if len(word) > 0 and  word != "。" and word != ",":
        fiobj.write(word + '\t' + handle  + '\n')
    else:
        fiobj.write('\n')
 
def convertTag():    
    fiobj    = open( home_dir + 'people-daily.txt','r')
    trainobj = open( home_dir +'train.data','w' )
    testobj  = open( home_dir  +'test.data','w')
 
    arr = fiobj.readlines()
    i = 0
    for a in sys.stdin:
        i += 1
        a = a.strip('\r\n\t ')
        if a=="":continue
        words = a.split(" ")
        test = False
        if i % 10 == 0:
            test = True
        for word in words[1:]:
            print "---->", word
            word = word.strip('\t ')
            if len(word) > 0:        
                i1 = word.find('[')
            if i1 >= 0:
                word = word[i1+1:]
            i2 = word.find(']')
            if i2 > 0:
                w = word[:i2]
            word_hand = word.split('/')
            print "----",word
            w,h = word_hand
            #print w,h
            if h == 'nr':    #ren min
                #print 'NR',w
                if w.find('·') >= 0:
                    tmpArr = w.split('·')
                    for tmp in tmpArr:
                        saveDataFile(trainobj,testobj,test,tmp,h)
                    continue
            saveDataFile(trainobj,testobj,test,w,h)
        saveDataFile(trainobj, testobj, test,"","")
            
    trainobj.flush()
    testobj.flush()
 
if __name__ == '__main__':    
    convertTag()

执行训练和测试

设置模板为:

 
1
2
3
4
5
6
7
8
# Unigram
U00:%x[-2,0]
U01:%x[-1,0]
U02:%x[0,0]
U03:%x[1,0]
U04:%x[2,0]
U05:%x[-1,0]/%x[0,0]
U06:%x[0,0]/%x[1,0]

训练的时候的-p参数根据自己机器情况设置

 
1
2
crf_learn -f 3 -p 4 -c 4.0 template train.data model > train.rst  
crf_test -m model test.data > test.rst

计算准确率

通过命令:python clc_f.py  test.rst 执行python脚本,clc_f.py中的具体程序:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import sys
 
if __name__=="__main__":
    try:
        file = open(sys.argv[1], "r")
    except:
        print "result file is not specified, or open failed!"
        sys.exit()
    wc = 0
    wc_of_test = 0
    wc_of_gold = 0
    wc_of_correct = 0
    flag = True
    
    for l in file:
        if l=='\n': continue
    
        _, g, r = l.strip().split()
    
        if r != g:
            flag = False
   wc += 1
 
        if flag:
            wc_of_correct +=1
        flag = True
    
 
    print "WordCount from result:", wc
    print "WordCount of correct post :", wc_of_correct
            
    #准确率
    P = wc_of_correct/float(wc)
    
    print "准确率:%f" % (P)

实验结果

 

转:CRF++词性标注的更多相关文章

  1. 隐马尔可夫(HMM)/感知机/条件随机场(CRF)----词性标注

    笔记转载于GitHub项目:https://github.com/NLP-LOVE/Introduction-NLP 7. 词性标注 7.1 词性标注概述 什么是词性 在语言学上,词性(Par-Of- ...

  2. NLP —— 图模型(二)条件随机场(Conditional random field,CRF)

    本文简单整理了以下内容: (一)马尔可夫随机场(Markov random field,无向图模型)简单回顾 (二)条件随机场(Conditional random field,CRF) 这篇写的非常 ...

  3. Hanlp分词之CRF中文词法分析详解

    这是另一套基于CRF的词法分析系统,类似感知机词法分析器,提供了完善的训练与分析接口. CRF的效果比感知机稍好一些,然而训练速度较慢,也不支持在线学习. 默认模型训练自OpenCorpus/pku9 ...

  4. 条件随机场(CRF)理论及应用

    http://x-algo.cn/index.php/2016/02/15/conditional-random-field-crf-theory-and-implementation/ 条件随机场( ...

  5. Hanlp等七种优秀的开源中文分词库推荐

    Hanlp等七种优秀的开源中文分词库推荐 中文分词是中文文本处理的基础步骤,也是中文人机自然语言交互的基础模块.由于中文句子中没有词的界限,因此在进行中文自然语言处理时,通常需要先进行分词. 纵观整个 ...

  6. 第四期coding_group笔记_用CRF实现分词-词性标注

    一.背景知识 1.1 什么是分词? NLP的基础任务分为三个部分,词法分析.句法分析和语义分析,其中词法分析中有一种方法叫Tokenization,对汉字以字为单位进行处理叫做分词. Example ...

  7. 条件随机场(CRF) - 1 - 简介(转载)

    转载自:http://www.68idc.cn/help/jiabenmake/qita/20160530618222.html 首先我们先弄懂什么是"条件随机场",然后再探索其详 ...

  8. CRF条件随机场简介

    CRF(Conditional Random Field) 条件随机场是近几年自然语言处理领域常用的算法之一,常用于句法分析.命名实体识别.词性标注等.在我看来,CRF就像一个反向的隐马尔可夫模型(H ...

  9. 条件随机场CRF简介

    http://blog.csdn.net/xmdxcsj/article/details/48790317 Crf模型 1.   定义 一阶(只考虑y前面的一个)线性条件随机场: 相比于最大熵模型的输 ...

随机推荐

  1. THE TOOLS TO MANAGE YOUR DATA ACROSS CLOUDS

    http://blog.grexit.com/manage-data-across-clouds/ That the average small business uses a cloud servi ...

  2. D3D9 effect (hlsl)(转)

      转:http://blog.csdn.net/leonwei/article/details/8212800 effect其实整合了shader和render state的控制两大部分内容 9.1 ...

  3. 蜗牛—ORACLE基础之学习(二)

    如何创建一个表,这个表和还有一个表的结构一样但没有数据是个空表,旧表的数据也插入的 create table newtable as select * from oldtable 清空一个表内的数据 ...

  4. After 2 years, I have finally solved my "Slow Hyper-V Guest Network Performance" issue. I am ecstatic.

    Edit - It should be known that I was initially researching this issue back in 2012 and the solution ...

  5. 华为机试正式版(西安c/c++/java),今天下午去机试的题目,新奇出炉了!

    下面题目都是回顾的.题目都非常easy, 大家有些基础就能够參加!(语言能够是c/c++.也能够是java的) 题目一(60分): 字符串操作. 将小写转换成大写, 将大写转化为小写, 数字的不做转换 ...

  6. SQLServer2008:在查看表记录或者修改存储过程时出现错误。错误消息为: 目录名无效

    登陆数据库后,右键打开表提示:目录名无效,执行SQL语句也提示有错误,本来想重装的这个肯定能解决,但是这个方法真的不视为上上策啊,于是在网上找到了这个解决办法,还真是立即见效啊!分享给大家,希望有帮助 ...

  7. ios 向sqlite数据库插入和读取图片数据

    向sqlite数据库插入和读取图片数据 (for ios) 假定数据库中存在表 test_table(name,image), 下面代码将图片文件test.png的二进制数据写到sqlite数据库: ...

  8. Netty Pipeline、channel、Context之间的数据流向

    以下所绘制图形均基于Netty4.0.28版本. 一.connect(outbound类型事件)  当用户调用channel的connect时,会发起一个outbound类型的事件,该事件将在pipe ...

  9. WordPress主题开发:循环代码

    have_posts() 有没有文章信息 if...else <?php if( have_posts() ) : while( have_posts() ) : the_post(); ?&g ...

  10. 监听Listview的滚动状态,是否滚动到了顶部或底部

    /** * @author:Jack Tony * @description : 监听listview的滑动状态,如果到了顶部就刷新数据 * @date :2015年2月9日 */ private c ...