Zipf's law 再び

require "nkf"
require "open-uri"
require "logger"

profile = {}
logger = Logger.new(STDERR)

for i in 0..9
  uri = "http://www.nicovideo.jp/ranking/res/#{100*i+1}"
  html = NKF.nkf("-e", open(uri).read)
  logger.info("#{uri}: opening")
  item = html.scan(%r_コメント:<strong[^>]*>([\d,]+)</strong>.*?<p class="TXT12"><a href="watch/(.*?)">_me)
  item.each{|tmp,id|
    res = tmp.gsub(",","").to_i
    profile[id] = res
  }
  logger.info("#{uri}: fetched.  #{profile.size} items found.")
  sleep(1)
end

profile.values.sort.reverse.each_with_index{|v,i|
  puts "#{i+1} #{v}"
}

gnuplot の fit だとうまく探れなかったので手動で探索した。

set term gif medium size 500,400
set yrange [:2e6]
set out "nicovideo.gif"
set log
plot "nicovideo.txt", 1.6e6 * x**-0.9

もともと前回はでっぱりがあったのが気になってたが、これってよくあることらしい。Wikipedia の Zipf's law の項 にも似たような図が載っていてそのキャプションには次のように書いてある。じゃあ下半分はなんなんだろう。

A plot of word frequency in Wikipedia (November 27, 2006). The plot is in log-log coordinates. x is rank of a word in the frequency table; y is the total number of the word’s occurences. Most popular words are “the”, “of” and “and”, as expected. Zipf's law corresponds to the upper linear portion of the curve, roughly following the green (1/x) line.

http://en.wikipedia.org/wiki/Zipf's_law

Wikipedia:Does Wikipedia traffic obey Zipf's law? という項目もあって、気になるけれど鯖につながりにくいし眠いので寝る。