Mallet的基本使用

【官网下载】

MALLET is a Java-based package for statistical natural language processing, document classification, clustering, topic modeling, information extraction, and other machine learning applications to text.

下载mallet包,注意里面已经含有了训练数据集
【准备测试】
(1)在bin\mallet.bat里面第33行左右的位置加入
if "%CMD%"=="classify-file" set CLASS=cc.mallet.classify.tui.Csv2Classify
if "%CMD%"=="classify-dir" set CLASS=cc.mallet.classify.tui.Text2Classify
  CMD命令行进入解压目录,如 C:\mallet-2.0.7
(2)文本分类

1. 将源文件格式转换为mallet自己的处理格式

C:\mallet-2.0.7 >bin\mallet import-dir --input D:\testfile --output D:\classify-input.mallet

 

2.使用mallet算法库中的NaiveBayes算法训练获得分类器

C:/mallet>bin\mallet train-classifier --input D:\classify-input.mallet --trainer NaiveBayes --training-portion 0.8 --output-classifier D:\classifier1.classifier  

--input参数的值classify-input.mallet 是第 一步中生成的特征向量

--trainer参数的值NaiveBayes 是指训练分类器的算法,可以指定其他算法,例如 MaxEnt等 

--training-portion 参数的值这里是0.8 , 可以根据需要设定,0.8 的意思是随机抽取classify-input.mallet 数据中的80% 当训练数据,剩下的当测试数据,用于测试已训练好的分类器的准确性等等性能指标。

--output-classifier 参数的值classifier1.classifier 是所存已训练好的分类器的名称。

 

3.使用分类器测试新数据集

C:/mallet>bin\mallet classify-file --input D:\text.txt --output - --classifier D:\classifier1.classifier

--output 后面参数值“- ”意思是直接在命令行中输出所属各个类别的概率,也可以是”D:\result_file.txt“。

--classifier 参数的值是指使用的分类器名称(即,训练好的分类器)。

 

(3)主题建模

1. 将训练集转换为mallet专用格式

C:/mallet>bin\mallet import-dir --input D:\sample-data\topic-input --output D:\topic-input.mallet --keep-sequence --remove-stopwords

 --keep-sequence 参数必须有,否则会出错,因为主题建模时所用数据源就是特征序列,而不是特征向量,所以必须用--keep-sequence 此参数 来限制转换数据的格式。

--remove-stopwords 的意思是移除停用词。

 

2. 训练主题

C:/mallet>bin\mallet train-topics --input D:\topic-input.mallet --num-topics 2 --output-doc-topics D:\docstopics.txt --inferencer -filename D:\infer1.inferencer

--num-topics 的值2 意思是限定主题个数为2,默认的主题数为10

--output-doc-topics 参数的意思是输出文档- 主题矩阵,存到docstopics文件中

--inferencer -filename 参数的意思是对将训练好的主题模型进行存储,此主题模型存到参数值infer1.inferencer

 

3.测试集

C:/mallet>bin\mallet import-dir --input D:\sample-data\data --output D:\topic-test.mallet --keep-sequence  --remove-stopwords

 

4.获得测试集的主题类别

C:/mallet>bin\mallet infer-topics --input D:\topic-test.mallet --inferencer D:\infer1.inferencer --output-doc-topics D:\testdocstopics.txt

 

【参考博客】

Mallet 使用说明
mallet使用笔记(学topic model、做文本分类等)
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值