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、做文本分類等)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章