Python 使用nltk提取单词
从这个网站上,我了解到如何从标记的语料库中拆分标记的单词 网站中的代码:Python 使用nltk提取单词,python,nlp,nltk,Python,Nlp,Nltk,从这个网站上,我了解到如何从标记的语料库中拆分标记的单词 网站中的代码: >>> 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 depa
>>> 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'...
您所问的有点令人困惑,因为在您的输出示例中,每个元素都被包装在一个1元组中,我真的不明白这有什么意义
编辑:尽管正如拉斯曼指出的:
('The',)
将是一个1元组,而('The')=='The'
我不是nltk
专家,但您可以直接使用以下参数选择第一个元组元素:
[nltk.tag.str2tuple(t)[0] for t in sent.split()]
这将为您提供所有单词的列表:
['The', 'grand', 'jury'...
您所问的有点令人困惑,因为在您的输出示例中,每个元素都被包装在一个1元组中,我真的不明白这有什么意义
编辑:尽管正如拉斯曼指出的:
('The',)
将是一个1元组,而('The')=='The'
实际上,OP的示例中没有一个元组('The')=='The'
。实际上,OP的示例中没有一个元组<代码>('The')='The'。