nltk中的TaggedCorpusReader和UnigramTagger(python)

时间:2011-12-28 18:00:01

标签: python nltk

我正在尝试使用nltk以非常低保真的方式对新闻文章进行自动分类。我已经创建了一个与我的类别(即教师/ EDU,计算机/ TECH等)相关的单词/标签对的自定义语料库。我一直在阅读,this question让我非常接近,但我'我仍然坚持。

根据我的代码到目前为止,如何使用我的标记器标记我的句子?

import nltk

# Loads my custom word/tag corpus
from nltk.corpus.reader import TaggedCorpusReader
reader = TaggedCorpusReader('taggers','.*')

#Sets up the UnigramTagger
default_tagger = nltk.data.load(nltk.tag._POS_TAGGER)
tagger = nltk.tag.UnigramTagger(model=reader.tagged_words(), backoff=default_tagger)

#Sample content
sent = 'The students went to school to ask their teacher what the homework for the day was but she told them to check their email.'
tokens = nltk.tokenize.word_tokenize(sent)

# Sad Panda
tagged = tagger.tag(tokens) 
# ^ produces AttributeError: 'ConcatenatedCorpusView' object has no attribute 'get'

这也很可能是做我正在做的事情的一种糟糕的方式,但对于第一次运行来说似乎已经足够了。提前谢谢。

1 个答案:

答案 0 :(得分:2)

标记符用于词性标注,而不是文本分类。看看路透社的语料库 - 它使用类别文件将新闻文章分类为多个类别。然后查看nltk.classify模块并阅读如何训练文本分类器。