Created
March 20, 2011 06:28
-
-
Save mallowlabs/878159 to your computer and use it in GitHub Desktop.
TDDBC でペアプロした結果です
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'open-uri' | |
class TweetCategorizer | |
def categorize(tweet) | |
time, user, body = split(tweet) | |
statuses = [] | |
statuses << "HashTag" if hashtag?(body) | |
statuses << "Reply" if reply?(body) | |
statuses << "Mention" if mention?(body) | |
statuses << "UnofficialRT" if unofficial_rt?(body) | |
statuses << "Normal" if statuses.empty? | |
"#{statuses.join(",")}¥t#{body}" | |
end | |
def categorize_from_twitter(uri) | |
response = fetch(uri) | |
response.split("¥n").map do |line| | |
categorize(line) | |
end | |
end | |
def categorize_from_twitter_with_time(uri, time, minutes = 30, max = 20) | |
fetch(uri).split("¥n").select { |line| | |
tweet_time, user, body = split(line) | |
(Time.parse(tweet_time) - Time.parse(time)).abs <= minutes * 60 | |
}.map { |line| | |
categorize(line) | |
} | |
end | |
def split(tweet) | |
tweet.split("¥t") | |
end | |
def hashtag?(body) | |
(body =‾ /#¥w+/) != nil | |
end | |
def reply?(body) | |
(body =‾ /^@¥w+/) != nil | |
end | |
def mention?(body) | |
(body =‾ /.*¥s@¥w+/) != nil | |
end | |
def unofficial_rt?(body) | |
(body =‾ /.*(RT|QT|MT)¥s@¥w+/) != nil | |
end | |
def fetch(uri, page = 0) | |
unless page == 0 | |
uri += "/" + page.to_s | |
end | |
open(uri).read | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'rspec' | |
require 'tweet_categorizer' | |
describe TweetCategorizer do | |
subject { TweetCategorizer.new } | |
describe "categorizeは" do | |
it { subject.categorize("2011/03/20 13:40:00\tbleis\tあいうえお").should == "Normal\tあいうえお" } | |
it { subject.categorize("2011/03/20 13:40:00\tmzp\tあいうえお").should == "Normal\tあいうえお" } | |
it { subject.categorize("2011/03/20 13:40:00\tmzp\tあいうえお #tddbc").should == "HashTag\tあいうえお #tddbc" } | |
it { subject.categorize("2011/03/20 13:40:00\tmzp\t@user あいうえお").should == "Reply\t@user あいうえお" } | |
it { subject.categorize("2011/03/20 13:40:00\tmzp\tテライケメン! = @bleis").should == "Mention\tテライケメン! = @bleis" } | |
it { subject.categorize("2011/03/20 13:40:00\tmzp\t@bleis テライケメン! = @bleis #tddbc").should == "HashTag,Reply,Mention\t@bleis テライケメン! = @bleis #tddbc" } | |
it { subject.categorize("2011/03/20 13:40:00\tmzp\t@bleis テライケメン! = @bleis").should == "Reply,Mention\t@bleis テライケメン! = @bleis" } | |
it { subject.categorize("2011/03/20 13:40:00\tmzp\t@bleis テライケメン! #tddbc").should == "HashTag,Reply\t@bleis テライケメン! #tddbc" } | |
it { subject.categorize("2011/03/20 13:40:00\tmzp\tテライケメン! = @bleis #tddbc").should == "HashTag,Mention\tテライケメン! = @bleis #tddbc" } | |
it { subject.categorize("2011/03/20 13:40:00\tmzp\tRT @bleis テライケメン!" ).should == "Mention,UnofficialRT\tRT @bleis テライケメン!" } | |
it { subject.categorize("2011/03/20 13:40:00\tmzp\tMT @bleis テライケメン!" ).should == "Mention,UnofficialRT\tMT @bleis テライケメン!" } | |
it { subject.categorize("2011/03/20 13:40:00\tmzp\tQT @bleis テライケメン!" ).should == "Mention,UnofficialRT\tQT @bleis テライケメン!" } | |
end | |
describe "categorize_from_twitter" do | |
before { | |
subject.stub(:fetch).and_return( <<TWEETS | |
2011/03/18 08:54:05\tsuer\t福岡まで辿りつけなかったらどうしようという不安は常につきまとう | |
2011/03/18 08:53:30\tsuer\t東海道新幹線は今のところ大丈夫そうだ | |
TWEETS | |
) | |
} | |
it { subject.categorize_from_twitter("http://192.168.1.40:4567/public_timeline").should include("Normal\t福岡まで辿りつけなかったらどうしようという不安は常につきまとう") } | |
it { | |
subject.categorize_from_twitter("http://192.168.1.40:4567/public_timeline").size.should == 2 | |
} | |
end | |
describe "categorize_from_twitter_with_time" do | |
before { | |
subject.stub(:fetch).and_return( <<TWEETS | |
2011/03/18 08:54:05\tsuer\t福岡まで辿りつけなかったらどうしようという不安は常につきまとう | |
2011/03/18 08:23:30\tsuer\t東海道新幹線は今のところ大丈夫そうだ | |
TWEETS | |
) | |
} | |
it { | |
subject.categorize_from_twitter_with_time("http://192.168.1.40:4567/public_timeline", "2011/03/18 09:00:00", 30, 20).size.should == 1 | |
} | |
it { | |
subject.categorize_from_twitter_with_time("http://192.168.1.40:4567/public_timeline", "2011/03/18 09:00:00", 30, 20).should include("Normal\t福岡まで辿りつけなかったらどうしようという不安は常につきまとう") | |
} | |
end | |
describe "fetch" do | |
it { subject.fetch("http://192.168.1.40:4567/public_timeline").should == <<TWEETS | |
2011/03/18 08:54:05\tsuer\t福岡まで辿りつけなかったらどうしようという不安は常につきまとう | |
2011/03/18 08:53:30\tsuer\t東海道新幹線は今のところ大丈夫そうだ | |
2011/03/18 08:52:21\tpocketberserker\t恋愛エニューメレーション | |
2011/03/18 08:52:10\tpocketberserker\t名古屋組早いなぁ | |
2011/03/18 08:51:52\tdaiki_kameya\t@bleis 遠出ですかー? [フッター候補募集中] | |
2011/03/18 08:50:53\tbleis\tみずぴーさん発見! | |
2011/03/18 08:50:32\tyou_and_i\tテライケメン! RT @bleis: 名駅なう | |
2011/03/18 08:49:10\tmzp\t@bleis 銀時計のへんにいます | |
2011/03/18 08:48:21\tmzp\t集合場所きめてなかったー | |
2011/03/18 08:47:51\tbleis\t名駅なう | |
2011/03/18 08:34:21\tcubeon\tぽぽぽぽーん おはよう | |
2011/03/18 08:33:01\tsue445\t現在持ってる楽天スーパーポイント983ptを全部投入する時がきたか | |
2011/03/18 08:23:13\tpocketberserker\t余分な荷物は持っていけそうにないな・・・というかさすがにディスプレイ重すぎてむりぽ | |
2011/03/18 08:15:51\tsue445\tmukuri | |
2011/03/18 07:44:35\tpocketberserker\tというわけででかけてくー | |
2011/03/18 07:34:54\tpocketberserker\tうーん、ポジペを今印刷して持っていくのはかさばりそうだ。現地で印刷だな。 | |
2011/03/18 07:33:18\tpocketberserker\tはい、7時にでかけるのに失敗 | |
2011/03/18 07:30:40\tsuer\t移動の準備 | |
2011/03/18 07:24:50\tmzp\tおきてます | |
2011/03/18 07:23:52\tpocketberserker\tフラグですね | |
TWEETS | |
} | |
it { subject.fetch("http://192.168.1.40:4567/public_timeline", 20).should == <<TWEETS | |
2011/03/16 13:34:55\takineko\t時間がなーいー | |
2011/03/16 13:34:41\takineko\tこれだけ荒れていると「不謹慎だ!」って言ってるやつが不謹慎に見えてくる。まぁ大体あってるんだけど | |
2011/03/16 13:28:12\tbleis\t@maeda_ let op_QmarkQmark ... でもできたけど呼び出せない・・・ | |
2011/03/16 13:24:53\tcubeon\t鹿島の犬王袋を犬玉袋と毎回書いてしまう俺を誰かなんとかしてほしい… | |
2011/03/16 13:23:39\tmaeda_\t@bleis 定義できても呼び出せないね | |
2011/03/16 13:22:46\tbleis\t@maeda_ ううむ・・・ | |
2011/03/16 13:21:42\tmaeda_\t@bleis let ``??`` x y = ... はOKっぽいよ | |
2011/03/16 13:20:42\tbleis\tlet (??) x y = ...ってできないのか #fsharp | |
2011/03/16 13:16:57\tukstudio\tこのbot監視してるけど、揺れと同時に通知がきたりして意味がない http://twitter.com/zishin3255 | |
2011/03/16 13:16:41\tcubeon\tポジションペーパー書くの忘れてた… | |
2011/03/16 13:11:35\tt_wada\t@cz75hiro 了解です。ありがとうございます! | |
2011/03/16 13:10:56\tpocketberserker\ttddbcの後実家に帰る→実家に持って帰る荷物をリストアップ→リリカルなのはForceとVivid3巻を荷物に→あ、ついでになのはシャープペンとタオルもっていこう→今ここ | |
2011/03/16 13:05:13\tukstudio\t実家の方は震度3か。向こうも心配だなぁ。 | |
2011/03/16 13:04:53\tcz75hiro\t@t_wada @bleis @pocketberserker @mzp 13:00に博多駅組と合流してから車で空港に向かえば、ちょうどいい時間に着くと思います。空港のターミナルで待っていただく形でいいと思いますよ。皆さんにあとで携帯番号をDしておきます | |
2011/03/16 13:04:02\tukstudio\ttwitterのTLを「緊急地震速報」で通知させてみるか。 | |
2011/03/16 13:00:13\tukstudio\t緊急地震速報bot、監視しとくかな | |
2011/03/16 12:59:28\tukstudio\t外が急にサイレンで騒がしいけど何かあったかな | |
2011/03/16 12:56:56\tukstudio\ttenki.jp おもい | |
2011/03/16 12:55:42\tirof\t「便利な言葉を使ってはいけない」ってなんの言葉でしたっけ。 | |
2011/03/16 12:52:47\tkaorun55\tやばい、でかい | |
TWEETS | |
} | |
end | |
describe "split は " do | |
before { @time, @user, @body = subject.split("2011/03/20 13:40:00\tbleis\tあいうえお") } | |
it { @time.should == "2011/03/20 13:40:00"} | |
it { @user.should == "bleis"} | |
it { @body.should == "あいうえお"} | |
end | |
describe "hashtag? は" do | |
it { subject.hashtag?("hoge #tddbc").should be_true } | |
it { subject.hashtag?("hoge").should_not be_true } | |
it { subject.hashtag?("#").should_not be_true } | |
end | |
describe "reply? は" do | |
it { subject.reply?("@user あいうえお").should == true } | |
it { subject.reply?("hoge").should == false } | |
it { subject.reply?("@").should == false } | |
end | |
describe "hashtag? は" do | |
it { subject.hashtag?("hoge #tddbc").should == true } | |
it { subject.hashtag?("hoge").should == false } | |
it { subject.hashtag?("#").should == false } | |
end | |
describe "reply? は" do | |
it { subject.reply?("@user あいうえお").should == true } | |
it { subject.reply?("hoge").should == false } | |
it { subject.reply?("@").should == false } | |
it { subject.reply?(" @user hoge").should == false } | |
end | |
describe "mention? は" do | |
it { subject.mention?("あいうえお @user あいうえお").should == true } | |
it { subject.mention?("@user あいうえお").should == false } | |
it { subject.mention?("@").should == false } | |
it { subject.mention?(" @user hoge").should == true } | |
end | |
describe "unofficial_rt? は" do | |
it { subject.unofficial_rt?("RT @bleis テライケメン!").should == true } | |
it { subject.unofficial_rt?("MT @bleis テライケメン!").should == true } | |
it { subject.unofficial_rt?("QT @bleis テライケメン!").should == true } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment