来自网站http://nltk.googlecode.com/svn/trunk/doc/book/ch05.html我开始了解如何从标记语料库中分割标记词。
网站中的代码:
>>> 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/PPS
... said/VBD ``/`` ARE/BER well/QL operated/VBN and/CC follow/VB generally/RB
... 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()]
[('The', 'AT'), ('grand', 'JJ'), ('jury', 'NN'), ('commented', 'VBD'),
('on', 'IN'), ('a', 'AT'), ('number', 'NN'), ... ('.', '.')]
这里我得到了标记单词的列表。我想要的是一个只包含单词的列表。例如:
[('The'), ('grand'), ('jury')...
而不是
('The', 'AT'), ('grand', 'JJ'), ('jury', 'NN')...
有什么建议我如何获得这个吗?
提前致谢。
最佳答案
我不是 nltk
专家,但您可以直接选择第一个元组元素:
[nltk.tag.str2tuple(t)[0] for t in sent.split()]
这将为您提供所有单词的列表:
['The', 'grand', 'jury'...
你问的有点令人困惑,因为在你的输出示例中,每个元素都包含在一个一元组中,我真的不明白这一点。
编辑:尽管正如拉斯曼指出的那样:('The',)
将是一个 1 元组,而 ('The') == “那个”
。
关于python - 使用 nltk 提取单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9148679/