当前位置:   article > 正文

旅游网站之数据可视化_数据可视化旅游网站

数据可视化旅游网站

旅游网站之数据可视化

第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   **********/
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

第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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

第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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/409017
推荐阅读
相关标签
  

闽ICP备14008679号