Python自然语言处理-学习笔记(5) —— 标注词汇

用nltk做词性标注

先上函数~~~

import nltk
函数用法解释
pos_tag(text)词性标注器,对分词后的文档做词性标注
nltk.tag.str2tuple(word+’/’+tag)手动标注,返回(单词,标注)
corpus.tagged_words()语料库(brown)的单词标注接口,返回(单词,标注)列表
corpus.tagged_words()类似于单词标注,将已标注的词划分成句子
tagger = DefaultTagger(‘NN’)
tagger.tag(tokens)
tagger.evaluate(tagged_sents)
默认标注器
给分词文本默认标注
评估性能
patterns = [(regexp,tag)]
tagger = RegexpTagger(patterns)
tagger.tag(tokens)
tagger.evalute(tagged_sents)
正则表达式标注器
UnigramTagger(model=likely_tag_dict

,backoff=nltk.DefaultTagger(tag))
查询标注器(一元标注器),如果查询不到则设定默认标注backoff
BigramTagger(tag,backof)二元标注器
TrigramTagger(tag,backoff)三元标注器
ConfusionMatrix(label_tag_list,test_tag_list)混淆矩阵,便捷查看标注错误数
规律总结

1.名词出现在限定词和形容词(包括数词)之后
2.过去分词一般跟在助动词(如have)之后
3.形容词一般在名词前,副词一般在动词前

关于n-gram标注器

一般形式 : key = (t1,t2,..tn-1,w1) , value = tn
数据稀疏问题:当n越大时,上下文(即key)的特异性增加,要标注的数据中包含的训练数据不存在的上下文不存在的概率越大,只能默认标注。所以n-gram标注器不应考虑跨越句子边界的上下文。
例如:

cfd = ConditionalFreqDist(
    ((a[1],b[1],c[0]),c[1])
    for sent in news_tagged_sents  #不考虑跨越句子边界的上下文
    for a,b,c in trigrams(sent))
组合标注器

解决精度和覆盖范围之间的权衡问题——尽可能使用更精确的问题,但在很多时候逊于范围更广的问题。
eg:bigram标注器->unigram标注器->默认标注器,从中依次寻找标注。

t0 = DefaultTagger('NN')
t1 = UnigramTagger(train_sents,backoff=t0)  #backoff称为回退技术
t2 = UnigramTagger(train_sents,backoff=t1)
t2.evaluate(test_sents)
存储标注器
import pickle
f = open('t2.pkl','wb')
pickle.dump(t2,f)
f.close()

f = open('t2.pkl','rb')
_t2 = pickle.load(f)
t2.evaluate(test_sents)

详细代码

简化的标记集
标记含义例子
JJ形容词new, good, high, special, big, local
ADV动词really, already, still, early, now
CNJ连词and, or, but, if, while, although
DET限定词the, a, some, most, every, no
EX存在量词there, there’s
FW外来词dolce, ersatz, esprit, quo, maitre
MOD情态动词will, can, would, may, must, should
NN名词year, home, costs, time, education
NN$名词所有格Rachel’s
NNS名词复数
NP专有名词Alison, Africa, April, Washington
CD数词twenty-four, fourth, 1991, 14:24
PRO代词he, their, her, its, my, I, us
IN介词on, of, at, with, by, into, under
TO词 toto
UH感叹词ah, bang, ha, whee, hmpf, oops
VB动词基本形式is, has, get, do, make, see, run
VBD动词过去式said, took, told, made, asked
VBG动词现在分词making, going, playing, working
VBN过去分词given, taken, begun, sung
VBZ动词第三人称单数gives,studies,goes,has
WHWh 限定词who, which, when, what, where, how
.标点符号. , ; !

更多

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值