受到Google關于Map Reduce和Google Filesystem的一些著名論文的啟發,這些新功能用于將索引分發到Nutch,并添加到Nutch中,這些功能最終由他們自己擁有: Hadoop 。 從那時起,許多項目都通過Hadoop開發。 我們正面臨由Lucene火花點燃的開源代碼的大爆炸。
- 分類:這是一種有監督的學習,您為機器提供了…事物的許多實例(例如文檔)以及它們的類別。 機器從所有這些機器中學習將已知實例分類為將來的實例。
- 聚類:從某種意義上講,它類似于分類,它使事物成組,但是這一分類不受監督。 您給機器提供了很多東西,然后機器將成組的類似物品組合在一起。
- 建議:這正是IMDb或Amazon對頁面底部推薦的電影或書籍的處理方式。 根據其他用戶的喜歡程度(通過用戶投票的排名明星來衡量),Amazon可以推斷“喜歡這本書的其他用戶也喜歡這些其他人”。
離線示例
- 從網上獲取火腿/垃圾郵件語料庫(當然已經分類了)。
- 用語料庫的80%訓練分類器,剩下20%進行測試
- 創建一個簡單的Web服務,對在線垃圾郵件進行分類。 您向其提供郵件,并且會顯示“好”或“不好”(或“火腿”或“垃圾郵件”)
訓練分類器時,會為其提供一個文件(是,一個文件),該文件每行包含一個已經分析過的文檔。 “分析”的含義與Lucene分析文檔的含義相同。 實際上,我們將使用Lucene StandardAnalizer來清理一些文檔并將它們轉換為術語流。 該流將放在此培訓文件的一行中,其中第一個術語是該項目所屬的類別。 例如,培訓文件將如下所示
垃圾郵件現在購買偉哥特別折扣
我們將分別在其中放置一個垃圾郵件目錄和一個火腿目錄。
垃圾郵件
垃圾郵件
接下來,我們將使用以下命令準備訓練和測試文件
bin/mahout prepare20newsgroups -p examples/bin/work/spam/train -o examples/bin/work/spam/prepared-train -a org.apache.mahout.vectorizer.DefaultAnalyzer -c UTF-8
bin/mahout prepare20newsgroups -p examples/bin/work/spam/test -o examples/bin/work/spam/prepared-test -a org.apache.mahout.vectorizer.DefaultAnalyzer -c UTF-8
bin/mahout trainclassifier -i examples/bin/work/spam/prepared-train -o examples/bin/work/spam/bayes-model -type bayes -ng 1 -source hdfs
bin/mahout testclassifier -m examples/bin/work/spam/bayes-model -d examples/bin/work/spam/prepared-test -type bayes -ng 1 -source hdfs -method sequential
一段時間后,我們得到以下結果
-------------------------------------------------------Correctly Classified Instances : 383 95,75%Incorrectly Classified Instances : 17 4,25%Total Classified Instances : 400
=======================================================Confusion Matrix-------------------------------------------------------a b <--Classified as189 11 | 200 a = spam6 194 | 200 b = ham
很好的結果!!!
實時對垃圾郵件進行分類的服務器
public class Antispam extends HttpServlet {private SpamClassifier sc;public void init() {try {sc = new SpamClassifier();sc.init(new File("bayes-model"));} catch (FileNotFoundException e) {e.printStackTrace();} catch (InvalidDatastoreException e) {e.printStackTrace();}}protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Reader reader = req.getReader();try {long t0 = System.currentTimeMillis();String category = sc.classify(reader);long t1 = System.currentTimeMillis();resp.getWriter().print(String.format("{\"category\":\"%s\", \"time\": %d}", category, t1-t0));} catch (InvalidDatastoreException e) {e.printStackTrace();}}}
public class SpamClassifier {private ClassifierContext context;private Algorithm algorithm;private Datastore datastore;private File modelDirectory;Analyzer analyzer;public SpamClassifier(){analyzer = new DefaultAnalyzer();}public void init(File basePath) throws FileNotFoundException, InvalidDatastoreException{if(!basePath.isDirectory() || !basePath.canRead()){throw new FileNotFoundException(basePath.toString());}modelDirectory = basePath;
algorithm = new BayesAlgorithm();BayesParameters p = new BayesParameters();p.set("basePath", modelDirectory.getAbsolutePath());p.setGramSize(1);datastore = new InMemoryBayesDatastore(p);context = new ClassifierContext(algorithm, datastore);context.initialize();}public String classify(Reader mail) throws IOException, InvalidDatastoreException {String document[] = BayesFileFormatter.readerToDocument(analyzer, mail);ClassifierResult result = context.classifyDocument(document, "unknown");return result.getLabel();}}
該算法是該方法的核心,并且是策略設計模式的明顯實例。 我們正在使用BayesAlgorithm,但是我們可以使用使用互補樸素貝葉斯算法的CbayesAlgorithm。
ClassifierContext是用于對文檔進行分類的接口。
curl http://localhost:8080/antispam -H "Content-T-Type: text/xml" --data-binary @ham.txt
我們得到
{"category":"ham", "time": 10}
結論
參考:來自我們的JCG合作伙伴 Emmanuel Espina的 火腿,垃圾郵件和大象(或如何使用Mahout構建垃圾郵件過濾服務器) ? 在emmaespina博客上。
翻譯自: https://www.javacodegeeks.com/2012/03/apache-mahout-build-spam-filter-server.html