7 月4th

Ruby mecab+RSS Parser

t-matsuda Ruby Read on

前回に引き続き、Rubyやってます。

次は、mecabとRSS Parser使って、RSSを読み込んでその内容から名詞(固有名詞と一般のみ)を取得するスクリプトを組んでみました。


#!/usr/bin/ruby

require 'rss'
require 'open-uri'
require 'MeCab'
require 'uri'

url = 'http://kishi-r.com/index.xml'

rss_source = ''

open(url) do |f|
rss_source = f.read
end

rss = RSS::Parser.parse(rss_source)
puts rss.channel.title
puts rss.channel.link
puts rss.channel.description

begin
c = MeCab::Tagger.new("-Ochasen")

rss.items.each do |item|
n = c.parseToNode(item.description.gsub(/< .+?>/,""))

list = Array.new
while n do
f = n.feature.split(/,/)
if /名詞/ =~ f[0] && (/固有名詞/ =~ f[1] || /一般/ =~ f[1])
#print n.surface,  "t", n.feature, "t", n.cost, "n"
list.push(n.surface)
end
n = n.next
end
puts item.title
puts item.link
puts item.date
p list.uniq
exit
end
rescue
print "RuntimeError: ", $!, "n";
end

※テスト用としてkishi-r氏のRSS使わせてもらいました。(勝手にw)あざーす。
※あえて一個目のエントリーでexit使って停めてます

結果

kishi-r.com
http://kishi-r.com/
レコードと本とコンピューターとスケボーたち
「MacBook」が到着!!
http://kishi-r.com/2007/07/macbook.html
Tue, 03 Jul 2007 22:31:07 +0900
["Apple", "Store", "MacBook", "箱", "中", "発泡スチロール", "ロゴ", "ぉ", "気", "本体", "インストール", "CD", "中身", "感じ", "シール", "オプション", "テーブル", "横", "GB", "グレード", "月", "サイト", "日本", "業者", "荷物", "状況", "イロイロ"]

まぁとりあえずオッケーなんだけど
もう少し取得する名詞の種類を検討したほうがいい気がするなぁ。。。
「ぉ」とか取得してるしw
うーん。。。


About this entry