class Obj
class Foo
end
class Bar
end
end
obj = Obj::Foo
case obj
when Obj::Foo
puts "obj is Foo."
when Obj::Bar
puts "obj is Bar"
else
puts "obj is unknown...."
end
% sudo port install mecab
% sudo port install mecab-ipadic-utf8
% sudo port install rb-mecab
マイグレーションファイルを書く
class CreateStatuses < ActiveRecord::Migration
def self.up
create_table :statuses do |t|
t.column :text, :string
t.column :screen_name, :string
t.timestamps
end
end
def self.down
drop_table :statuses
end
end
コードを書く
require "rexml/document"
require "open-uri"
require 'MeCab'
module MeCab
class Node
def category
return self.feature.split(/,/)[0]
end
def each(&b)
b[self]
self.next.each(&b) if self.next
end
end
end
class Status < ActiveRecord::Base
def self.get_xml_page(user, page)
xml = open("http://twitter.com/statuses/user_timeline/#{user}.xml?page=#{page}").read # XMLを取ってくる
doc = REXML::Document.new xml
doc.elements.each('/statuses/status') do |s|
text = s.elements["text"].text # 発言内容を取得
screen_name = s.elements['user/screen_name'].text
p "#{screen_name} #{text}"
status = Status.new(:text => text, :screen_name => screen_name)
if status.save # DBに挿入
p "SUCCESS"
else
p "FAILED"
end
end
end
def self.parse
m = MeCab::Tagger.new
h={}
Status.find(:all).each do |s|
nodes = m.parseToNode(s.text)
nodes.each do |node|
next unless node.category == '名詞' # 名詞のみ解析対象とする
if h[node.surface]
h[node.surface] += 1 # 出現回数をカウント
else
h[node.surface] = 1
end
end
end
# 結果出力
h.to_a.sort{ |a,b|
(b[1] <=> a[1]) * 2 + (a[0] <=> b[0])
}.each{ |e| puts "#{e[0]} #{e[1]}"}
end
def self.get_xml
Status.delete_all
1.upto(10) do |page|
get_xml_page('akio0911', page)
end
parse
end
end
$ port search sed
...
gsed textproc/gsed 4.1.5 GNU version of the stream editor, sed
ssed textproc/ssed 3.62 Super-sed - a heavily enhanced version of sed