【问题标题】:How to use meta-data in NLTK classifiers如何在 NLTK 分类器中使用元数据
【发布时间】:2014-01-07 05:10:12
【问题描述】:

据我所知,使用 NLTK 分类器的示例:

它们似乎只处理句子本身的功能。所以,你会...

corpus = 
[
("This is a sentence"),
("This is another sentence")
]

...然后你应用一些函数,比如 count_words_ending_in_a_vowel() 到句子本身。

相反,我想将一段外部数据应用于句子,不是从文本本身派生的东西,而是外部标签,例如:

corpus = 
[
("This is a sentence", "awesome"),
("This is another sentence", "not awesome")
]

或者

corpus = 
[
{"text": "This is a sentence", "label": "awesome"},
{"text": "This is another sentence", "label": "not awesome"}
]

(如果我可能有多个外部标签。)

我的问题是:鉴于我的数据集中有这些外部标签,我如何将语料库重新格式化为NaiveBayesClassifier.train() 期望的格式?我知道我还需要在上面的“文本”字段上应用标记器——但是我应该输入 NaiveeBayesClassifier.train 函数的总格式是什么?

申请

classifier = nltk.NaiveBayesClassifier.train(goods)
print(classifier.show_most_informative_features(32))

我更广泛的目标 --- 我想研究不同的词频如何能够预测标签,哪些词集在将标签彼此分开时提供的信息最多。这种有 k-means 的感觉,但我被告知我应该能够完全在 NLTK 中完成此操作,只是在将其转换为适当的数据输入格式时遇到了麻烦。

【问题讨论】:

    标签: python nlp nltk


    【解决方案1】:

    我通过以下方法取得了成功:

    train = [({'some': True, 'tokens': True}, 'label'),
             ({'other': True, 'word': True}, 'different label'),
             ({'cool': True, 'document': True}, 'label')]
    classifier = nltk.NaiveBayesClassifier.train(train)
    

    所以train 是一个文档列表(每个都是一个元组)。每个元组的第一个元素是一个标记字典(标记是键,值是True 表示该标记的存在),第二个元素是与文档关联的标签。

    【讨论】:

    • 嗯,我的数据是你描述的格式,我的分类器不断返回>>> print classifier.show_most_informative_features(4) Most Informative Features None 。我认为这意味着我有语法错误。但这似乎意味着我的数据/模型有问题?
    猜你喜欢
    • 2012-11-11
    • 2017-03-02
    • 2011-06-21
    • 2018-04-09
    • 2016-08-26
    • 2020-07-06
    • 2012-11-11
    • 2018-09-11
    • 2012-09-05
    相关资源
    最近更新 更多