Search in sources :

Example 1 with WordCloud

use of com.kennycason.kumo.WordCloud in project pyramid by cheng-li.

the class ClusterLabels method plot.

public static void plot(Config config) throws Exception {
    BM bm = (BM) Serialization.deserialize(new File(config.getString("output.dir"), "model"));
    double[] coefficients = bm.getMixtureCoefficients();
    int[] sortedComponents = ArgSort.argSortDescending(bm.getMixtureCoefficients());
    File clusterFolder = Paths.get(config.getString("output.dir"), "clusters").toFile();
    clusterFolder.mkdirs();
    FileUtils.cleanDirectory(clusterFolder);
    for (int i = 0; i < sortedComponents.length; i++) {
        int k = sortedComponents[i];
        List<WordFrequency> frequencies = getCluster(bm, k);
        double max = frequencies.stream().mapToDouble(WordFrequency::getFrequency).max().getAsDouble();
        double sum = frequencies.stream().mapToDouble(WordFrequency::getFrequency).sum();
        double ratio = sum / max;
        final Dimension dimension = new Dimension(600, 600);
        final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.RECTANGLE);
        wordCloud.setPadding(0);
        wordCloud.setAngleGenerator(new AngleGenerator(0));
        wordCloud.setBackground(new RectangleBackground(dimension));
        wordCloud.setColorPalette(buildRandomColorPalette(20));
        wordCloud.setBackgroundColor(Color.WHITE);
        wordCloud.setFontScalar(new LinearFontScalar(20, (int) (500 / ratio)));
        wordCloud.setWordStartStrategy(new CenterWordStart());
        wordCloud.build(frequencies);
        File out = Paths.get(config.getString("output.dir"), "clusters", "" + i + "_" + coefficients[k] + ".png").toFile();
        wordCloud.writeToFile(out.getAbsolutePath());
    }
}
Also used : WordFrequency(com.kennycason.kumo.WordFrequency) LinearFontScalar(com.kennycason.kumo.font.scale.LinearFontScalar) BM(edu.neu.ccs.pyramid.clustering.bm.BM) RectangleBackground(com.kennycason.kumo.bg.RectangleBackground) AngleGenerator(com.kennycason.kumo.image.AngleGenerator) WordCloud(com.kennycason.kumo.WordCloud) File(java.io.File) CenterWordStart(com.kennycason.kumo.wordstart.CenterWordStart)

Aggregations

WordCloud (com.kennycason.kumo.WordCloud)1 WordFrequency (com.kennycason.kumo.WordFrequency)1 RectangleBackground (com.kennycason.kumo.bg.RectangleBackground)1 LinearFontScalar (com.kennycason.kumo.font.scale.LinearFontScalar)1 AngleGenerator (com.kennycason.kumo.image.AngleGenerator)1 CenterWordStart (com.kennycason.kumo.wordstart.CenterWordStart)1 BM (edu.neu.ccs.pyramid.clustering.bm.BM)1 File (java.io.File)1