NLP学习(六)-词性标注问题

在这里插入图片描述

nltk词性标注词性对照

在这里插入图片描述

1.词性标注器

#词性标注器
text = word_tokenize("今天 的 天气 是 真的 好 苹果")
print(pos_tag(text))

在这里插入图片描述

2.标注语料库

  • 统一标注集合词性
    在这里插入图片描述
#标注语料库
tagged_token = nltk.tag.str2tuple("苹果/NN")#将这种形式的数据转化成元组
tagged_token  #所有的标注语料库都是这种形式的 tagged_token[0]--->词语   tagged_token[1]--->标注信息

#('苹果', 'NN')

#类比英文可以将一段已经标注好的文章来进行分成多个单独的元组列表
sent = '''
The/AT grand/JJ jury/NN commented/VBD on/IN a/AT number/NN of/IN
other/AP topics/NNS ,/, AMONG/IN them/PPO the/AT Atlanta/NP and/CC
Fulton/NP-tl County/NN-tl purchasing/VBG departments/NNS which/WDT it/PP
said/VBD ``/`` ARE/BER well/QL operated/VBN and/CC follow/VB generally/R
accepted/VBN practices/NNS which/WDT inure/VB to/IN the/AT best/JJT
interest/NN of/IN both/ABX governments/NNS ''/'' ./.
 '''
[nltk.tag.str2tuple(t) for t in sent.split()]# 按空格分开

在这里插入图片描述

#每个语料库都有自己的词性标注格式, 可以通过设置tagset来统一简化标注形式(必须是已经构建好的语料库)
print(nltk.corpus.nps_chat.tagged_words())
print(nltk.corpus.conll2000.tagged_words())
print(nltk.corpus.treebank.tagged_words())

print(nltk.corpus.brown.tagged_words(tagset='universal'))
print(nltk.corpus.nps_chat.tagged_words(tagset='universal'))
print(nltk.corpus.conll2000.tagged_words(tagset='universal'))
print(nltk.corpus.treebank.tagged_words(tagset='universal'))

在这里插入图片描述

##一种搜索格式(类比英文)搜索两个连续词之间后一个是一个确定词性的前提下他的前一个词的词性最大可能是什么

from nltk.corpus import brown
brown_news_tagged = brown.tagged_words(categories='news', tagset='universal')#转化成词语/词性的元组形式
print(brown_news_tagged)
tag_fd = nltk.FreqDist(tag for (word, tag) in brown_news_tagged)
tag_fd.most_common()
tag_fd.plot()

#名词
word_tag_pairs = list(nltk.bigrams(brown_news_tagged))#建立前后两个元组之间的连接
nltk.FreqDist(a[1] for (a, b) in word_tag_pairs if b[1] == 'NOUN').most_common()#取出最大可能的组合

在这里插入图片描述

3.自动标注

  • 默认标注器(所有词都默认标注为一种词性,这种方法不好,没什么意义,但是对于一些不知道词性新词来说可以默认标注为频数最多的词性,可以猜猜看…)
#给每个词都进行同样的词性标注(没什么用, 默认标注)
raw = '你 说 巧 不巧 正好 我 今天 也 想 去 打 篮球'#你/r  说/v  巧/a  不巧/a  正好/d  我/r  今天/nt  也/d  想/v  去/v  打/v  篮球/n  
tokens = nltk.word_tokenize(raw)
default_tagger = nltk.DefaultTagger('NN')
default_tagger.tag(tokens)  

在这里插入图片描述

  • 正则表达式标注器(根据特殊后缀对单词进行词性标注)
#正则表达式标注器(不适用于中文, 只适用于英文)
patterns = [
    (r'.*ing$', 'VBG'), # gerunds
    (r'.*ed$', 'VBD'), # simple past
    (r'.*es$', 'VBZ'), # 3rd singular present
    (r'.*ould$', 'MD'), # modals
    (r'.*\'s$', 'NN$'), # possessive nouns
    (r'.*s$', 'NNS'), # plural nouns
    (r'^-?[0-9]+(.[0-9]+)?$', 'CD'), # cardinal numbers
    (r'.*', 'NN') # nouns (default)
]
regexp_tagger = nltk.RegexpTagger(patterns)
regexp_tagger.tag(brown.sents()[3])

在这里插入图片描述

  • 查询标注器(NO)
#查询标注器(找出一个文本中频数最多的100个词的标注, 来对一个新文本中但必须是同类别的文本数据, 因为最常用的词都差不多这样来进行标注,
#但是那些不在100个词内的词语就无法进行正确的标注了   这样的话就需要一个很大的原始标注字典来进行查询了, 也不好)
fd = nltk.FreqDist(brown.words(categories='news'))
cfd = nltk.ConditionalFreqDist(brown.tagged_words(categories='news'))
most_freq_words = fd.most_common()[:100]
likely_tags = dict((word, cfd[word].max()) for (word,freq) in most_freq_words)
baseline_tagger = nltk.UnigramTagger(model=likely_tags)
baseline_tagger.evaluate(brown.tagged_sents(categories='news'))

sent = brown.sents(categories='news')[3]
baseline_tagger.tag(sent)

在这里插入图片描述

4.N-gram标注(基于统计算法的标注)

  • 一元模型标注
#训练集与测试集相同
from nltk.corpus import brown
brown_tagged_sents = brown.tagged_sents(categories='news')
brown_sents = brown.sents(categories='news')
unigram_tagger = nltk.UnigramTagger(brown_tagged_sents)
print(unigram_tagger.tag(brown_sents[2007]))

[('Various', 'JJ'), ('of', 'IN'), ('the', 'AT'), ('apartments', 'NNS'), ('are', 'BER'), ('of', 'IN'), ('the', 'AT'), ('terrace', 'NN'), ('type', 'NN'), (',', ','), ('being', 'BEG'), ('on', 'IN'), ('the', 'AT'), ('ground', 'NN'), ('floor', 'NN'), ('so', 'QL'), ('that', 'CS'), ('entrance', 'NN'), ('is', 'BEZ'), ('direct', 'JJ'), ('.', '.')]

#训练集与测试集不同
# #分离训练与测试数据
#分离训练与测试数据
size = int(len(brown_tagged_sents) * 0.9)
print(size)
train_sents = brown_tagged_sents[:size]
test_sents = brown_tagged_sents[size:]
unigram_tagger = nltk.UnigramTagger(train_sents)
unigram_tagger.evaluate(test_sents)

4160
0.8121200039868434
  • N-gram标注
#考虑前面一个词的词性来对后面的词进行词性标注----最多的是使用二元标注器
bigram_tagger = nltk.BigramTagger(train_sents)
bigram_tagger.tag(brown_sents[2007])
unseen_sent = brown_sents[4203]
bigram_tagger.tag(unseen_sent)
bigram_tagger.evaluate(test_sents)

0.10206319146815508


#组合标注器
t0 = nltk.DefaultTagger('NN')#默认标注
t1 = nltk.UnigramTagger(train_sents, backoff=t0)#一元标注
t2 = nltk.BigramTagger(train_sents, backoff=t1)#二元标注
t2.evaluate(test_sents)

0.8452108043456593

5.标注器的存储与读取

#储存标注器
from pickle import dump
output = open('t2.pkl', 'wb')
dump(t2, output, -1)
output.close()

from pickle import load
input = open('t2.pkl', 'rb')
tagger = load(input)
input.close()

text = """The board's action shows what free enterprise
    is up against in our complex maze of regulatory laws ."""
tokens = text.split()
tagger.tag(tokens)

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值