【问题标题】:How to tag an article with NLTK?如何用 NLTK 标记文章?
【发布时间】:2015-08-06 09:12:28
【问题描述】:

我正在尝试了解如何通过 NLTK 标记文章。根据bullet point 2.7 解释如何 找到最常用的名词。

def findtags(tag_prefix, tagged_text):
    cfd = nltk.ConditionalFreqDist((tag, word) for (word, tag) in tagged_text
                                  if tag.startswith(tag_prefix))
    return dict((tag, cfd[tag].most_common(5)) for tag in cfd.conditions())


>>> tagdict = findtags('NN', nltk.corpus.brown.tagged_words(categories='news'))
>>> for tag in sorted(tagdict):
...     print(tag, tagdict[tag])

但这真的很令人困惑,因为我看不到如何注入一段文本来找到它的标签。相反,它使用的是预定义的数据结构(nltk.corpus.brown.tagged_words)。不知道如何在此处继续。

【问题讨论】:

    标签: python nltk


    【解决方案1】:

    简而言之

    要标记文本,请使用 nltk.pos_tag,但请注意其怪癖 (Python NLTK pos_tag not returning the correct part-of-speech tag):

    >>> from nltk import sent_tokenize, word_tokenize, pos_tag
    >>> text = "This is a foo bar piece of text. And there are many sentences in this text."
    >>> tagged_text = [pos_tag(word_tokenize(sent)) for sent in sent_tokenize(text)]
    >>> tagged_text
    [[('This', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('foo', 'NN'), ('bar', 'NN'), ('piece', 'NN'), ('of', 'IN'), ('text', 'NN'), ('.', '.')], [('And', 'CC'), ('there', 'EX'), ('are', 'VBP'), ('many', 'JJ'), ('sentences', 'NNS'), ('in', 'IN'), ('this', 'DT'), ('text', 'NN'), ('.', '.')]]
    

    长期

    可以通过以下方式找到 NLTK 数据集列表:

    >>> import nltk
    >>> nltk.download()
    

    nltk.corpus.brown 语料库是自然语言处理或文本处理最常用的语料库之一(行话见What is the difference between corpus and lexicon in NLTK (python))。

    对于棕色语料库,它是一个完全标记和标记化的语料库,因此所有 NLTK 提供的都是一个阅读器。要访问各种注释,请参阅http://www.nltk.org/howto/corpus.html 上的第 1.3 节,这里有几个示例:

    >>> from nltk.corpus import brown
    >>> brown.words()[:50]
    [u'The', u'Fulton', u'County', u'Grand', u'Jury', u'said', u'Friday', u'an', u'investigation', u'of', u"Atlanta's", u'recent', u'primary', u'election', u'produced', u'``', u'no', u'evidence', u"''", u'that', u'any', u'irregularities', u'took', u'place', u'.', u'The', u'jury', u'further', u'said', u'in', u'term-end', u'presentments', u'that', u'the', u'City', u'Executive', u'Committee', u',', u'which', u'had', u'over-all', u'charge', u'of', u'the', u'election', u',', u'``', u'deserves', u'the', u'praise']
    >>> brown.tagged_words()[:50]
    [(u'The', u'AT'), (u'Fulton', u'NP-TL'), (u'County', u'NN-TL'), (u'Grand', u'JJ-TL'), (u'Jury', u'NN-TL'), (u'said', u'VBD'), (u'Friday', u'NR'), (u'an', u'AT'), (u'investigation', u'NN'), (u'of', u'IN'), (u"Atlanta's", u'NP$'), (u'recent', u'JJ'), (u'primary', u'NN'), (u'election', u'NN'), (u'produced', u'VBD'), (u'``', u'``'), (u'no', u'AT'), (u'evidence', u'NN'), (u"''", u"''"), (u'that', u'CS'), (u'any', u'DTI'), (u'irregularities', u'NNS'), (u'took', u'VBD'), (u'place', u'NN'), (u'.', u'.'), (u'The', u'AT'), (u'jury', u'NN'), (u'further', u'RBR'), (u'said', u'VBD'), (u'in', u'IN'), (u'term-end', u'NN'), (u'presentments', u'NNS'), (u'that', u'CS'), (u'the', u'AT'), (u'City', u'NN-TL'), (u'Executive', u'JJ-TL'), (u'Committee', u'NN-TL'), (u',', u','), (u'which', u'WDT'), (u'had', u'HVD'), (u'over-all', u'JJ'), (u'charge', u'NN'), (u'of', u'IN'), (u'the', u'AT'), (u'election', u'NN'), (u',', u','), (u'``', u'``'), (u'deserves', u'VBZ'), (u'the', u'AT'), (u'praise', u'NN')]
    >>> brown.sents()
    [[u'The', u'Fulton', u'County', u'Grand', u'Jury', u'said', u'Friday', u'an', u'investigation', u'of', u"Atlanta's", u'recent', u'primary', u'election', u'produced', u'``', u'no', u'evidence', u"''", u'that', u'any', u'irregularities', u'took', u'place', u'.'], [u'The', u'jury', u'further', u'said', u'in', u'term-end', u'presentments', u'that', u'the', u'City', u'Executive', u'Committee', u',', u'which', u'had', u'over-all', u'charge', u'of', u'the', u'election', u',', u'``', u'deserves', u'the', u'praise', u'and', u'thanks', u'of', u'the', u'City', u'of', u'Atlanta', u"''", u'for', u'the', u'manner', u'in', u'which', u'the', u'election', u'was', u'conducted', u'.'], ...]
    >>> brown.sents()[:3]
    [[u'The', u'Fulton', u'County', u'Grand', u'Jury', u'said', u'Friday', u'an', u'investigation', u'of', u"Atlanta's", u'recent', u'primary', u'election', u'produced', u'``', u'no', u'evidence', u"''", u'that', u'any', u'irregularities', u'took', u'place', u'.'], [u'The', u'jury', u'further', u'said', u'in', u'term-end', u'presentments', u'that', u'the', u'City', u'Executive', u'Committee', u',', u'which', u'had', u'over-all', u'charge', u'of', u'the', u'election', u',', u'``', u'deserves', u'the', u'praise', u'and', u'thanks', u'of', u'the', u'City', u'of', u'Atlanta', u"''", u'for', u'the', u'manner', u'in', u'which', u'the', u'election', u'was', u'conducted', u'.'], [u'The', u'September-October', u'term', u'jury', u'had', u'been', u'charged', u'by', u'Fulton', u'Superior', u'Court', u'Judge', u'Durwood', u'Pye', u'to', u'investigate', u'reports', u'of', u'possible', u'``', u'irregularities', u"''", u'in', u'the', u'hard-fought', u'primary', u'which', u'was', u'won', u'by', u'Mayor-nominate', u'Ivan', u'Allen', u'Jr.', u'.']]
    >>> brown.tagged_sents()[:3]
    [[(u'The', u'AT'), (u'Fulton', u'NP-TL'), (u'County', u'NN-TL'), (u'Grand', u'JJ-TL'), (u'Jury', u'NN-TL'), (u'said', u'VBD'), (u'Friday', u'NR'), (u'an', u'AT'), (u'investigation', u'NN'), (u'of', u'IN'), (u"Atlanta's", u'NP$'), (u'recent', u'JJ'), (u'primary', u'NN'), (u'election', u'NN'), (u'produced', u'VBD'), (u'``', u'``'), (u'no', u'AT'), (u'evidence', u'NN'), (u"''", u"''"), (u'that', u'CS'), (u'any', u'DTI'), (u'irregularities', u'NNS'), (u'took', u'VBD'), (u'place', u'NN'), (u'.', u'.')], [(u'The', u'AT'), (u'jury', u'NN'), (u'further', u'RBR'), (u'said', u'VBD'), (u'in', u'IN'), (u'term-end', u'NN'), (u'presentments', u'NNS'), (u'that', u'CS'), (u'the', u'AT'), (u'City', u'NN-TL'), (u'Executive', u'JJ-TL'), (u'Committee', u'NN-TL'), (u',', u','), (u'which', u'WDT'), (u'had', u'HVD'), (u'over-all', u'JJ'), (u'charge', u'NN'), (u'of', u'IN'), (u'the', u'AT'), (u'election', u'NN'), (u',', u','), (u'``', u'``'), (u'deserves', u'VBZ'), (u'the', u'AT'), (u'praise', u'NN'), (u'and', u'CC'), (u'thanks', u'NNS'), (u'of', u'IN'), (u'the', u'AT'), (u'City', u'NN-TL'), (u'of', u'IN-TL'), (u'Atlanta', u'NP-TL'), (u"''", u"''"), (u'for', u'IN'), (u'the', u'AT'), (u'manner', u'NN'), (u'in', u'IN'), (u'which', u'WDT'), (u'the', u'AT'), (u'election', u'NN'), (u'was', u'BEDZ'), (u'conducted', u'VBN'), (u'.', u'.')], [(u'The', u'AT'), (u'September-October', u'NP'), (u'term', u'NN'), (u'jury', u'NN'), (u'had', u'HVD'), (u'been', u'BEN'), (u'charged', u'VBN'), (u'by', u'IN'), (u'Fulton', u'NP-TL'), (u'Superior', u'JJ-TL'), (u'Court', u'NN-TL'), (u'Judge', u'NN-TL'), (u'Durwood', u'NP'), (u'Pye', u'NP'), (u'to', u'TO'), (u'investigate', u'VB'), (u'reports', u'NNS'), (u'of', u'IN'), (u'possible', u'JJ'), (u'``', u'``'), (u'irregularities', u'NNS'), (u"''", u"''"), (u'in', u'IN'), (u'the', u'AT'), (u'hard-fought', u'JJ'), (u'primary', u'NN'), (u'which', u'WDT'), (u'was', u'BEDZ'), (u'won', u'VBN'), (u'by', u'IN'), (u'Mayor-nominate', u'NN-TL'), (u'Ivan', u'NP'), (u'Allen', u'NP'), (u'Jr.', u'NP'), (u'.', u'.')]]
    

    结构:

    • nltk.corpus.brown.words() 是一个字符串列表,列表中的每一项都是一个单词
    • nltk.corpus.brown.tagged_words() 是一个元组列表,其中第一个元素作为单词,元组中的第二个元素作为标签
    • nltk.corpus.sents() 是一个字符串列表的列表,其中另一个列表包含整个语料库,内部列表是一个句子
    • nltk.corpus.tagged_sents() 是一个元组列表的列表,它与nltk.corpus.sents() 相同,但内部列表是一个单词和标签的元组。

    【讨论】:

    • 非常感谢您的出色回答。那么语料库不仅仅是一个示例文件,它实际上是处理标记所需要的吗?查看 nltk 文件夹,即使在删除所有 zip 文件后,单独的 corpus 文件夹也是 1.4 GB。这一切都需要吗?或者我可以保留brown 以获得最佳效果吗? models 文件夹怎么样?这也需要吗? 120mb
    • 视任务而定,如果你只需要棕色语料库,你可以做import nltk; nltk.download('brown')nltk_data 文件夹中的其他文件用于其他功能。但最有可能的是,nltk.sent_tokenize 需要punktnltk.pos_tag 也需要maxent_treebank_pos_tagger,可能有一天你会需要universal_tagsetsnowball_data
    • 但是,如果您喜欢“包含电池”的想法,那么只需下载所有型号。至于语料库,如果您有兴趣将它们用作训练数据,它也非常有用。我通常做nltk.download('all') 这样我就能得到一切=)
    • NLTK 中的Corpora(语料库的复数)已经过预处理和压缩。 NLTK 提供 API 来轻松读取它们。
    • 太棒了,感谢您提供的信息。由于我在 GoogleAppEngine 上使用代码,因此存在一些限制,我需要预先准备所有要上传的库和数据。那么当你说它们被压缩并且 NLTK 可以读取它们时,性能如何?
    猜你喜欢
    • 2016-01-09
    • 2011-07-02
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    相关资源
    最近更新 更多