1.Java使用HanLp提取关键词短语

首先HanLp是开源免费的,可以直接引入依赖

  <dependency>
            <groupId>com.hankcs</groupId>
            <artifactId>hanlp</artifactId>
            <version>portable-1.8.4</version>
  </dependency>

小伙伴们如果有兴趣可以查看官方文档,写的非常详细,我这里就是用的 关键词提取的功能

示例代码如下:

String content = "世嘉:《索尼克:超级巨星》销量不佳与《马里奥惊奇》撞车有关";
List<String> keywordList = HanLP.extractKeyword(content, 2);
System.out.println(keywordList);

 官方文档:

https://github.com/hankcs/HanLP/tree/1.x#14-%E5%85%B3%E9%94%AE%E8%AF%8D%E6%8F%90%E5%8F%96icon-default.png?t=N7T8https://github.com/hankcs/HanLP/tree/1.x#14-%E5%85%B3%E9%94%AE%E8%AF%8D%E6%8F%90%E5%8F%96

2.Vue实现词云图 

先看效果:

那么我们如何实现如上的词云图呢?直接上代码:

setWordChart = function (detail) {
    const that = this;
    if (that.$refs.wordChart) {
      // 如果已经初始化,则销毁图表实例
      echarts.dispose(this.$refs.wordChart);
    }
    // 获取 chart 元素
    const chartDom = that.$refs.wordChart;
    // 初始化 echarts
    const myChart = echarts.init(chartDom);
    //let data = Object.entries(detail).map(([name, value]) => ({name, value}));//这个detail就是你自己通过handlp获取到的关键词存储到了数据库,通过后端数据库查询到的值传过来来的,我这里是传了一个map回来,关键词和值的key-value形式
    // 示例数据
    let data = [
      { value: 60, name: '运量' },
      { value: 41, name: '延安路' },
      { value: 37, name: '公交' },
      { value: 35, name: '中运量' },
      { value: 15, name: '申昆路' },
      { value: 14, name: '路' },
      { value: 13, name: '上海' },
      { value: 11, name: '车道' },
      { value: 10, name: '电车' },
      { value: 10, name: '终点站' },
      { value: 9, name: '公交车' },
      { value: 9, name: '专用道' },
      { value: 9, name: '载客' },
      { value: 8, name: '无轨电车' },
      { value: 7, name: '开通' },
      { value: 7, name: '外滩' },
      { value: 7, name: '专用' },
      { value: 5, name: '线路' },
      { value: 5, name: '全文' },
      { value: 5, name: '延安东路' },
      { value: 4, name: '延安西路' },
      { value: 4, name: '试运行' },
      { value: 3, name: 'brt' },
      { value: 3, name: '高架' },
      { value: 3, name: '司机' },
      { value: 3, name: '运营' },
      { value: 3, name: '枢纽站' },
      { value: 2, name: '联调' },
      { value: 2, name: '区间' },
      { value: 2, name: '巴士' },
      { value: 2, name: '巨龙' },
      { value: 2, name: '中山东一路' },
      { value: 2, name: '车辆' },
      { value: 2, name: '虹桥' },
      { value: 2, name: '青平公路' },
      { value: 2, name: '乘坐' },
      { value: 2, name: '巨龙车' },
      { value: 2, name: '乘客' },
      { value: 2, name: '线网' },
      { value: 2, name: '通车' },
      { value: 2, name: '车站' },
      { value: 2, name: '去外滩' },
      { value: 2, name: '空载' },
      { value: 2, name: '尝鲜' },
      { value: 2, name: '红灯' },
      { value: 2, name: '体验' },
      { value: 2, name: '安东路外滩' },
      { value: 2, name: '试车' },
      { value: 2, name: '速递' },
      { value: 2, name: '站台' },
      { value: 2, name: '延安' },
      { value: 1, name: '西藏' },
      { value: 1, name: '客流' },
      { value: 1, name: '车厢' },
      { value: 1, name: '上海·七莘' },
      { value: 1, name: '长宁' },
      { value: 1, name: '恶臭' },
      { value: 1, name: '娄山关' },
      { value: 1, name: '天桥' },
      { value: 1, name: '红绿灯' },
      { value: 1, name: '正式' },
      { value: 1, name: '路权' },
      { value: 1, name: '新片' },
      { value: 1, name: '视频' },
      { value: 1, name: '头班车' },
      { value: 1, name: '中路' },
      { value: 1, name: '北路站' },
      { value: 1, name: '传说' },
      { value: 1, name: '小时' },
      { value: 1, name: '路试' },
      { value: 1, name: '单程' },
      { value: 1, name: '路上' },
      { value: 1, name: '拥堵' },
      { value: 1, name: '启用' },
      { value: 1, name: '换乘' },
      { value: 1, name: '调试' },
      { value: 1, name: '中门' },
      { value: 1, name: '凯旋路' },
      { value: 1, name: '候车' },
      { value: 1, name: '双流路' },
      { value: 1, name: '上班' },
      { value: 1, name: '天山路' },
      { value: 1, name: '交通委' },
      { value: 1, name: '市民' },
      { value: 1, name: '发车' },
      { value: 1, name: '上车' },
      { value: 1, name: '开车' },
      { value: 1, name: '驾驶员' },
      { value: 1, name: '路线' },
      { value: 1, name: '样车' },
      { value: 1, name: '华山路' },
      { value: 1, name: '缩线' },
      { value: 1, name: '铰接' },
      { value: 1, name: '马路' },
      { value: 1, name: '凑热闹' },
      { value: 1, name: '高峰' },
      { value: 1, name: '中路站' },
      { value: 1, name: '你好' },
      { value: 1, name: '外滩站' }
    ]
    //因为这里我采用了自定义形状的方式,数据太少,展示不出具体的形状,所以我就把数据扩大了,大家可以结合自身业务情况选择用不用自定义形状
    data = [...data,...data,...data,...data]
 //词云图自定义形状   
const maskImage = new Image();
  //五角星形状 图片可以转Base64,也可以你引入到的项目中直接获取
maskImage.src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAACypJREFUeF7tnW2a1TYMRs1KCjuBlZSupLASupO2K6E7oZi5eRjuTG4kW/6QdPKHH8iJ88onfmUnc98UDhRAgVMF3qANCqDAuQIAwuhAgQcKAAjDAwUAhDGAAm0KMIO06UarJAoASJJEc5ttCgBIm260SqIAgCRJNLfZpgCAtOlGqyQKAEiSRHObbQoASJtutEqiAIAkSTS32aYAgLTpNqrVx1LKf6WUf0ZdgPPqFAAQnV6jo7+UUt6WUj6MvhDnlykAIDKdZkV9vV3o3awLcp3HCgDIPiOk2qs6g9SjziDYrA1yAyAbJOHWhQpHhaQef5VS/tina3l7AiD75L7aq1p/1KMW6tisDXIDIBsk4budel9K+fuuK9isDXIDIBsk4Watjvrj6NGnUsrnPbqXtxcAskfun9uro0fYrA1yAyAbJKGU8u2kG9isxfkBkMUJOLFX2Kz1efnRAwBZn4hanNci/bWj7oWwq74wRwCyUPzbpc/sFcu963PDDLI4B893z8+6UjcM68YhxwIFmEEWiP7sks93z896gs1amCMAWSj+g9Wr571iuXdhjgBknfgSe3X0juXeRXkCkEXC397cPV5OvOoFNutKoUH/DyCDhBWc9rXd87Nm2CyBoCNCAGSE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QWQXXbPe5+6u9Ul4W1WBkB2Wd61HEy7WMZe4MW1wKrADICs/vZDW29Ix8IOdUmrXZTe4/K4DICstFejB9DquiR8HRIdkJX2apb9WF2XWFrH5TPGfQeiA7Jq93zFoFlVl4yeJZdCEx2Q2fZqVL0hHSQr6pLQNisyILPt1S5P0hV1yYoZU/rQ6IqLDMhMezWr3pAme3ZdssvDQaqPOC4yILN2z3d+es6qS8LarKiAzLBXq+sN6VNwVl2y84NCqtWLuKiAjLZX3izFjLok5DciUQEZaa92qzekT8fRdUlImxURkJHffkSwESPrkgj6/PLAiQjIiAHgpd7QzCYjvnv3Orue6hYREGt75a3e0EBi/d17OJsVERDL3fNwT8Q7ekbUJaFsVjRALJd3QyX6YlqxtKWhHirRALH49iNavaGxXBZ1SShLGg2QXnsVKrlSMp7FWeyXhKpDIgHSa69CWYMGOI4mFnVJGHsaCZCe3fMwCe0A475pT10SZiaOBEiLvcpab0g5an2PK4zNigJIi70K85STjvbGuNa6JMSsHAUQrb2i3tDR0lKXhHgARQFEs3se4smmG99m0Zq6JITNigCI1F5Rb9hwoqlL3D+MIgAisVchpnub8W1yFmld4v4bkQiAXNkr6g0TJl6cRFKXuLdZ3gG5+vbD/RQ/ZmybnvWqLnGdA++AnCWHesOUgcuTPapLXM/g3gF5zV5Rb1yO5yEBZ3WJa5vlHZD73XPXT6shw3buSc/qErc2yzMg98u7bpMwdwxPudq99XX74PIMyPHtB/XGlDGvvsjzusSt7fUMSLVXboVXDzefDY66pK42vvN4C14Bqfbqt1LKZ4+iJ+vzUZf8e3ugubp9r4C4EpnO/lCgziJ1xnd1AIirdNHZ2QoAyGzFuZ4rBQDEVbro7GwFAGS24lzPlQIA4ipddHa2AgAyW3Gu50oBAHGVLjo7WwEAma0413OlAIC4Shedna0AgMxWnOu5UgBAXKWLzs5WAEBmK871XCnwP/hMydihzhN5AAAAAElFTkSuQmCC'
    //爱心形状 //maskImage.src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADIC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AmhaclFSaXilxBLhuJpxeBGLkkpIKhlECObNVLxfEitdV3wggmepxHwiEDGgN3O9332PrBEfUhjuD9TkwK+zikB0WL5lqceeCY14IocIJBHc5AaeRjyfO5r0Dhivl1BmE7JGJ+LIIJ2A3vQmHxs+FLF+sI1eoxNvCKQT0G9cJh5XeX97D35d8dqufIUIovmO35+rfMnwOjrevjICcWMEf6wQQCBWdOCMGwIIxI0R/LFCAIFY0YEzbgggEDdG8McKAQRiRQfOuCGAQNwYwR8rBBCIFR0444YAAnFjBH+sEEAgVnTgjBsCCMSNEfyxQgCBWNGBM24IIBA3RvDHCgEEYkUHzrghgEDcGMEfKwQQiBUdOOOGAAJxYwR/rBBAIFZ04IwbAgjEjRH8sUIAgVjRgTNuCCAQN0bwxwoBBGJFB864IYBA3BjBHysE/gON9oHYiHef6wAAAABJRU5ErkJggg=='

    // 配置项
    const option = {
      series: [{
        type: 'wordCloud',
        shape: 'roundRect',
        maskImage: maskImage,
        sizeRange: [10, 30],//设置关键词字体大小
        rotationRange: [0, 0],
        rotationStep: 45,
        gridSize: 10,//设置间隔距离
        drawOutOfBound: false,
        data: data,
        height:'100%',//这里你如果使用自定义的形状,必须保证你的高度足够和宽度足够
        width:'100%',
        textStyle: {
          color: function() {
            // Random color
            return 'rgb(' + [
              Math.round(Math.random() * 160),
              Math.round(Math.random() * 160),
              Math.round(Math.random() * 160)
            ].join(',') + ')'
          }
        },
        emphasis: {
          focus: 'self', // 设置词云图的 emphasis 样式
          textStyle: {
            shadowBlur: 10,
            shadowColor: '#333'
          },
        }
      }],
    };
    // 设置图表配置项
    maskImage.onload =function (){
      myChart.setOption(option);
    }
    myChart.on('click', function (params) {
      that.keyword = params.data.name
      that.doSearch()
    });
  }

注意事项:

1.要使用echats的词云图,需要先前端下载 echarts-wordcloud,下载方式 npm install echarts-wordcloud;

2.小伙伴如果要自己找另外的形状,可以去阿里巴巴矢量图标库里面找,直接下载为png的图片就好了,但是我建议最好不要用线性的图标,因为线性的图可能很难出现你需要的效果,就是页面上不报错,词云图就是一片白板,其实是出现了,但是跟图标形状有关,有的词直接就超出你的范围,就看不到了,我建议用填充(filling)的矢量图,图标库链接如下:

iconfont-阿里巴巴矢量图标库icon-default.png?t=N7T8https://www.iconfont.cn/search/index?searchType=icon&q=%E5%BF%83&page=1&fromCollection=-1&tag=fill

3. 图片下载好了之后,可以有两个选择

        1.图片转Base64,可以去这个转化网站,进行转化,链接如下:base64图片在线转换工具 - 站长工具 (chinaz.com)icon-default.png?t=N7T8https://tool.chinaz.com/tools/imgtobase/

然后复制转化出来的链接,maskImage.src='你转化出来的链接' 

       2.如果不想转Base64也是可以的,直接把图片放到你的项目的某个资源包下面,然后获取地址,比如 maskImage.src= require('./image/star.png'),最好用上require,不然有可能你是获取不到那个图片的。

 4.词云图里面的每个关键词是可以绑定点击事件的,我这里就绑定了一个事件,用来查询与关键词相关的数据,代码如下:

myChart.on('click', function (params) {
      that.keyword = params.data.name
      that.doSearch()
    });

    这里要注意的是你当前如果直接使用this的话,是调用不到你外面的其他方法的,你需要指定最开始的this,所以我在最上面定义了 const that = this,这样通过that.方法名()就可以调用啦。

总结:通过如上的操作就可以实现我们上面的效果啦,希望能帮到各位小伙伴!

Logo

前往低代码交流专区

更多推荐