shoeset

Hex Artifact Content
Login

Artifact 5619f0c2d62e51c71c541d7b12ee06d23bd856c96448ae686477b936ff102cb7:


0000: 23 20 54 61 6b 65 6e 20 66 72 6f 6d 20 68 74 74  # Taken from htt
0010: 70 3a 2f 2f 73 6e 69 70 70 65 74 73 2e 64 7a 6f  p://snippets.dzo
0020: 6e 65 2e 63 6f 6d 2f 70 6f 73 74 73 2f 73 68 6f  ne.com/posts/sho
0030: 77 2f 36 30 32 37 0a 23 20 54 77 65 61 6b 65 64  w/6027.# Tweaked
0040: 20 74 6f 20 72 65 74 75 72 6e 20 76 61 6c 75 65   to return value
0050: 73 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 53  s suitable for S
0060: 68 6f 65 73 20 28 6c 69 6e 65 73 20 33 38 2d 34  hoes (lines 38-4
0070: 30 2c 20 65 74 63 29 0a 63 6c 61 73 73 20 54 61  0, etc).class Ta
0080: 67 43 6c 6f 75 64 0a 20 20 20 20 0a 0a 20 20 64  gCloud.    ..  d
0090: 65 66 20 69 6e 69 74 69 61 6c 69 7a 65 28 77 6f  ef initialize(wo
00a0: 72 64 73 29 0a 20 20 20 20 40 77 6f 72 64 63 6f  rds).    @wordco
00b0: 75 6e 74 20 3d 20 63 6f 75 6e 74 5f 77 6f 72 64  unt = count_word
00c0: 73 28 77 6f 72 64 73 29 0a 20 20 65 6e 64 0a 20  s(words).  end. 
00d0: 20 0a 0a 20 20 64 65 66 20 63 6f 75 6e 74 5f 77   ..  def count_w
00e0: 6f 72 64 73 28 77 6f 72 64 73 29 0a 20 20 20 20  ords(words).    
00f0: 77 6f 72 64 63 6f 75 6e 74 20 3d 20 7b 7d 0a 20  wordcount = {}. 
0100: 20 20 20 77 6f 72 64 73 2e 73 70 6c 69 74 28 2f     words.split(/
0110: 5c 73 2f 29 2e 65 61 63 68 20 64 6f 20 7c 77 6f  \s/).each do |wo
0120: 72 64 7c 20 0a 20 20 20 20 20 20 77 6f 72 64 2e  rd| .      word.
0130: 64 6f 77 6e 63 61 73 65 21 0a 20 20 20 20 20 20  downcase!.      
0140: 69 66 20 77 6f 72 64 2e 73 74 72 69 70 2e 73 69  if word.strip.si
0150: 7a 65 20 3e 20 30 0a 20 20 20 20 20 20 20 20 75  ze > 0.        u
0160: 6e 6c 65 73 73 20 77 6f 72 64 63 6f 75 6e 74 2e  nless wordcount.
0170: 6b 65 79 3f 28 77 6f 72 64 2e 73 74 72 69 70 29  key?(word.strip)
0180: 0a 20 20 20 20 20 20 20 20 20 20 77 6f 72 64 63  .          wordc
0190: 6f 75 6e 74 5b 77 6f 72 64 2e 73 74 72 69 70 5d  ount[word.strip]
01a0: 20 3d 20 30 0a 20 20 20 20 20 20 20 20 65 6c 73   = 0.        els
01b0: 65 0a 20 20 20 20 20 20 20 20 20 20 77 6f 72 64  e.          word
01c0: 63 6f 75 6e 74 5b 77 6f 72 64 2e 73 74 72 69 70  count[word.strip
01d0: 5d 20 3d 20 77 6f 72 64 63 6f 75 6e 74 5b 77 6f  ] = wordcount[wo
01e0: 72 64 2e 73 74 72 69 70 5d 20 2b 20 31 0a 20 20  rd.strip] + 1.  
01f0: 20 20 20 20 20 20 65 6e 64 0a 20 20 20 20 20 20        end.      
0200: 65 6e 64 0a 20 20 20 20 65 6e 64 0a 20 20 20 20  end.    end.    
0210: 77 6f 72 64 63 6f 75 6e 74 0a 20 20 65 6e 64 0a  wordcount.  end.
0220: 20 20 0a 0a 20 20 64 65 66 20 66 6f 6e 74 5f 72    ..  def font_r
0230: 61 74 69 6f 28 77 6f 72 64 63 6f 75 6e 74 3d 7b  atio(wordcount={
0240: 7d 29 0a 20 20 20 20 6d 69 6e 2c 20 6d 61 78 20  }).    min, max 
0250: 3d 20 31 30 30 30 30 30 30 2c 20 2d 31 30 30 30  = 1000000, -1000
0260: 30 30 30 0a 20 20 20 20 77 6f 72 64 63 6f 75 6e  000.    wordcoun
0270: 74 2e 65 61 63 68 5f 6b 65 79 20 64 6f 20 7c 77  t.each_key do |w
0280: 6f 72 64 7c 0a 20 20 20 20 20 20 6d 61 78 20 3d  ord|.      max =
0290: 20 77 6f 72 64 63 6f 75 6e 74 5b 77 6f 72 64 5d   wordcount[word]
02a0: 20 69 66 20 77 6f 72 64 63 6f 75 6e 74 5b 77 6f   if wordcount[wo
02b0: 72 64 5d 20 3e 20 6d 61 78 0a 20 20 20 20 20 20  rd] > max.      
02c0: 6d 69 6e 20 3d 20 77 6f 72 64 63 6f 75 6e 74 5b  min = wordcount[
02d0: 77 6f 72 64 5d 20 69 66 20 77 6f 72 64 63 6f 75  word] if wordcou
02e0: 6e 74 5b 77 6f 72 64 5d 20 3c 20 6d 69 6e 0a 20  nt[word] < min. 
02f0: 20 20 20 65 6e 64 0a 20 20 20 20 31 38 2e 30 20     end.    18.0 
0300: 2f 20 28 6d 61 78 20 2d 20 6d 69 6e 29 0a 20 20  / (max - min).  
0310: 65 6e 64 0a 20 20 0a 0a 20 20 64 65 66 20 62 75  end.  ..  def bu
0320: 69 6c 64 0a 20 20 20 20 63 6c 6f 75 64 20 3d 20  ild.    cloud = 
0330: 53 74 72 69 6e 67 2e 6e 65 77 0a 20 20 20 20 72  String.new.    r
0340: 61 74 69 6f 20 3d 20 66 6f 6e 74 5f 72 61 74 69  atio = font_rati
0350: 6f 28 40 77 6f 72 64 63 6f 75 6e 74 29 0a 09 72  o(@wordcount)..r
0360: 61 74 69 6f 20 3d 20 31 20 69 66 20 72 61 74 69  atio = 1 if rati
0370: 6f 2e 69 6e 66 69 6e 69 74 65 3f 0a 09 64 65 62  o.infinite?..deb
0380: 75 67 28 72 61 74 69 6f 29 20 23 4e 65 65 64 20  ug(ratio) #Need 
0390: 74 6f 20 66 69 78 20 66 6f 72 20 69 6e 66 69 6e  to fix for infin
03a0: 69 74 79 0a 09 63 6f 6c 6f 72 20 3d 20 5b 22 73  ity..color = ["s
03b0: 74 65 65 6c 62 6c 75 65 22 2c 20 22 64 65 65 70  teelblue", "deep
03c0: 70 69 6e 6b 22 5d 0a 20 20 20 20 40 77 6f 72 64  pink"].    @word
03d0: 63 6f 75 6e 74 2e 65 61 63 68 5f 6b 65 79 20 64  count.each_key d
03e0: 6f 20 7c 77 6f 72 64 7c 0a 20 20 20 20 20 20 66  o |word|.      f
03f0: 6f 6e 74 5f 73 69 7a 65 20 3d 20 28 31 30 20 2b  ont_size = (10 +
0400: 20 28 40 77 6f 72 64 63 6f 75 6e 74 5b 77 6f 72   (@wordcount[wor
0410: 64 5d 20 2a 20 72 61 74 69 6f 29 29 2e 72 6f 75  d] * ratio)).rou
0420: 6e 64 20 23 6d 75 73 74 20 72 6f 75 6e 64 20 66  nd #must round f
0430: 6f 72 20 73 68 6f 65 73 20 73 69 6e 63 65 20 70  or shoes since p
0440: 69 78 65 6c 73 0a 09 20 20 63 6c 6f 75 64 20 3c  ixels..  cloud <
0450: 3c 20 25 51 7b 70 61 72 61 20 22 23 7b 77 6f 72  < %Q{para "#{wor
0460: 64 7d 22 2c 20 3a 73 69 7a 65 20 3d 3e 20 23 7b  d}", :size => #{
0470: 66 6f 6e 74 5f 73 69 7a 65 7d 2c 20 3a 73 74 72  font_size}, :str
0480: 6f 6b 65 20 3d 3e 20 23 7b 63 6f 6c 6f 72 5b 30  oke => #{color[0
0490: 5d 7d 3b 20 7d 0a 09 20 20 63 6f 6c 6f 72 2e 72  ]}; }..  color.r
04a0: 65 76 65 72 73 65 21 20 23 61 6c 74 65 72 6e 61  everse! #alterna
04b0: 74 65 20 62 65 74 77 65 65 6e 20 63 6f 6c 6f 75  te between colou
04c0: 72 73 0a 20 20 20 20 65 6e 64 0a 20 20 20 20 63  rs.    end.    c
04d0: 6c 6f 75 64 0a 09 23 64 65 62 75 67 20 63 6c 6f  loud..#debug clo
04e0: 75 64 0a 20 20 65 6e 64 0a 0a 0a 65 6e 64 0a 0a  ud.  end...end..
04f0: 0a                                               .