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