如何从棕色语料库中获取动词、名词、形容词?
How can I get verbs, nouns, adjectives from brown corpus?
我一直想把brown corpus中的名词,动词..等全部分开,所以尝试用代码
brown.all_synsets('n')
但显然此代码仅适用于 wordnet。顺便说一句,我正在使用 python 3.4。
已编辑
@alvas 回答有效。但是当我随机使用它时,它会出错。看看。
nn = {word for word, pos in brown.tagged_words() if pos.startswith('NN')}
print(nn)
输出是
{'such', 'rather', 'Quite', 'Such', 'quite'}
但是当我使用
random.choice(nn)
我明白了
Traceback (most recent call last):
File "/home/aziz/Desktop/2222.py", line 5, in <module>
print(random.choice(NN))
File "/usr/lib/python3.4/random.py", line 256, in choice
return seq[i]
TypeError: 'set' object does not support indexing
TL;DR
>>> from nltk.corpus import brown
>>> {word for word, pos in brown.tagged_words() if pos.startswith('NN')}
更长
遍历 .tagged_words()
函数,这将 return 一个 ('word', 'POS')
元组的列表:
>>> from nltk.corpus import brown
>>> brown.tagged_words()
[(u'The', u'AT'), (u'Fulton', u'NP-TL'), ...]
请阅读本章以了解 NLTK 语料库 API 的工作原理:http://www.nltk.org/book/ch02.html
然后,对其进行列表理解并保存一组(即唯一列表)用名词标签标记的单词,例如NN, NNS, NNP, etc.
。
>>> {word for word, pos in brown.tagged_words() if pos.startswith('NN')}
请注意输出可能不是你所期望的,因为用词性标记句法和句法名词的单词不需要语义argument/entity。
另外,我认为您提取的单词不正确。仔细检查列表:
>>> nouns = {word for word, pos in brown.tagged_words() if pos.startswith('NN')}
>>> 'rather' in nouns
False
>>> 'such' in nouns
False
>>> 'Quite' in nouns
False
>>> 'quite' in nouns
False
>>> 'Such' in nouns
False
列表理解的输出:http://pastebin.com/bJaPdpUk
当 nn
是一个集合时,为什么 random.choice(nn)
失败?
random.choice()
的输入是一个序列(参见 https://docs.python.org/2/library/random.html#random.choice)。
random.choice(seq)
Return a random element from the non-empty sequence
seq. If seq is empty, raises IndexError.
而python中的序列类型python是
str, unicode, list, tuple, bytearray, buffer, xrange
在 Python 2.x(参见 https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange)。
list, tuple, range
在 Python 3.x(参见 https://docs.python.org/3.6/library/stdtypes.html#sequence-types-list-tuple-range)
- (二进制序列类型)
bytes, bytearray, memoryview
in Python 3.x
- (文本字符串序列)
str
in Python 3.x
由于 set
不是序列,您将得到 IndexError
.
我一直想把brown corpus中的名词,动词..等全部分开,所以尝试用代码
brown.all_synsets('n')
但显然此代码仅适用于 wordnet。顺便说一句,我正在使用 python 3.4。
已编辑
@alvas 回答有效。但是当我随机使用它时,它会出错。看看。
nn = {word for word, pos in brown.tagged_words() if pos.startswith('NN')}
print(nn)
输出是
{'such', 'rather', 'Quite', 'Such', 'quite'}
但是当我使用
random.choice(nn)
我明白了
Traceback (most recent call last):
File "/home/aziz/Desktop/2222.py", line 5, in <module>
print(random.choice(NN))
File "/usr/lib/python3.4/random.py", line 256, in choice
return seq[i]
TypeError: 'set' object does not support indexing
TL;DR
>>> from nltk.corpus import brown
>>> {word for word, pos in brown.tagged_words() if pos.startswith('NN')}
更长
遍历 .tagged_words()
函数,这将 return 一个 ('word', 'POS')
元组的列表:
>>> from nltk.corpus import brown
>>> brown.tagged_words()
[(u'The', u'AT'), (u'Fulton', u'NP-TL'), ...]
请阅读本章以了解 NLTK 语料库 API 的工作原理:http://www.nltk.org/book/ch02.html
然后,对其进行列表理解并保存一组(即唯一列表)用名词标签标记的单词,例如NN, NNS, NNP, etc.
。
>>> {word for word, pos in brown.tagged_words() if pos.startswith('NN')}
请注意输出可能不是你所期望的,因为用词性标记句法和句法名词的单词不需要语义argument/entity。
另外,我认为您提取的单词不正确。仔细检查列表:
>>> nouns = {word for word, pos in brown.tagged_words() if pos.startswith('NN')}
>>> 'rather' in nouns
False
>>> 'such' in nouns
False
>>> 'Quite' in nouns
False
>>> 'quite' in nouns
False
>>> 'Such' in nouns
False
列表理解的输出:http://pastebin.com/bJaPdpUk
当 nn
是一个集合时,为什么 random.choice(nn)
失败?
random.choice()
的输入是一个序列(参见 https://docs.python.org/2/library/random.html#random.choice)。
random.choice(seq)
Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.
而python中的序列类型python是
str, unicode, list, tuple, bytearray, buffer, xrange
在 Python 2.x(参见 https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange)。list, tuple, range
在 Python 3.x(参见 https://docs.python.org/3.6/library/stdtypes.html#sequence-types-list-tuple-range)- (二进制序列类型)
bytes, bytearray, memoryview
in Python 3.x - (文本字符串序列)
str
in Python 3.x
由于 set
不是序列,您将得到 IndexError
.