帮我写一段根据数据库查询的词语统计成的热评词云的主要实现代码 Java
时间: 2024-02-15 09:03:22 浏览: 63
好的,以下是一个简单的实现代码,你可以根据自己的需求进行修改和优化:
```java
import java.sql.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.category.*;
import org.jfree.chart.renderer.category.*;
public class WordCloud {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/your_database_name";
static final String USER = "your_database_username";
static final String PASS = "your_database_password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "SELECT word, count FROM word_count";
ResultSet rs = stmt.executeQuery(sql);
Map<String, Integer> map = new HashMap<>();
while(rs.next()) {
String word = rs.getString("word");
int count = rs.getInt("count");
map.put(word, count);
}
rs.close();
stmt.close();
conn.close();
createWordCloud(map);
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(stmt!=null) stmt.close();
} catch(SQLException se2) {
}
try {
if(conn!=null) conn.close();
} catch(SQLException se) {
se.printStackTrace();
}
}
}
public static void createWordCloud(Map<String, Integer> map) throws IOException {
List<WordFrequency> frequencies = new ArrayList<>();
for(Map.Entry<String, Integer> entry : map.entrySet()) {
String word = entry.getKey();
int count = entry.getValue();
frequencies.add(new WordFrequency(word, count));
}
WordCloudGenerator wordCloud = new WordCloudGenerator(800, 600, frequencies);
BufferedImage image = wordCloud.generate();
File file = new File("wordcloud.png");
ImageIO.write(image, "png", file);
}
static class WordCloudGenerator {
private final int width;
private final int height;
private final List<WordFrequency> frequencies;
private final Random random = new Random();
public WordCloudGenerator(int width, int height, List<WordFrequency> frequencies) {
this.width = width;
this.height = height;
this.frequencies = frequencies;
}
public BufferedImage generate() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for(WordFrequency frequency : frequencies) {
dataset.addValue(frequency.getCount(), "Words", frequency.getWord());
}
JFreeChart chart = ChartFactory.createBarChart("", "", "", dataset, PlotOrientation.HORIZONTAL, false, false, false);
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setRangeGridlinePaint(Color.BLACK);
plot.getDomainAxis().setVisible(false);
plot.getRangeAxis().setVisible(false);
plot.getRenderer().setSeriesPaint(0, Color.BLACK);
BufferedImage image = chart.createBufferedImage(width, height);
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = result.createGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(image, 0, 0, null);
for(WordFrequency frequency : frequencies) {
int fontSize = (int)(Math.log(frequency.getCount()) / Math.log(2) * 10) + 20;
g.setFont(new Font("SansSerif", Font.BOLD, fontSize));
g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
g.drawString(frequency.getWord(), random.nextInt(width), random.nextInt(height));
}
g.dispose();
return result;
}
}
static class WordFrequency {
private final String word;
private final int count;
public WordFrequency(String word, int count) {
this.word = word;
this.count = count;
}
public String getWord() {
return word;
}
public int getCount() {
return count;
}
}
}
```
上述代码中的 `word_count` 表是一个包含词语和出现次数的表,你需要根据自己的数据库表结构进行修改。此外,代码中使用了 JFreeChart 和 BufferedImage 类来生成词云图,你需要先将这两个类的相关库添加到你的项目中。
阅读全文