首页 > 编程语言 >终于实现python的词云啦~~~

终于实现python的词云啦~~~

时间:2023-10-11 16:58:38浏览次数:48  
标签:java name python value 终于 import 词云 public String

1、准备好一个txt文件(我是直接在python项目里面创建了一个txt文件)

2、编写代码分词,并连接到Navicat的mysql数据库,将分词之后的数据导入进去:

import jieba
import numpy as np
import pymysql

txt = open("hlm.txt","r",encoding='utf-8').read()
words = jieba.lcut(txt) # 进行精确分词
counts = {}

for word in words:
    if len(word) == 1:
        continue
    if word.isdigit():
        continue
    else:
        counts[word] = counts.get(word,0)+1

items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)

print(len(words))

# 建立数据库连接
db_conn = pymysql.connect(host='localhost',user='root',password='20214063',db='test',charset='utf8')

print("连接数据库成功!")
# 创建游标对象
cur = db_conn.cursor()

# 执行sql语句
for i in range(len(words)):
    word,count = items[i]
    # print(word,count)
    data = (word,count)
    # print("data  ", data)
    try:
        cur.execute("insert into alldata values('%s','%s')" % (word,count))
        db_conn.commit()
    except Exception as err:
        print(err)

3、编写java代码,实现词云

在IDEA里面使用JDBC连接数据库即可:

//DBConnection.java
package org.example;

import java.sql.*;

public class DBConnection {
    public static Connection getConn() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");

        String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=true";

        String username="root";
        String password="20214063";

        Connection connection= DriverManager.getConnection(url,username,password);

        return connection;
    }
}


//alldata.java
package org.example;

public class allData {
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public allData(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public allData() {
    }

    @Override
    public String toString() {
        return "allData{" +
                "name='" + name + '\'' +
                ", value='" + value + '\'' +
                '}';
    }

    private String name;
    private String value;

}


//allMapper.java
package org.example;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class allMapper {
    public static List<allData> getAll() throws SQLException, ClassNotFoundException {
        List<allData> list=new ArrayList<>();
        Connection connection=DBConnection.getConn();

        String sql="select * from alldata";

        Statement statement=connection.createStatement();
        ResultSet resultSet=statement.executeQuery(sql);

        while(resultSet.next()){
            allData a=new allData();
            a.setName(resultSet.getString(1));
            a.setValue(resultSet.getString(2));
            list.add(a);
        }

        resultSet.close();
        statement.close();
        connection.close();

        return list;
    }
}


//test.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>后端数据词云图</title>
  <!-- 引入 echarts.js -->
  <script src="JS/echarts.min.js"></script>
  <script src="JS/echarts-wordcloud.min.js"></script>
  <script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main" style="width: 100%;height:500px;"></div>
<script type="text/javascript">
  // 基于准备好的dom,初始化echarts实例
  var myChart = echarts.init(document.getElementById('main'));

  //获取后端数据
  var word1=[];
  var count1=[];
  var myData={};

  $.ajax({
    url:"/allServlet",
    type:"post",
    async:true,
    dataType:"json",
    success:function (data){
      for(var i=0;i<data.length;i++){
        var d={};
        d["name"]=data[i].name;
        d["value"]=data[i].value;
        myData.push(data);

        word1.push(data[i].name);
        count1.push(data[i].value);
        alert(myData);
      }
    }
  })

  // 指定图表的配置项和数据
  var option = {
    title: {
      text: 'hlm段落一词云图',
      /*subtext: '纯属虚构',*/
      left: 'center'
    },
    tooltip: {},
    series: [
      {
        type : 'wordCloud',  //类型为字符云
        shape:'smooth',  //平滑
        gridSize : 8, //网格尺寸
        size : ['50%','60%'],
        //sizeRange : [ 50, 100 ],
        rotationRange : [-45, 0, 45, 90], //旋转范围
        textStyle : {
          normal : {
            fontFamily:'微软雅黑',
            color: function() {
              return 'rgb(' +
                      Math.round(Math.random() * 255) +
                      ', ' + Math.round(Math.random() * 255) +
                      ', ' + Math.round(Math.random() * 255) + ')'
            }
          },
          emphasis : {
            shadowBlur : 5,  //阴影距离
            shadowColor : '#333'  //阴影颜色
          }
        },
        left: 'center',
        top: '-10%',
        right: null,
        bottom: null,
        width:'100%',
        height:'100%',
        data:word1,count1
      }
    ]
  };

  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option);
</script>
</body>
</html>


//allServlet.java
package org.example;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/allServlet")
public class allServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        try {
            List<allData> list=allMapper.getAll();

            ObjectMapper mapper=new ObjectMapper();

            String json=mapper.writeValueAsString(list);

            resp.getWriter().write(json);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

我先用静态的测试了一把:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>ECharts</title>
  <!-- 引入 echarts.js -->
  <script src="JS/echarts.min.js"></script>
  <script src="JS/echarts-wordcloud.min.js"></script>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main" style="width: 100%;height:500px;"></div>
<script type="text/javascript">
  // 基于准备好的dom,初始化echarts实例
  var myChart = echarts.init(document.getElementById('main'));

  // 指定图表的配置项和数据
  var option = {
    title: {
      text: '高频知识点词云图',
      /*subtext: '纯属虚构',*/
      left: 'center'
    },
    tooltip: {},
    series: [
      {
        type : 'wordCloud',  //类型为字符云
        shape:'smooth',  //平滑
        gridSize : 8, //网格尺寸
        size : ['50%','60%'],
        //sizeRange : [ 50, 100 ],
        rotationRange : [-45, 0, 45, 90], //旋转范围
        textStyle : {
          normal : {
            fontFamily:'微软雅黑',
            color: function() {
              return 'rgb(' +
                      Math.round(Math.random() * 255) +
                      ', ' + Math.round(Math.random() * 255) +
                      ', ' + Math.round(Math.random() * 255) + ')'
            }
          },
          emphasis : {
            shadowBlur : 5,  //阴影距离
            shadowColor : '#333'  //阴影颜色
          }
        },
        left: 'center',
        top: '-10%',
        right: null,
        bottom: null,
        width:'100%',
        height:'100%',
        data:[
          {value: 335, name: 'Hbase'},
          {value: 310, name: 'fastDFS'},
          {value: 234, name: 'Kafaka'},
          {value: 435, name: 'Vue'},
          {value: 1548, name: 'Jquery'},
          {value: 310, name: 'MongoDB'},
          {value: 1400, name: 'Mysql'},
          {value: 1350, name: 'Redis'},
          {value: 5548, name: 'Java'},

          {value: 350, name: 'Mysql'},
          {value: 2010, name: 'Oracle'},
          {value: 234, name: 'Redis'},
          {value: 2345, name: 'Html'},
          {value: 138, name: 'Jquery'},
          {value: 310, name: 'CSS'},
          {value: 1400, name: 'H5'},
          {value: 135, name: 'Aof'},
          {value: 253, name: 'Rdb'},

          {value: 3135, name: '多线程'},
          {value: 305, name: 'JVM虚拟机'},
          {value: 2234, name: '进程与线程'},
          {value: 435, name: '接口'},
          {value: 1580, name: '类'},
          {value: 510, name: '多态'},
          {value: 3600, name: '散点图'},
          {value: 2350, name: '聚点图'},
          {value: 650, name: '热点图'}
        ],
      }
    ]
  };

  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option);
</script>
</body>
</html>

标签:java,name,python,value,终于,import,词云,public,String
From: https://www.cnblogs.com/liuzijin/p/17757253.html

相关文章

  • 【20230613】【Python基础教程】第一章 基础知识
    第一章基础知识I1.4数字与表达python3.x会进行一些浮点数的计算点击查看代码print(1/2)#浮点计算print(1//2)#整除实现结果只保留整数部分print(1%2)#取余保留余数print(2**3)#幂函数结果如下:点击查看代码0.50181.4.1长整型数python3.x......
  • python xmind生成
    importxmindclassAddXmind:def__init__(self,topic_name:str,path:str):self.topic_name=topic_name#主题名称self.path=path#绝对路径defadd(self,child:list):workbook=xmind.load(self.path)first_sh......
  • python32days
    异常元类—————————————————————————————————————————————异常就是错误发生的信号,我们需要对该信号做处理,如果不处理,往后的代码就不能执行了异常的分类 逻辑错误#是允许出现的,但是呢,编程的时候尽量避免逻辑错误的发生语法错......
  • Python-pip配置国内镜像源
    推荐的国内镜像站[个人推荐清华大学pypi镜像站(https://mirrors.tuna.tsinghua.edu.cn/help/pypi/),每五分钟同步一次,资源丰富,下载速度很快]:清华大学:https://pypi.tuna.tsinghua.edu.cn/simple阿里云:http://mirrors.aliyun.com/pypi/simple/豆瓣:http://pypi.douban.com/simple/......
  • 用Python爬虫实现个性化搜索优化
    在当今信息爆炸的时代,传统的SEO手段已经不再足够。为了更好地满足用户个性化需求,我们需要突破传统SEO的限制,采用更智能、更个性化的优化方法。本文将介绍如何利用Python爬虫实现个性化搜索优化,让您的网站在用户搜索中脱颖而出。通过个性化搜索优化,您将能够更好地了解用户需求、提供......
  • python报错cannot import name ‘compare_ssim‘ from ‘skimage.measure‘
    问题原因: scikit-image版本过高导致 解决办法: 重装,安装低版本 pipuninstallscikit-imagepipinstallscikit-image==0.15.0-U-ihttps://pypi.tuna.tsinghua.edu.cn/simple ......
  • 《最新出炉》系列初窥篇-Python+Playwright自动化测试-18-处理鼠标拖拽-上篇
    1.简介本文主要介绍两个在测试过程中可能会用到的功能:在selenium中宏哥介绍了Actions类中的拖拽操作和Actions类中的划取字段操作。例如:需要在一堆log字符中随机划取一段文字,然后右键选择摘取功能。playwright同样可以实现元素的拖拽和释放的操作。2.拖拽操作鼠标拖拽操作,顾名......
  • python:exec和eval函数使用
    我的案例方法:#函数公共配置defdebug_function(debug_req,function_text):try:exec(function_text)re=eval(debug_req)return{'code':200,'msg':'获取成功','data':re}exceptExceptionase:......
  • Python模板字符串Template如:${变量名称}
    1.概述如果你在操作字符串,如果你操作的字符串内容很多,希望字符串中的内容能够根据规则动态替换,并且在长篇幅的字符串中需要替换任意位置任意次数的字符,使用str提供的replace方法代码会写的非常复杂,且出错不易排查。在这个场景中试试Template类把,他能够创建一个模板替换字符串。......
  • 1、python脚本模拟登陆启信宝
    ##coding:utf-8#importrequests#fromlxmlimportetree#classlogin(object):#def__init__(self):#self.headers={#'Referer':'http://www.qixin.com/auth/login?return_url=%2F',#'User-......