python - 使用 nltk 提取单词

标签 python nlp nltk

来自网站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/

相关文章:

python - 将来自 NLTK NaiveBayesClassifier 的信息量最大的特征存储在列表中

python - 如何安装和调用 Stanford NERTagger?

Windows 上 Jupyter 笔记本中的 Python 路径

python - 从Python中的df列中删除字符串

machine-learning - Genisim doc2vec : how is short doc processed?

java - 如何在Java中结合英语单词?

nlp - 使用 NLTK 访问 Switchboard 语料库中的对话行为注释

python :getting the count for the adjectives in a string

python - Tensorflow:feed_dict 的形状错误{}

python - 如何通过 ssh 进入服务器,并使用 python 在该服务器上执行 bash 命令