受到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