Skip to content

Instantly share code, notes, and snippets.

@knsmr
Created January 2, 2011 22:53
Show Gist options
  • Select an option

  • Save knsmr/762901 to your computer and use it in GitHub Desktop.

Select an option

Save knsmr/762901 to your computer and use it in GitHub Desktop.
Analyze Tweets and count the numbers of each letters used
# -*- coding: utf-8 -*-
require 'twitter'
class MyTweets
def initialize(name)
@name = name
@c = Twitter::Client.new
@tweets = []
end
def retrieve(pages)
pages.each do |i|
begin
$stderr.print "Retrieving page #{i}...\n"
@tweets << @c.user_timeline(@name,
{:count => 200, :page => i}).map(&:text)
rescue
$stderr.print "Oops, the 5xx whale at page #{i}. Let's wait a bit...\n"
sleep 5
$stderr.print "Retrying.\n"
retry
end
end
@tweets.flatten
end
end
class Stats
def initialize(tweets)
@tweets = tweets
@freq = Hash.new(0) # 頻度情報のハッシュ
@total = 0 # ヒットした文字の総数
@max = 0 # 最頻出の文字の頻度値
end
def count(regx)
@tweets.each do |t|
t.gsub(regx) do |hit|
hit = $&
@freq[hit] += 1
@total += 1
end
end
@max = @freq.values.max
end
def show
rank = cumulative = 0
keys = @freq.keys.sort{|k1,k2| @freq[k1] <=> @freq[k2]}.reverse
keys.each do |char|
ratio = @freq[char]/@total.to_f * 100
cumulative += ratio
rank += 1
stars = "*" * (@freq[char]/@max.to_f * 40).to_i
printf("%s : %40s [%5d個:%5.1f%:%3d%(%2d位)]\n",
char, stars, @freq[char], ratio, cumulative, rank)
end
printf("合計:%d個\n",@total)
end
end
if __FILE__ == $0
t = MyTweets.new(ARGV.shift)
tweets = t.retrieve((1..16)) # Twitter APIの上限は過去3200ツイート
# tweets = File.read("my_tweets.txt").split("\n")
stat = Stats.new(tweets)
stat.count(/[一-龠]/) # /[一-龠]/ でutf8の漢字
stat.show
end
=begin
% ruby wctweet.rb knsmr
Retrieving page 1.
Retrieving page 2.
Retrieving page 3.
Retrieving page 4.
Retrieving page 5.
Retrieving page 6.
Retrieving page 7.
Retrieving page 8.
Retrieving page 9.
Retrieving page 10.
Retrieving page 11.
Retrieving page 12.
Oops, the 5xx whale at page 12. Let's wait a bit...
Retrying.
Retrieving page 12.
Retrieving page 13.
Oops, the 5xx whale at page 13. Let's wait a bit...
Retrying.
Retrieving page 13.
Retrieving page 14.
Oops, the 5xx whale at page 14. Let's wait a bit...
Retrying.
Retrieving page 14.
Retrieving page 15.
Retrieving page 16.
人 : **************************************** [ 612個: 1.6%: 1%( 1位)]
思 : ********************************** [ 529個: 1.3%: 2%( 2位)]
本 : **************************** [ 442個: 1.1%: 4%( 3位)]
分 : *************************** [ 428個: 1.1%: 5%( 4位)]
日 : *************************** [ 418個: 1.1%: 6%( 5位)]
的 : ************************ [ 377個: 1.0%: 7%( 6位)]
語 : ************************ [ 368個: 0.9%: 8%( 7位)]
見 : *********************** [ 362個: 0.9%: 9%( 8位)]
書 : *********************** [ 355個: 0.9%: 9%( 9位)]
気 : ********************** [ 340個: 0.9%: 10%(10位)]
言 : ********************** [ 340個: 0.9%: 11%(11位)]
感 : ********************* [ 329個: 0.8%: 12%(12位)]
話 : ******************** [ 321個: 0.8%: 13%(13位)]
使 : ******************* [ 302個: 0.8%: 14%(14位)]
出 : ***************** [ 270個: 0.7%: 14%(15位)]
大 : **************** [ 253個: 0.6%: 15%(16位)]
動 : **************** [ 245個: 0.6%: 16%(17位)]
面 : *************** [ 236個: 0.6%: 16%(18位)]
:
:
専 : ** [ 34個: 0.1%: 72%(293位)]
達 : ** [ 34個: 0.1%: 73%(294位)]
位 : ** [ 34個: 0.1%: 73%(295位)]
員 : ** [ 34個: 0.1%: 73%(296位)]
登 : ** [ 34個: 0.1%: 73%(297位)]
主 : ** [ 33個: 0.1%: 73%(298位)]
移 : ** [ 33個: 0.1%: 73%(299位)]
親 : ** [ 33個: 0.1%: 73%(300位)]
覚 : ** [ 33個: 0.1%: 73%(301位)]
置 : ** [ 33個: 0.1%: 73%(302位)]
残 : ** [ 33個: 0.1%: 73%(303位)]
深 : ** [ 33個: 0.1%: 73%(304位)]
投 : ** [ 33個: 0.1%: 73%(305位)]
形 : ** [ 33個: 0.1%: 74%(306位)]
:
:
侮 : [ 1個: 0.0%: 99%(1740位)]
哀 : [ 1個: 0.0%: 99%(1741位)]
些 : [ 1個: 0.0%: 99%(1742位)]
郷 : [ 1個: 0.0%: 99%(1743位)]
奥 : [ 1個: 0.0%: 99%(1744位)]
咀 : [ 1個: 0.0%: 99%(1745位)]
嚼 : [ 1個: 0.0%: 99%(1746位)]
剰 : [ 1個: 0.0%: 99%(1747位)]
冴 : [ 1個: 0.0%: 99%(1748位)]
脚 : [ 1個: 0.0%: 99%(1749位)]
拓 : [ 1個: 0.0%: 99%(1750位)]
合計:39263個
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment