Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2015 @wolfposd
- *
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
- package word.cloud
- import groovy.transform.Field
- import java.awt.Color
- import java.awt.Font
- import java.math.RoundingMode
- import wordcloud.CollisionMode
- import wordcloud.LayeredWordCloud
- import wordcloud.ParallelLayeredWordCloud
- import wordcloud.bg.PixelBoundryBackground
- import wordcloud.bg.RectangleBackground
- import wordcloud.font.CloudFont
- import wordcloud.font.scale.LinearFontScalar
- import wordcloud.image.AngleGenerator
- import wordcloud.palette.ColorPalette
- import wordcloud.wsc.CenterWordStart
- import wordcloud.wsc.RandomWordStart
- /**
- * @author @wolfposd
- */
- @Field database = new Database("mydatabase.sqlite")
- @Field t3 = database.selectTagsAsWordFrequency("1500");
- measure2()
- def measure2()
- {
- def time1 = measure("ParallelLayeredWordCloud 4Tile") { func2(new ParallelLayeredWordCloud(4, 2000, 2000, CollisionMode.PIXEL_PERFECT), "test4tile-parallel.png")}
- def time2 = measure("NormalLayeredWordCloud 4Tile") { func2(new LayeredWordCloud(4, 2000, 2000, CollisionMode.PIXEL_PERFECT), "test4tile-normal.png")}
- println time1
- println time2
- }
- def func2(LayeredWordCloud lwc, filename)
- {
- final FONT = new Font("Lucida Sans", Font.PLAIN, 10)
- final MAXLAYERS = lwc.getLayers()
- def positions = [[0, 0], [0, 1000], [1000, 0], [1000, 1000]]
- def colors = [Color.RED, Color.WHITE, Color.BLUE, Color.GREEN]
- for(int i = 0; i < MAXLAYERS; i++)
- {
- def cloud = lwc[i]
- cloud.angleGenerator = new AngleGenerator(0)
- cloud.padding = 3
- cloud.startscheme = new CenterWordStart()
- cloud.cloudFont = new CloudFont(FONT)
- cloud.colorPalette = new ColorPalette(colors[i])
- def pos = positions[i]
- cloud.background = new RectangleBackground(pos[0], pos[1], 1000, 1000)
- cloud.fontScalar = new LinearFontScalar(10,40)
- }
- for(int i = 0; i < MAXLAYERS; i++)
- {
- lwc.build(i,t3)
- }
- lwc.writeToFile(filename)
- if(lwc instanceof ParallelLayeredWordCloud)
- {
- lwc.shutdown()
- }
- }
- def measure(def description, def func)
- {
- def start = System.currentTimeMillis()
- func()
- def end = System.currentTimeMillis()
- def time = round((end-start)/1000, 4)
- return "$description took $time secs to compute"
- }
- def round(double value, int places)
- {
- BigDecimal bd = new BigDecimal(value);
- bd = bd.setScale(places, RoundingMode.HALF_UP);
- return bd.doubleValue();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement