|
|
|
@ -0,0 +1,114 @@
|
|
|
|
|
package com.sztzjy.marketing.util;
|
|
|
|
|
|
|
|
|
|
import com.kennycason.kumo.CollisionMode;
|
|
|
|
|
import com.kennycason.kumo.WordCloud;
|
|
|
|
|
import com.kennycason.kumo.WordFrequency;
|
|
|
|
|
import com.kennycason.kumo.bg.CircleBackground;
|
|
|
|
|
import com.kennycason.kumo.bg.PixelBoundaryBackground;
|
|
|
|
|
import com.kennycason.kumo.font.KumoFont;
|
|
|
|
|
import com.kennycason.kumo.font.scale.SqrtFontScalar;
|
|
|
|
|
import com.kennycason.kumo.image.AngleGenerator;
|
|
|
|
|
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
|
|
|
|
|
import com.kennycason.kumo.nlp.tokenizers.ChineseWordTokenizer;
|
|
|
|
|
import com.kennycason.kumo.palette.ColorPalette;
|
|
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import static cn.hutool.core.io.FileUtil.getInputStream;
|
|
|
|
|
|
|
|
|
|
public class TagWordCloudService {
|
|
|
|
|
|
|
|
|
|
// 照片纵横比
|
|
|
|
|
private double ratio = 1;
|
|
|
|
|
// 获取词云图片
|
|
|
|
|
// wordFile:单词及其频率文件路径
|
|
|
|
|
// pngOutputPath:图片输出路径,应该以.png结尾
|
|
|
|
|
// shapePicPath:词云形状图片路径,其背景应为透明背景,格式为png
|
|
|
|
|
public void generate(String pngOutputPath, String shapePicPath,String context) throws IOException {
|
|
|
|
|
// final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
|
|
|
|
|
// 共检索多少个词
|
|
|
|
|
// frequencyAnalyzer.setWordFrequenciesToReturn(1000);
|
|
|
|
|
// 单词最短长度,一个汉字和一个英文字符都是1
|
|
|
|
|
// frequencyAnalyzer.setMinWordLength(2);
|
|
|
|
|
// frequencyAnalyzer.setStopWords(loadStopWords());
|
|
|
|
|
// 设置中文支持,另一种加载方式不用设置
|
|
|
|
|
// frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
|
|
|
|
|
// final List<WordFrequency> wordFrequencies;
|
|
|
|
|
// // 加载词云有两种方式,一种是在txt文件中统计词出现的个数,另一种是直接给出每个词出现的次数,这里使用第二种
|
|
|
|
|
// // 文件格式如下
|
|
|
|
|
//// 100: frog
|
|
|
|
|
//// 94: dog
|
|
|
|
|
//// 43: cog
|
|
|
|
|
//// 20: bog
|
|
|
|
|
// FrequencyFileLoader frequencyFileLoader = new FrequencyFileLoader();
|
|
|
|
|
// File file = new File(wordFile);
|
|
|
|
|
// wordFrequencies = frequencyFileLoader.load(file);
|
|
|
|
|
// 可以直接从文件中读取
|
|
|
|
|
FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
|
|
|
|
|
frequencyAnalyzer.setWordFrequenciesToReturn(600);
|
|
|
|
|
frequencyAnalyzer.setMinWordLength(2);
|
|
|
|
|
frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
|
|
|
|
|
InputStream inputStream = null;
|
|
|
|
|
List<WordFrequency> wordFrequencies = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
inputStream = new ByteArrayInputStream(context.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
wordFrequencies = frequencyAnalyzer.load(inputStream);
|
|
|
|
|
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}finally {
|
|
|
|
|
if (inputStream != null) {
|
|
|
|
|
try {
|
|
|
|
|
inputStream.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// final Dimension dimension = new Dimension(600, 600);
|
|
|
|
|
// final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
|
|
|
|
|
// wordCloud.setPadding(2);
|
|
|
|
|
// wordCloud.setBackground(new CircleBackground(300));
|
|
|
|
|
// wordCloud.setColorPalette(new ColorPalette(new Color(0x4055F1), new Color(0x408DF1), new Color(0x40AAF1), new Color(0x40C5F1), new Color(0x40D3F1), new Color(0xFFFFFF)));
|
|
|
|
|
// wordCloud.setFontScalar(new SqrtFontScalar(10, 40));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成图片的像素大小
|
|
|
|
|
//设置图片分辨率
|
|
|
|
|
Dimension dimension = new Dimension(600, 600);
|
|
|
|
|
// final Dimension dimension = new Dimension(1024, (int)(1024*ratio));
|
|
|
|
|
final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
|
|
|
|
|
// 调节词云的稀疏程度,越高越稀疏
|
|
|
|
|
wordCloud.setPadding(2);
|
|
|
|
|
|
|
|
|
|
// //设置背景色
|
|
|
|
|
wordCloud.setBackgroundColor(new Color(255,255,255));
|
|
|
|
|
//设置背景图片
|
|
|
|
|
//wordCloud.setBackground(new PixelBoundaryBackground(shapePicPath));
|
|
|
|
|
// wordCloud.setBackground(new CircleBackground(300));
|
|
|
|
|
wordCloud.setBackground(new CircleBackground(300));
|
|
|
|
|
|
|
|
|
|
// 颜色模板,不同频率的颜色会不同
|
|
|
|
|
wordCloud.setColorPalette(new ColorPalette(new Color(0x4055F1), new Color(0x408DF1), new Color(0x40AAF1), new Color(0x40C5F1), new Color(0x40D3F1), new Color(0xFFFFFF)));
|
|
|
|
|
// 设置字体
|
|
|
|
|
Font font = new Font("楷体", 0, 25);
|
|
|
|
|
wordCloud.setKumoFont(new KumoFont(font));
|
|
|
|
|
// 设置偏转角,角度为0时,字体都是水平的
|
|
|
|
|
// wordCloud.setAngleGenerator(new AngleGenerator(0, 90, 9));
|
|
|
|
|
wordCloud.setAngleGenerator(new AngleGenerator(0));
|
|
|
|
|
// 字体的大小范围,最小是多少,最大是多少
|
|
|
|
|
wordCloud.setFontScalar(new SqrtFontScalar(10, 40));
|
|
|
|
|
|
|
|
|
|
wordCloud.build(wordFrequencies);
|
|
|
|
|
wordCloud.writeToFile(pngOutputPath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|