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 词 to to
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
WH Wh 限定词 who, which, when, what, where, how
. 标点符号 . , ; !

更多

猜你喜欢

转载自blog.csdn.net/qq_37717661/article/details/81323328