Skip to content

Instantly share code, notes, and snippets.

@jewel12
Created May 19, 2014 16:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jewel12/b33f73635bbbed2b9e97 to your computer and use it in GitHub Desktop.
Save jewel12/b33f73635bbbed2b9e97 to your computer and use it in GitHub Desktop.
自動でログ色付け君マシーン
require 'rainbow'
class WordFreq < Hash
def initialize
super(0)
end
def add(w)
self[w] += 1
end
def max
return @max || @max = super { |a, b| a[1] <=> b[1] }[1]
end
end
class Colorer
def initialize(mapper)
@mapper = mapper
end
def colorize(words)
words.map {|word|
@mapper.map(word)
}
end
end
class DefaultColorMapper
def initialize(word_freq)
@color_map = [:red,:yellow,:magenta,:green,:cyan,:blue].zip [0.9, 0.8, 0.7, 0.6, 0.3, 0]
@word_freq = word_freq
@map = map_color
end
def map_color
mapped = Hash.new
@word_freq.each do |word, freq|
mapped[word] = Rainbow(word).color(get_color(freq))
end
return mapped
end
def get_color(freq)
score = freq.to_f / @word_freq.max
@color_map.each do |c,f|
return c if score > f
end
end
def map(word)
return @map[word]
end
end
class CodingInColorMapper
def initialize(words)
@colors = %w(
#936565
#C48686
#8E574D
#BA7D67
#AD713E
#C69060
#99701F
#B59140
#9B954B
#BCB23B
#808E42
#9EB25F
#539B53
#75BC75
#42845D
#5EAA7D
#0F8776
#00A99D
#138591
#4FA4B2
#0071BC
#4DA0CE
#3062AA
#6291BF
#5462B5
#7696DD
#5E57B5
#7D7DD8
#7A5E99
#9787AD
#955AA3
#AC85B5
#A05A89
#B780A8
)
@words = words
@map = map_color
end
def map_color
mapped = Hash.new
(@words.sort.zip @colors.cycle).each do |w, c|
mapped[w] = Rainbow(w).color(c)
end
return mapped
end
def map(word)
return @map[word]
end
end
wf = WordFreq.new
content = STDIN.read
line_words = content.each_line.map {|l| l.chomp.split(' ')}
words = line_words.flatten
words.each {|w| wf.add w}
# mapper = DefaultColorMapper.new(wf)
mapper = CodingInColorMapper.new(words)
c = Colorer.new(mapper)
line_words.each do |ws|
puts c.colorize(ws).join(' ')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment