java生成词云

项目有需求生成词云图片,在hub上找到一个开源jar包,记录下,以备以后查阅。

github URL:https://github.com/kennycason/kumo


package com.hp.portal.util;

import java.awt.Color;
import java.awt.Dimension;
import java.io.OutputStream;
import java.util.List;

import com.hp.portal.common.Constants;
import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.RectangleBackground;
import com.kennycason.kumo.font.FontWeight;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.LinearFontScalar;
import com.kennycason.kumo.image.AngleGenerator;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.palette.ColorPalette;

public class WordCloudUtils {

	public static void buildWordCouldByWordFrequencies(int width_img, int height_img, int angle_word, int maxFont,
			int minFont, List wordFrequencies, Color backgroudColor, OutputStream outputStream,
			Color... wordColors) {
		writeToStreamAsPNG(buildWordCouldByWordFrequencies(width_img, height_img, angle_word, maxFont, minFont,
				wordFrequencies, backgroudColor, wordColors), outputStream);
	}

	public static void buildWordCouldByWordFrequencies(int width_img, int height_img, int angle_word, int maxFont,
			int minFont, List wordFrequencies, Color backgroudColor, String filePath,
			Color... wordColors) {
		writeToFile(buildWordCouldByWordFrequencies(width_img, height_img, angle_word, maxFont, minFont,
				wordFrequencies, backgroudColor, wordColors), filePath);
	}

	public static void buildWordCouldByWords(int width_img, int height_img, int angle_word, int maxFont, int minFont,
			List words, Color backgroudColor, OutputStream outputStream, Color... wordColors) {
		writeToStreamAsPNG(buildWordCouldByWordFrequencies(width_img, height_img, angle_word, maxFont, minFont,
				buildWordFrequencies(words), backgroudColor, wordColors), outputStream);
	}

	public static void buildWordCouldByWords(int width_img, int height_img, int angle_word, int maxFont, int minFont,
			List words, Color backgroudColor, String filePath, Color... wordColors) {
		writeToFile(buildWordCouldByWordFrequencies(width_img, height_img, angle_word, maxFont, minFont,
				buildWordFrequencies(words), backgroudColor, wordColors), filePath);
	}

	private static WordCloud buildWordCouldByWordFrequencies(int width_img, int height_img, int angle_word, int maxFont,
			int minFont, List wordFrequencies, Color backgroudColor, Color... wordColors) {
		Dimension dimension = new Dimension(width_img, height_img);
		WordCloud wordCloud = new WordCloud(dimension, CollisionMode.RECTANGLE);
		wordCloud.setPadding(0);
		wordCloud.setBackground(new RectangleBackground(dimension));
		wordCloud.setKumoFont(new KumoFont(Constants.WORDCLOUD_FONT_TYPE, FontWeight.PLAIN));
		wordCloud.setColorPalette(new ColorPalette(wordColors));
		wordCloud.setFontScalar(new LinearFontScalar(minFont, maxFont));
		wordCloud.setBackgroundColor(backgroudColor);
		wordCloud.setAngleGenerator(new AngleGenerator(angle_word));
		wordCloud.build(wordFrequencies);
		return wordCloud;
	}

	private static List buildWordFrequencies(List words) {
		FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
		List wordFrequencies = frequencyAnalyzer.load(words);
		return wordFrequencies;
	}

	private static void writeToFile(WordCloud wordCloud, String filePath) {
		wordCloud.writeToFile(filePath);
	}

	private static void writeToStreamAsPNG(WordCloud wordCloud, OutputStream outputStream) {
		wordCloud.writeToStreamAsPNG(outputStream);
	}
}


你可能感兴趣的:(java)