先来看看这个图,这个是由echarts-wordcloud生成的一个词云图,其中包含的是一些数据中出现的高频词汇。这样的词云图能直观的展示一段时间内的高频热词,能在一定程度上起到一个分析数据的作用。
想要实现这个功能,一共分为三步。第一,中文分词,实现大段文字解析词语。第二,高频词统计,统计词语出现次数,并且排序,取其中排名靠前是词语。第三,生成词云,界面展示。
一、中文分词
使用IKAnalyzer实现中文分词
1、引入pom依赖
<!-- https://mvnrepository.com/artifact/com.jianggujin/IKAnalyzer-lucene --> <dependency> <groupId>com.jianggujin</groupId> <artifactId>IKAnalyzer-lucene</artifactId> <version>8.0.0</version> </dependency>
2、引入配置文件到resource目录
IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>IK Analyzer 扩展配置</comment> <!--用户可以在这里配置自己的扩展字典 --> <entry key="ext_dict">ext.dic;</entry> <!--用户可以在这里配置自己的扩展停止词字典--> <entry key="ext_stopwords">stopword.dic;</entry> </properties>
3、创建分词对象
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.*; import lombok.experimental.Accessors; import javax.validation.constraints.NotEmpty; @Data @NoArgsConstructor @AllArgsConstructor @Builder @Accessors(chain = true) @ToString(callSuper = true) @EqualsAndHashCode(callSuper = false) @ApiModel(value = "IkAnalyzerResult", description = "Ik分词结果") public class IkAnalyzerResult { /** * 分词内容 */ @ApiModelProperty(value = "分词内容") private String term; /** * 分词类型 CN_CHAR 中文字符 CN_WORD 中文单词 ARABIC 阿拉伯数字 ENGLISH 英文 */ @ApiModelProperty(value = "分词类型 CN_CHAR 中文字符 CN_WORD 中文单词 ARABIC 阿拉伯数字 ENGLISH 英文") private String type; /** * 分词起始下标 */ @ApiModelProperty(value = "分词起始下标") private Integer startOffset; /** * 分词结束下标 */ @ApiModelProperty(value = "分词结束下标") private Integer endOffset; }
4、实现分词功能
/** * * @param analysisText 分析文本 * @param useSmart 是否开启智能分词 * @param typeList 显示分词类型 如不设置则不进行过滤 CN_CHAR 中文字符 CN_WORD 中文单词 ARABIC 阿拉伯数字 ENGLISH 英文 * @return List<IkAnalyzerResult> * @throws IOException */ public static List<IkAnalyzerResult> IkAnalyzerText(String analysisText,boolean useSmart,List<String> typeList) throws IOException { // 返回结果 List<IkAnalyzerResult> resultList=new ArrayList<IkAnalyzerResult>(); //创建一个标准分析器对象 是否开启智能分词 Analyzer analyzer=new IKAnalyzer(useSmart); //获取tokenStream对象 //参数1域名 2要分析的文本内容 TokenStream tokenStream=analyzer.tokenStream("",analysisText); //添加引用,用于获取每个关键词 CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class); //获取当前分词的类型 TypeAttribute typeAttribute = tokenStream.addAttribute(TypeAttribute.class); //添加一个偏移量的引用,记录了关键词的开始位置以及结束位置 OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class); //将指针调整到列表的头部 tokenStream.reset(); //遍历关键词列表,incrementToken判断是否结束 while (tokenStream.incrementToken()) { // 分词类型 String type =String.valueOf(typeAttribute.type()); // 类型集合不为空 则按类型 if(CollectionUtil.isNotEmpty(typeList)){ // 包含的类型,才加入结果中 if(typeList.contains(type)){ // 创建分词对象 IkAnalyzerResult ikAnalyzerResult=new IkAnalyzerResult(); // 设置分词内容 ikAnalyzerResult.setTerm(String.valueOf(charTermAttribute)); // 设置分词类型 ikAnalyzerResult.setType(type); // 设置分词起始下标 ikAnalyzerResult.setStartOffset(offsetAttribute.startOffset()); // 设置分词结束下标 ikAnalyzerResult.setStartOffset(offsetAttribute.endOffset()); // 添加到集合中 resultList.add(ikAnalyzerResult); } // 集合类型为空 则全部添加到返回结果中 }else{ // 创建分词对象 IkAnalyzerResult ikAnalyzerResult=new IkAnalyzerResult(); // 设置分词内容 ikAnalyzerResult.setTerm(String.valueOf(charTermAttribute)); // 设置分词类型 ikAnalyzerResult.setType(type); // 设置分词起始下标 ikAnalyzerResult.setStartOffset(offsetAttribute.startOffset()); // 设置分词结束下标 ikAnalyzerResult.setStartOffset(offsetAttribute.endOffset()); // 添加到集合中 resultList.add(ikAnalyzerResult); } } tokenStream.close(); return resultList; }
二、高频词统计
/** * 高频词统计和排序 * @param ikAnalyzerResultList 分词结果集合 * @param sortFlag 排序方式 asc 正序 desc 倒序 * @param statisticsCount 统计数量(如果最终数量大于统计数量则以统计数量为准) * @return String元素 包括分词内容+"|"+出现频率 */ public static List<String> HighWordFrequencyStatisticsAndSort(List<IkAnalyzerResult> ikAnalyzerResultList,String sortFlag,Integer statisticsCount){ // 返回结果 List<String> resultList=new ArrayList<String>(); // 分词频率统计map Map<String,Integer> wordFrequencyMap=new HashMap<String,Integer>(); // 判断传入分词结果集合不为空 if(CollectionUtil.isNotEmpty(ikAnalyzerResultList)){ // 遍历分词结果 ikAnalyzerResultList.stream().forEach(ikAnalyzerResult -> { // map包含该分词 if(wordFrequencyMap.containsKey(ikAnalyzerResult.getTerm())){ // 分词频率+1 wordFrequencyMap.put(ikAnalyzerResult.getTerm(),wordFrequencyMap.get(ikAnalyzerResult.getTerm())+1); }else{ // 分词频率设置为1 wordFrequencyMap.put(ikAnalyzerResult.getTerm(),1); } }); // 遍历 for(Map.Entry<String, Integer> entry:wordFrequencyMap.entrySet()){ // 添加到集合中 resultList.add(entry.getKey()+"|"+entry.getValue()); } // 排序 resultList= resultList.stream().sorted(((o1, o2) -> { Integer count1=Integer.parseInt(o1.substring(o1.lastIndexOf("|")+1)); Integer count2=Integer.parseInt(o2.substring(o2.lastIndexOf("|")+1)); // 正序 if("asc".equals(sortFlag)){ return count1 - count2; // 倒序 }else { return count2 - count1; } })).collect(Collectors.toList()); } // 如果结果数量大于统计数量,则截取统计数量的数据 if(resultList.size()>statisticsCount){ resultList=resultList.subList(0,statisticsCount); } return resultList; }
三、生成词云
1、词云数据接口调用
// 词云list List<Map<String,Object>> chartsWordCloudEvalKeyList=new ArrayList<Map<String,Object>>(); // 设置过滤词类型 List<String> typeList=new ArrayList<String>(); typeList.add("CN_WORD"); // 文本分词 List<IkAnalyzerResult> ikAnalyzerResultList= IkAnalyzerUtil.IkAnalyzerText(analysisContent.toString(),true,typeList); // 高频词统计 List<String> result=IkAnalyzerUtil.HighWordFrequencyStatisticsAndSort(ikAnalyzerResultList,"desc",100); // 判断不为空 if(CollectionUtil.isNotEmpty(result)){ // 遍历结果集 result.stream().forEach(s -> { // 获取名称 String name=s.substring(0,s.lastIndexOf("|")); // 获取词频 String value=s.substring(s.lastIndexOf("|")+1); // 创建map对象 Map<String,Object> chartsMap=new HashMap<String,Object>(); // 设置值 chartsMap.put("name",name); chartsMap.put("value",value); // 添加到集合中 chartsWordCloudEvalKeyList.add(chartsMap); }); }
2、echarts-wordcloud实现词云(vue版本)
<template> <div :class="className" :style="{height:height,width:width}" /> </template> <script> import echarts from 'echarts' require('echarts/theme/macarons') require('echarts-wordcloud') import resize from '@/views/mobile-nurse/rule/data-analysis/mixins/resize.js' import chartNoData from '@/assets/index/chartNoData.png' export default { mixins: [resize], props: { echartsData: { type: Array, default: function() { return [] } }, className: { type: String, default: 'chart' }, width: { type: String, default: '100%' }, height: { type: String, default: '250px' } }, data() { return { chart: null, echartsDataObj: {}, echartsDataOptions: {}, imageData: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAO4AAADICAYAAADvG90JAAAWNElEQVR4Xu2dedS/5ZzHX/6YmVJRKi1ojwqjydaqIilJacgkhFSYM5UkSyiFSpaypIXRiJBjyJqTZBjLjL2hxZpMRqEkSxznzJz3dH1PT0/P83zv5bqv5b7fn3O+5+l3uu/r8/m8r/t9L9f1We6GxQgYgeoQuFt1FttgI2AEMHF9ERiBChEwcSucNJtsBExcXwNGoEIETNwKJ80mGwET19dAbAS2BDYNv/sA9wLWAtYEVpuj7M/Ab8LvZuB64Mrw+3lsQ2sez8Stefby2r4TsBtw/wVEve+AJv0C+BxwOfAZ4GcD6ip+aBO3+CkqxsB1gKcBewK7Aqtntuwa4JPA24CfZLYluXoTNznkVSlcA/h74On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OBeNvGLerZXsXg7gzDxVwoYgYhbJZxoXAb0MVzDcAN43LtTq9iUncGQJKDlDB6qfUCYmtXoDAr4Azwm6CyGspBIEhiDtzTYkD+gY+rBBfbUZzBBT8r6erM3eaY5b0yCGJO3NE8coKnVTRcYVNWspF4DLgXcD7yzXRlgmBFMSdIa14Z3WG1yq04p0tZSDwTeB9oeud92DLmJO5VqQk7kJjjgCODMnyc430AdER+CHwAeAC4PvRR/eAgyOQi7gzx5Q4r1BKrUQ7mGPY6Vb88EWBsEqxs1SMQG7iLoTuwJCv+fiK8SzN9F8EsoqwXyrNONvTHYGSiDvzQnm+qmC/L7APcO/u7k3yTO2zfjg8WZVWZxkhAiUSdzHMOwQC7w1sN8I5iOGSvlkvBy4GPhljQI9RNgI1EHchgmsDe4Un8p6Awi2nKD8APr/g5wD/iV0FtRF38fQ8FNgJ0FNZP6UdjlFmT9QZWU3UMc5yC59qJ+5iV/U9LCLvCDw81GXeoAUeuQ/Vk/Qq4BpA5V709wrg1tyGWX9ZCIyNuEuhuyqwJaAysZuHvwrHVDVKkVrVKlOJYn/VPlI/pcNdG/ZRRdIrUxlhPfUjMAXizpulVUJZWZFYP0V1rQWsFvKM/yb81XGz318Bfwo/pbrN/lt/Z/++ZRFJXfx73kz4/zdGwMRtDJUPNALlIGDiljMXtsQINEbAxG0MlQ80AuUgYOKWMxe2xAg0RsDEbQyVDzQC5SBg4pYzF7bECDRG4P8A3SKu5/rwGYoAAAAASUVORK5CYII=' } }, watch: { echartsData: { handler(val) { this.echartsDataObj = val this.initChart() }, immediate: true } }, beforeDestroy() { if (!this.chart) { return } this.chart.dispose() this.chart = null }, methods: { initChart(data) { data = this.echartsDataObj this.echartsDataOptions = {} this.echartsDataOptions.title = { text: '关键字词云', x: 'center' } if (!data.length) { this.echartsDataOptions.title = { text: ['{a|}', '{b|暂无数据}'].join('\n'), x: 'center', y: 'center' } this.echartsDataOptions.title.textStyle = { rich: { a: { backgroundColor: { image: chartNoData }, height: 40 }, b: { color: '#C2C7CF' } } } } const maskImage = new Image() maskImage.src = this.imageData const _that = this maskImage.onload = function() { _that.echartsDataOptions.tooltip = {} _that.echartsDataOptions.legend = {} _that.echartsDataOptions.series = [{ type: 'wordCloud', gridSize: 1, sizeRange: [12, 55], rotationRange: [-45, 0, 45, 90], maskImage: maskImage, textStyle: { normal: { color: function() { return 'rgb(' + Math.round(Math.random() * 255) + ', ' + Math.round(Math.random() * 255) + ', ' + Math.round(Math.random() * 255) + ')' } } }, left: 'center', top: 'center', right: null, bottom: null, data: data }] _that.chart = echarts.init(_that.$el, 'macarons') _that.chart.clear() _that.chart.setOption(_that.echartsDataOptions) _that.chart.resize() } } } } </script>
注意事项:
echarts初始化方法需要在onload方法中,否则会出现词云加载不出来的问题。
发表评论