旅游网站之数据可视化

旅游网站之数据可视化

第1关:词云的绘制

package com.showdata;

import java.awt.Dimension;
import java.io.IOException;
import java.util.List;

import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;

public class WordCloud_img {

    public WordCloud get() throws IOException {
        /**********     Begin   **********/
        //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;
        /**********     End   **********/
    }
}

第2关:词云的渲染

package com.showdata;

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

import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.PixelBoundryBackground;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.image.AngleGenerator;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.palette.ColorPalette;
import com.kennycason.kumo.wordstart.CenterWordStart;

import java.awt.Font;

public class WordCloud_render {

    public WordCloud get() throws IOException {
        FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        frequencyAnalyzer.setWordFrequenciesToReturn(200);
        List<WordFrequency> wordFrequencies = frequencyAnalyzer.load("wordcloud.txt");
        Dimension dimension = new Dimension(500, 312);
        WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
        /**********     Begin   **********/
        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));
        /**********     End   **********/
        wordCloud.build(wordFrequencies);
        wordCloud.writeToFile("imgs/wordcloud_render.png");
        return wordCloud;
    }
}

第3关:获取酒店评论数据生成词云

package com.showdata;

import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.PixelBoundryBackground;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.image.AngleGenerator;
import com.kennycason.kumo.palette.LinearGradientColorPalette;
import com.kennycason.kumo.wordstart.CenterWordStart;
import com.util.HBaseUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 词云
 */
public class CommentWordCloud {

    public WordCloud get() throws IOException {
        Connection conn = HBaseUtil.getConnection();
        /**********     Begin   **********/
        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);

        wordCloud.writeToFile("imgs/wordcloud_comment.png");
        return wordCloud;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值