Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.61 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2015 @wolfposd
  3.  *
  4.  *
  5.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  6.  * of this software and associated documentation files (the "Software"), to deal
  7.  * in the Software without restriction, including without limitation the rights
  8.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9.  * copies of the Software, and to permit persons to whom the Software is
  10.  * furnished to do so, subject to the following conditions:
  11.  *
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in
  14.  * all copies or substantial portions of the Software.
  15.  *
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23.  * THE SOFTWARE.
  24.  *
  25.  */
  26. package word.cloud
  27.  
  28. import groovy.transform.Field
  29. import java.awt.Color
  30. import java.awt.Font
  31. import java.math.RoundingMode
  32. import wordcloud.CollisionMode
  33. import wordcloud.LayeredWordCloud
  34. import wordcloud.ParallelLayeredWordCloud
  35. import wordcloud.bg.PixelBoundryBackground
  36. import wordcloud.bg.RectangleBackground
  37. import wordcloud.font.CloudFont
  38. import wordcloud.font.scale.LinearFontScalar
  39. import wordcloud.image.AngleGenerator
  40. import wordcloud.palette.ColorPalette
  41. import wordcloud.wsc.CenterWordStart
  42. import wordcloud.wsc.RandomWordStart
  43.  
  44. /**
  45.  * @author @wolfposd
  46.  */
  47.  
  48. @Field database = new Database("mydatabase.sqlite")
  49. @Field t3 = database.selectTagsAsWordFrequency("1500");
  50.  
  51. measure2()
  52.  
  53.  
  54. def measure2()
  55. {
  56.     def time1  = measure("ParallelLayeredWordCloud 4Tile") { func2(new ParallelLayeredWordCloud(4, 2000, 2000, CollisionMode.PIXEL_PERFECT), "test4tile-parallel.png")}
  57.     def time2  = measure("NormalLayeredWordCloud 4Tile") { func2(new LayeredWordCloud(4, 2000, 2000, CollisionMode.PIXEL_PERFECT), "test4tile-normal.png")}
  58.  
  59.     println time1
  60.     println time2
  61. }
  62.  
  63.  
  64. def func2(LayeredWordCloud lwc, filename)
  65. {
  66.     final FONT = new Font("Lucida Sans", Font.PLAIN, 10)
  67.  
  68.     final MAXLAYERS = lwc.getLayers()
  69.  
  70.     def positions = [[0, 0], [0, 1000], [1000, 0], [1000, 1000]]
  71.     def colors = [Color.RED, Color.WHITE, Color.BLUE, Color.GREEN]
  72.  
  73.     for(int i = 0; i < MAXLAYERS; i++)
  74.     {
  75.         def cloud = lwc[i]
  76.         cloud.angleGenerator = new AngleGenerator(0)
  77.         cloud.padding = 3
  78.         cloud.startscheme = new CenterWordStart()
  79.         cloud.cloudFont = new CloudFont(FONT)
  80.         cloud.colorPalette = new ColorPalette(colors[i])
  81.  
  82.         def pos = positions[i]
  83.         cloud.background =  new RectangleBackground(pos[0], pos[1], 1000, 1000)
  84.         cloud.fontScalar = new LinearFontScalar(10,40)
  85.     }
  86.  
  87.  
  88.     for(int i = 0; i < MAXLAYERS; i++)
  89.     {
  90.         lwc.build(i,t3)
  91.     }
  92.  
  93.     lwc.writeToFile(filename)
  94.  
  95.  
  96.     if(lwc instanceof ParallelLayeredWordCloud)
  97.     {
  98.         lwc.shutdown()
  99.     }
  100. }
  101.  
  102.  
  103.  
  104.  
  105. def measure(def description, def func)
  106. {
  107.     def start = System.currentTimeMillis()
  108.  
  109.     func()
  110.  
  111.     def end = System.currentTimeMillis()
  112.  
  113.     def time = round((end-start)/1000, 4)
  114.  
  115.     return "$description took $time secs to compute"
  116. }
  117.  
  118.  
  119.  
  120. def round(double value, int places)
  121. {
  122.     BigDecimal bd = new BigDecimal(value);
  123.     bd = bd.setScale(places, RoundingMode.HALF_UP);
  124.     return bd.doubleValue();
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement