ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 第1关:词云的绘制 ``` FrequencyAnalyzer fr = new FrequencyAnalyzer(); fr.setWordFrequenciesToReturn(200); List<WordFrequency> wordFrequencies = fr.load("wordcloud.txt"); Dimension dimension = new Dimension(500, 312); WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT); wordCloud.build(wordFrequencies); wordCloud.writeToFile("imgs/wordcloud_img.png"); return wordCloud; ``` ### 第2关:词云的渲染 ``` java.awt.Font font = new java.awt.Font("宋体",3, 24); wordCloud.setKumoFont(new KumoFont(font)); wordCloud.setPadding(2); wordCloud.setBackgroundColor(Color.WHITE); wordCloud.setBackground(new PixelBoundryBackground("myImgs/whale_small.png")); wordCloud.setColorPalette(new ColorPalette(Color.RED, Color.BLUE, Color.GREEN)); wordCloud.setWordStartStrategy(new CenterWordStart()); wordCloud.setAngleGenerator(new AngleGenerator(0)); ``` ### 第3关:获取酒店评论数据生成词云 ``` TableName tableName = TableName.valueOf(Bytes.toBytes("comment_word_count")); Table table = conn.getTable(tableName); ResultScanner scanner = table.getScanner(new Scan()); List<WordFrequency> words = new ArrayList<>(); for (Result result : scanner) { String word = new String(result.getRow(), "utf-8"); int count = Bytes.toInt(result.getValue("word_info".getBytes(),"count".getBytes())); WordFrequency wordFrequency = new WordFrequency(word,count); if(count>10){ words.add(wordFrequency); } } Dimension dimension = new Dimension(500,312); WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT); wordCloud.setPadding(2); Font font = new Font("宋体", 2, 24); wordCloud.setKumoFont(new KumoFont(font)); wordCloud.setColorPalette(new LinearGradientColorPalette(Color.RED, Color.BLUE, Color.GREEN, 30, 30)); wordCloud.setBackgroundColor(Color.WHITE); wordCloud.setBackground(new PixelBoundryBackground("myImgs/whale_small.png")); wordCloud.setWordStartStrategy(new CenterWordStart()); wordCloud.setAngleGenerator(new AngleGenerator(0)); wordCloud.build(words); ```