python – NLTK布朗语料库标签
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – NLTK布朗语料库标签,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2125字,纯文字阅读大概需要4分钟。
内容图文
当我打印nltk.corpus.brown.tagged_words()时,它会输出大约1161192个带有单词及其相关标签的元组.
我想区分具有不同不同标签的不同字母.一个单词可以有多个标签.
Append list items by number of hyphens available我用这个帖子尝试了每个代码但是我没有得到任何超过3个标签的单词.据我所知,甚至还有8或9个标签.
我的做法哪里错了?怎么解决这个?我有两个不同的问题:
>如何在不同的不同标签下弄清楚语料库中不同单词的数量?语料库中的不同单词的数量让我们说8个不同的标签.
>同样,我想知道具有最多不同标签的单词.
而且,我只对单词感兴趣.我正在删除标点符号.
解决方法:
使用defaultdict(Counter)来跟踪单词及其POS.然后按键’len(Counter)对字典进行排序:
from collections import defaultdict, Counter
from nltk.corpus import brown
# Keeps words and pos into a dictionary
# where the key is a word and
# the value is a counter of POS and counts
word_tags = defaultdict(Counter)
for word, pos in brown.tagged_words():
word_tags[word][pos] +=1
# To access the POS counter.
print 'Red', word_tags['Red']
print 'Marlowe', word_tags['Marlowe']
print
# Greatest number of distinct tag.
word_with_most_distinct_pos = sorted(word_tags, key=lambda x: len(word_tags[x]), reverse=True)[0]
print word_with_most_distinct_pos
print word_tags[word_with_most_distinct_pos]
print len(word_tags[word_with_most_distinct_pos])
[OUT]:
Red Counter({u'JJ-TL': 49, u'NP': 21, u'JJ': 3, u'NN-TL': 1, u'JJ-TL-HL': 1})
Marlowe Counter({u'NP': 4})
that
Counter({u'CS': 6419, u'DT': 1975, u'WPS': 1638, u'WPO': 135, u'QL': 54, u'DT-NC': 6, u'WPS-NC': 3, u'CS-NC': 2, u'WPS-HL': 2, u'NIL': 1, u'CS-HL': 1, u'WPO-NC': 1})
12
用X号获得单词不同的POS:
# Words with 8 distinct POS
word_with_eight_pos = filter(lambda x: len(word_tags[x]) == 8, word_tags.keys())
for i in word_with_eight_pos:
print i, word_tags[i]
print
# Words with 9 distinct POS
word_with_nine_pos = filter(lambda x: len(word_tags[x]) == 9, word_tags.keys())
for i in word_with_nine_pos:
print i, word_tags[i]
[OUT]:
a Counter({u'AT': 21824, u'AT-HL': 40, u'AT-NC': 7, u'FW-IN': 4, u'NIL': 3, u'FW-IN-TL': 1, u'AT-TL': 1, u'NN': 1})
: Counter({u':': 1558, u':-HL': 138, u'.': 46, u':-TL': 22, u'IN': 20, u'.-HL': 8, u'NIL': 1, u',': 1, u'NP': 1})
内容总结
以上是互联网集市为您收集整理的python – NLTK布朗语料库标签全部内容,希望文章能够帮你解决python – NLTK布朗语料库标签所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。