python中如何判断词性,用python的NLTK计算动词,名词和其他词性

本文介绍如何利用Python的nltk库对文本进行词性标注,并统计动词、名词等词性的出现次数。通过pos_tag方法获取词性标签,结合Counter或defaultdict计算词性比例。
摘要由CSDN通过智能技术生成

I have multiple texts and I would like to create profiles of them based on their usage of various parts of speech, like nouns and verbs. Basially, I need to count how many times each part of speech is used.

I have tagged the text but am not sure how to go further:

tokens = nltk.word_tokenize(text.lower())

text = nltk.Text(tokens)

tags = nltk.pos_tag(text)

How can I save the counts for each part of speech into a variable?

解决方案

The pos_tag method gives you back a list of (token, tag) pairs:

tagged = [('the', 'DT'), ('dog', 'NN'), ('sees', 'VB'), ('the', 'DT'), ('cat', 'NN')]

If you are using Python 2.7 or later, then you can do it simply with:

>>> from collections import Counter

>>> counts = Cou

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值