blog5 自然语言处理NLTK的词性标注

2021SC@SDUSC

关键词提取的时候首先用到的就是用NLTK包对单词进行过滤

先用NLTK包的pos_tag方法(part-of-speech tagging )来对单词的词性进行标记,标记后的结果是二元数组格式。之后从这个二元数列中挑出我们所有需要的tag,存放进一个二元数列。

首先,引入包

import nltk

假设我们处理的是love和dislike这两个词,选择任意一段文本,建立他们的token

words=word_tokenize(‘He dislike study on monday. He love math.)

挑选出需要的词性,词性列表:

CD cardinaldigit 纯数 基数
EX existentialthere (like:“there is”… think of it like “thereexists”) 存在句;存现句
CC coordinatingconjunction 并列连词
FW foreignword 外来语;外来词;外文原词
IN preposition/subordinating conjunction介词/从属连词;主从连词;从属连接词
DT determiner 限定词(置于名词前起限定作用,如 the、some、my 等)
JJ adjective ‘big’ 形容词
JJR adjective, comparative ‘bigger’ (形容词或副词的)比较级形式
JJS adjective, superlative ‘biggest’ (形容词或副词的)最高级
LS listmarker 1)
PDT predeterminer ‘all the kids’ 前位限定词
NN noun, singular ‘desk’ 名词单数形式
NNS nounplural ‘desks’ 名词复数形式
NNP propernoun, singular ‘Harrison’ 专有名词
MD modal (could, will) 形态的,形式的 , 语气的;情态的
NNPS proper noun, plural ‘Americans’ 专有名词复数形式
POS possessiveending parent’s 属有词 结束语
PRP$ possessive pronoun my, his, hers 物主代词
PRP personalpronoun I, he, she 人称代词
RB adverb very, silently, 副词 非常 静静地
RBS adverb,superlative best (形容词或副词的)最高级
RBR adverb,comparative better (形容词或副词的)比较级形式
RP particle give up 小品词(与动词构成短语动词的副词或介词)
TO to go ‘to’ the store.
UH interjection errrrrrrrm 感叹词;感叹语
VB verb, baseform take 动词
VBN verb, pastparticiple taken 动词 过去分词
VBD verb, pasttense took 动词 过去时;过去式
VBG verb,gerund/present participle taking 动词 动名词/现在分词
VBZ verb, 3rdperson sing. present takes 动词 第三人称
VBP verb,sing. present, non-3d take 动词 现在
WDT wh-determiner which 限定词(置于名词前起限定作用,如 the、some、my 等)
WP wh-pronoun who, what 代词(代替名词或名词词组的单词)
WP $ possessivewh-pronoun whose 所有格;属有词
WRB wh-abverb where, when 副词
对于关键词提取,我们一般需要的是名词单数、名词复数、专有名词等,挑选出合适的tags,并把pos_tag方法创建的词和对应词性保存在pos_tags数列。

tags = set(['NN', 'NNS', 'NNP']);
p = p.split()             					# 把文本分割
    if p[0] in pre_word:                   
        p = p[1:]                           
    b = ' '.join(p)                         
    tag = nltk.pos_tag(p) 					# 对分割后的词进行词性标注

之后创建空数组ret,遍历pos_tags,把我们需要的词性的数组保存到ret[ ]

ret = []
for word,pos in pos_tags:
        if (pos in tags):
            ret.append(word)
 return ' '.join(ret)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值