java编程学习:基于Kmeans算法的文档聚类(包含Java代码及数据格式)

Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。

给你学习路线:html-css-js-jq-javase-数据库-jsp-servlet-Struts2-hibernate-mybatis-spring4-springmvc-ssh-ssm

介绍

给定多篇文档,如何对文档进行聚类。本博客使用的是k-means聚类方法。关于k-means网络上有很多资料介绍其算法思想和其数学公式。

针对文档聚类,首先要讲文档进行向量化,也就是说要对文档进行编码。可以使用one-hot编码,也可以使用TF-IDF编码,也可以使用doc2vec编码等,总之,要将其向量化。

在该源码基础上做了改进。

输入数据结构

小编推荐一个学Java的学习裙【 七六零,二五零,五四一 】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!

该输入文本的第一列为文本的标题,第二列是经过去高频词、停用词、低频词之后的数据。

源码

首先,我修改的是文档的表示,因为我的数据和作者的json数据并不同。

package com.clustering;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.List;import java.util.StringTokenizer;/** Class for storing a collection of documents to be clustered. */public class DocumentList implements Iterable { private final List documents = new ArrayList(); private int numFeatures; /** Construct an empty DocumentList. */ public DocumentList() { } /** * Construct a DocumentList by parsing the input string. The input string may contain multiple * document records. Each record must be delimited by curly braces {}. */ /*public DocumentList(String input) { StringTokenizer st = new StringTokenizer(input, "{"); int numDocuments = st.countTokens() - 1; String record = st.nextToken(); // skip empty split to left of { for (int i = 0; i < numDocuments; i++) { record = st.nextToken(); Document document = Document.createDocument(record); if (document != null) { documents.add(document); } } }*/ public DocumentList(String input) throws IOException { BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( new File(input)),"gbk")); String s = null; int i = 0; while ((s=reader.readLine())!=null) { String arry[] =s.split(" "); String content = s.substring(arry[0].length()).trim(); String title =arry[0]; Document document = new Document(i, content, title); documents.add(document); i++; } reader.close(); } /** Add a document to the DocumentList. */ public void add(Document document) { documents.add(document); } /** Clear all documents from the DocumentList. */ public void clear() { documents.clear(); } /** Mark all documents as not being allocated to a cluster. */ public void clearIsAllocated() { for (Document document : documents) { document.clearIsAllocated(); } } /** Get a particular document from the DocumentList. */ public Document get(int index) { return documents.get(index); } /** Get the number of features used to encode each document. */ public int getNumFeatures() { return numFeatures; } /** Determine whether DocumentList is empty. */ public boolean isEmpty() { return documents.isEmpty(); } @Override public Iterator iterator() { return documents.iterator(); } /** Set the number of features used to encode each document. */ public void setNumFeatures(int numFeatures) { this.numFeatures = numFeatures; } /** Get the number of documents within the DocumentList. */ public int size() { return documents.size(); } /** Sort the documents within the DocumentList by document ID. */ public void sort() { Collections.sort(documents); } @Override public String toString() { StringBuilder sb = new StringBuilder(); for (Document document : documents) { sb.append(" "); sb.append(document.toString()); sb.append(" "); } return sb.toString(); }}

其次,针对KMeansClusterer,我们做了如下修改,因为我想要自定义k,而源码作者提供了自动调节k值的方法。

package com.clustering;import java.util.Random;/** A Clusterer implementation based on k-means clustering. */public class KMeansClusterer implements Clusterer { private static final Random RANDOM = new Random(); private final double clusteringThreshold; private final int clusteringIterations; private final DistanceMetric distance; /** * Construct a Clusterer. * * @param distance the distance metric to use for clustering * @param clusteringThreshold the threshold used to determine the number of clusters k * @param clusteringIterations the number of iterations to use in k-means clustering */ public KMeansClusterer(DistanceMetric distance, double clusteringThreshold, int clusteringIterations) { this.distance = distance; this.clusteringThreshold = clusteringThreshold; this.clusteringIterations = clusteringIterations; } /** * Allocate any unallocated documents in the provided DocumentList to the nearest cluster in the * provided ClusterList. */ private void allocatedUnallocatedDocuments(DocumentList documentList, ClusterList clusterList) { for (Document document : documentList) { if (!document.isAllocated()) { Cluster nearestCluster = clusterList.findNearestCluster(distance, document); nearestCluster.add(document); } } } /** * Run k-means clustering on the provided documentList. Number of clusters k is set to the lowest * value that ensures the intracluster to intercluster distance ratio is below * clusteringThreshold. */ @Override public ClusterList cluster(DocumentList documentList) { ClusterList clusterList = null; for (int k = 1; k <= documentList.size(); k++) { clusterList = runKMeansClustering(documentList, k); if (clusterList.calcIntraInterDistanceRatio(distance) < clusteringThreshold) { break; } } return clusterList; } /** Create a cluster with the unallocated document that is furthest from the existing clusters. */ private Cluster createClusterFromFurthestDocument(DocumentList documentList, ClusterList clusterList) { Document furthestDocument = clusterList.findFurthestDocument(distance, documentList); Cluster nextCluster = new Cluster(furthestDocument); return nextCluster; } /** Create a cluster with a single randomly seelcted document from the provided DocumentList. */ private Cluster createClusterWithRandomlySelectedDocument(DocumentList documentList) { int rndDocIndex = RANDOM.nextInt(documentList.size()); Cluster initialCluster = new Cluster(documentList.get(rndDocIndex)); return initialCluster; } /** Run k means clustering on the provided DocumentList for a fixed number of clusters k. */ public ClusterList runKMeansClustering(DocumentList documentList, int k) { ClusterList clusterList = new ClusterList(); documentList.clearIsAllocated(); clusterList.add(createClusterWithRandomlySelectedDocument(documentList)); while (clusterList.size() < k) { clusterList.add(createClusterFromFurthestDocument(documentList, clusterList)); } for (int iter = 0; iter < clusteringIterations; iter++) { allocatedUnallocatedDocuments(documentList, clusterList); clusterList.updateCentroids(); if (iter < clusteringIterations - 1) { clusterList.clear(); } } return clusterList; }}package com.clustering;/** * An interface defining a Clusterer. A Clusterer groups documents into Clusters based on similarity * of their content. */public interface Clusterer { /** Cluster the provided list of documents. */ public ClusterList cluster(DocumentList documentList); public ClusterList runKMeansClustering(DocumentList documentList, int k);}

针对接口Clusterer ,其包含两类实现方法,其一是自动确定k数目的方法;其二是用户自定义k值的方法。

小编推荐一个学Java的学习裙【 七六零,二五零,五四一 】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!

结果输出部分

该部分,是自己写的一个类,用于输出聚类结果,以及类单词出现的概率(这里直接计算的是单词在该类中的频率),可自行定义输出topk个单词。具体代码如下:

package com.clustering;import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Hashtable;import java.util.List;import java.util.Map;import java.util.Map.Entry;public class OutPutFile { public static void outputdocument(String strDir,ClusterList clusterList) throws IOException{ BufferedWriter Writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( new File(strDir)),"gbk")); for (Cluster cluster : clusterList) { // System.out.println(cluster1.getDocuments()); String text = ""; for (Document doc: cluster.getDocuments()) { text +=doc.getContents()+" "; } Writer.write(text+" "); } Writer.close(); } public static void outputcluster(String strDir,ClusterList clusterList) throws IOException{ BufferedWriter Writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( new File(strDir)),"gbk")); Writer.write(clusterList.toString()); Writer.close(); } public static void outputclusterwprdpro(String strDir,ClusterList clusterList,int topword) throws IOException{ BufferedWriter Writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( new File(strDir)),"gbk")); Hashtable clusterdocumentlist = new Hashtable(); int clusterid=0; for (Cluster cluster : clusterList) { String text = ""; for (Document doc: cluster.getDocuments()) { text +=doc.getContents()+" "; } clusterdocumentlist.put(clusterid,text); clusterid++; } for (Integer key : clusterdocumentlist.keySet()) { Writer.write("Topic" + new Integer(key) + " "); List> list=oneclusterwprdpro(clusterdocumentlist.get(key)); int count=0; for (Map.Entry mapping : list) { if (count<=topword) { Writer.write(" " + mapping.getKey() + " " + mapping.getValue()+ " "); count++; }else { break; } } } Writer.close(); } //词频统计并排序 public static List> oneclusterwprdpro(String text){ Hashtable wordCount = new Hashtable(); String arry[] =text.split("\s+"); //词频统计 for (int i = 0; i < arry.length; i++) { if (!wordCount.containsKey(arry[i])) { wordCount.put(arry[i], Integer.valueOf(1)); } else { wordCount.put(arry[i], Integer.valueOf(wordCount.get(arry[i]).intValue() + 1)); } } //频率计算 Hashtable wordpro = new Hashtable(); for (java.util.Map.Entry j : wordCount.entrySet()) { String key = j.getKey(); double value = 1.0*j.getValue()/arry.length; wordpro.put(key, value); } //将map.entrySet()转换成list List> list = new ArrayList>(wordpro.entrySet()); Collections.sort(list, new Comparator>() { //降序排序 public int compare(Entry o1, Entry o2) { //return o1.getValue().compareTo(o2.getValue()); return o2.getValue().compareTo(o1.getValue()); } }); return list; }}

主方法

package web.main;import java.io.IOException;import com.clustering.ClusterList;import com.clustering.Clusterer;import com.clustering.CosineDistance;import com.clustering.DistanceMetric;import com.clustering.DocumentList;import com.clustering.Encoder;import com.clustering.KMeansClusterer;import com.clustering.OutPutFile;import com.clustering.TfIdfEncoder;/** * Solution for Newsle Clustering question from CodeSprint 2012. This class implements clustering of * text documents using Cosine or Jaccard distance between the feature vectors of the documents * together with k means clustering. The number of clusters is adapted so that the ratio of the * intracluster to intercluster distance is below a specified threshold. */public class ClusterDocumentsArgs { private static final int CLUSTERING_ITERATIONS = 30; private static final double CLUSTERING_THRESHOLD = 0.5; private static final int NUM_FEATURES =10000; private static final int k = 30; //自行定义k /** * Cluster the text documents in the provided file. The clustering process consists of parsing and * encoding documents, and then using Clusterer with a specific Distance measure. */ public static void main(String[] args) throws IOException { String fileinput = "/home/qianyang/kmeans/webdata/content"; DocumentList documentList = new DocumentList(fileinput); Encoder encoder = new TfIdfEncoder(NUM_FEATURES); encoder.encode(documentList); System.out.println(documentList.size()); DistanceMetric distance = new CosineDistance(); Clusterer clusterer = new KMeansClusterer(distance, CLUSTERING_THRESHOLD, CLUSTERING_ITERATIONS); ClusterList clusterList = clusterer.runKMeansClustering(documentList, k);// ClusterList clusterList = clusterer.cluster(documentList); //输出聚类结果 OutPutFile.outputcluster("/home/qianyang/kmeans/result/cluster"+k,clusterList); //输出topk个单词 OutPutFile.outputclusterwprdpro("/home/qianyang/kmeans/result/wordpro"+k+"and10", clusterList, 10); OutPutFile.outputclusterwprdpro("/home/qianyang/kmeans/result/wordpro"+k+"and15", clusterList, 15); OutPutFile.outputclusterwprdpro("/home/qianyang/kmeans/result/wordpro"+k+"and20", clusterList, 20); OutPutFile.outputclusterwprdpro("/home/qianyang/kmeans/result/wordpro"+k+"and25", clusterList, 25); }}

如下图所示为结果,我们可以看出每个簇下面的所聚集的文档有哪些。

如下图所示为簇下单词的频率。

小编推荐一个学Java的学习裙【 七六零,二五零,五四一 】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!

如果感觉基于频率计算得到的topk个单词区分度不明显,可再次使用tf-idf进行处理,这里就不做过多的介绍了。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容