Module: HatenaKeywordHaiku

Defined in:
lib/plugins/hatena_keyword_haiku.rb

Defined Under Namespace

Classes: Word

Constant Summary collapse

@@words =
nil

Class Method Summary collapse

Class Method Details

.generate(*args) ⇒ Object


28
29
30
31
32
33
34
# File 'lib/plugins/hatena_keyword_haiku.rb', line 28

def self.generate(*args)
  args  = [5,7,5] if args.empty?

  args.map{ |len|
    words[len.to_i].choice rescue raise "No word which length is #{len}"
  }.map{ |w| w.word }.join(' ')
end

.setup(csv_path = '/tmp/keywordlist_furigana.csv', csv_url = 'http://d.hatena.ne.jp/images/keyword/keywordlist_furigana.csv') ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/plugins/hatena_keyword_haiku.rb', line 37

def self.setup(csv_path = '/tmp/keywordlist_furigana.csv', csv_url = 'http://d.hatena.ne.jp/images/keyword/keywordlist_furigana.csv')
  return if @@words
  @@words = { }
  csv_path = File.expand_path(csv_path)
  unless File.exists? csv_path
    puts "haiku: downloading CSV"
    open(csv_path, 'w'){ |f|
      f.write(open(csv_url).read)
    }
  end


  puts "haiku: parsing CSV"
  open(csv_path).each_line{ |line|
    yomi, word = *NKF.nkf('-w', line.chomp).split(/\t/)
    next unless yomi and word
    w = Word.new(word, yomi)
    @@words[w.length] = [] unless @@words.has_key? w.length
    @@words[w.length].push w
  }
  puts "haiku: setup done"
  @@words
end

.wordsObject


61
62
63
64
# File 'lib/plugins/hatena_keyword_haiku.rb', line 61

def self.words
  setup unless @@words
  @@words
end