文本挖掘之文本聚类(借力打力)
文本挖掘之文本聚类(借力打力)
http://www.cnblogs.com/lyssym/p/4908239.html
简介 在文本相似度判定中,作者将该算法用于文本聚类中,其核心思想通过比较两个文本向量中元素的相似度,即向量中所含的元素相似个数越多,则两个向量越相似,继而上述文本越相似。作者在短文本相似判定中采用了余弦相似度该算法来实现,本文借鉴数学集合取交集,借用现有组件来实现上述算法功能,继而减少工作量,也具备便捷性,也能取得较好的效果。 数学集合 该方法的思想为:先将文本数据采用中文切分,将其切分为词汇(词组、短语、词汇的统称,本文不做细粒度划分),并以某种特征表示进行量化(本文按词频量化),通过取文本向量的交集,若交集的重复率为70%,则判定两篇文本相似。通过将相似文本聚合成链表存储,进而能实现文本聚类。 以下内容则以python中具体实现来阐述。 1 def match_topic(self,keywords,topics): 2 keywords = set(keywords) 3 keywords_count = len(keywords) 4 for topic in topics: 5 intersection = keywords.intersection(topic['keywords']) 6 if len(intersection) > keywords_count * 0.7 or len(intersection) > len(topic[']) * 0.7: 7 yield topic 备注:keywords 制定匹配话题关键字列表,tipics为待匹配文本数据,每条数据为一个JSON对象,其中包含Web文本数据的标题、内容、发布时间等信息。其中涉及到长句与短句的关系,因此文本采用“或”操作来确定匹配话题,实现话题聚类。 Elasticsearch Elasticsearch是全文检索服务器,采用倒排索引对词汇建立索引,因此在文本检索方面具有较大的效率优势。本文将该工具以类似数学集合的方式来组织,继而实现文本聚类。 以下内容则以Java中具体实现来阐述。 1 import org.elasticsearch.action.index.IndexRequestBuilder;
2 import org.elasticsearch.action.index.IndexResponse;
3 import org.elasticsearch.action.search.SearchResponse;
4 import org.elasticsearch.client.Client;
5 import org.elasticsearch.client.transport.TransportClient;
6 import org.elasticsearch.common.transport.InetSocketTransportAddress;
7 import org.elasticsearch.index.query.QueryBuilder;
8 import org.elasticsearch.index.query.QueryBuilders;
9 import org.elasticsearch.search.SearchHit;
10 import org.elasticsearch.search.SearchHits;
11
12 import java.util.List;
13 import org.elasticsearch.node.NodeBuilder.*;
14
15 public class SearchHandler {
16 private Client client;
17
18 public SearchHandler(String ipAddress)
19 {
20 client = new TransportClient().addTransportAddress(new InetSocketTransportAddress(ipAddress,9300));
21 }
22
23
24 void createIndexResponse(String indexName,String type,List<String> data)
25 {
26 IndexRequestBuilder requestBuilder = client.prepareIndex(indexName,type).setRefresh(true);
27 int len = data.size();
28 for (int i = 0; i < len; i++)
29 requestBuilder.setSource(data.get(i)).execute().actionGet();
30
31 }
32
33
34 public IndexResponse createIndexResponse(String indexName,String data)
35 {
36 IndexResponse response = client.prepareIndex(indexName,type).setSource(data).execute().actionGet();
37 return response;
38 }
39
40
41 void searchResponse(QueryBuilder queryBuilder,String indexName,String type)
42 {
43 SearchResponse searchResponse = client.prepareSearch(indexName).setTypes(type)
44 .setQuery(queryBuilder)
45 .setMinScore((float)3)
46 .setSize(10)
47 .execute()
48 .actionGet();
49 SearchHits hits = searchResponse.getHits();
50 System.out.println("查询到记录数=" + hits.getTotalHits());
51 SearchHit[] searchHits = hits.getHits();
52 for (SearchHit hit : searchHits) {
53 String keywords = (String)hit.getSource().get("title");
54 System.out.println(keywords);
55 }
56 }
57 }
3
class Test {
5
6 static void main(String[] args) {
7 SearchHandler sh = new SearchHandler("192.168.101.243");
8 String indexName = "title";
9 String type = "test";
10
11 QueryBuilder qb = QueryBuilders.matchQuery(indexName,"主持政治局常委会").minimumShouldMatch("70%").boost((float)0.2);
12 long start = System.currentTimeMillis();
13 sh.searchResponse(qb,indexName,type);
14 long end = System.currentTimeMillis();
15
16 System.out.println("查询所需时间为: " + (end -start) + " ms");
17 }
18 }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |