python - 使用我自己的语料库在 Python NLTK 中进行类别分类

标签 python nlp machine-learning nltk corpus

我是一名 NTLK/Python 初学者,并设法使用 CategorizedPlaintextCorpusReader 加载了我自己的语料库,但我如何实际训练和使用数据进行文本分类?

>>> from nltk.corpus.reader import CategorizedPlaintextCorpusReader
>>> reader = CategorizedPlaintextCorpusReader('/ebs/category', r'.*\.txt', cat_pattern=r'(.*)\.txt')
>>> len(reader.categories())
234

最佳答案

假设您想要一个具有词袋特征的朴素贝叶斯分类器:

from nltk import FreqDist
from nltk.classify.naivebayes import NaiveBayesClassifier

def make_training_data(rdr):
    for c in rdr.categories():
        for f in rdr.fileids(c):
            yield FreqDist(rdr.words(fileids=[f])), c

clf = NaiveBayesClassifier.train(list(make_training_data(reader)))

生成的 clfclassify 方法可用于任何 FreqDist 词。

(但请注意:从您的 cap_pattern 看来,您的语料库中的每个文件似乎都有样本一个类别。请检查这是否真的是您想要的。)

关于python - 使用我自己的语料库在 Python NLTK 中进行类别分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8818265/

相关文章:

python - Scrapy CLOSESPIDER_PAGECOUNT 设置不能正常工作

python - 从 'train model from_config' 导入 'deeppavlov.core.commands.train' 时出错

java - Google NLP api 提供找不到 TLS ALPN 提供程序;没有可用的 netty-tcnative、Conscrypt 或 Jetty NPN/ALPN

python - 将句子拆分为单词并将情感极性重新应用于每个单词时如何维护索引?

machine-learning - Google 预测 CSV 最大大小?

python - 如何使用 Keras.to_Categorical 在 dataFrame 中一次对多列进行 One-Hot 编码?

machine-learning - 使用贝叶斯网络对新实例进行分类

python - 将 "python2.7 -c"输出作为 C 文件的参数传递

python - "by = lambda x: lambda y: getattr(y, x)"是什么意思?

python - pandas 中类似 SQL 的语句?