2008-03-16
第 24 回 Ruby/Rails 勉強会(Ruby初心者向けレッスン)
Ruby初心者向けレッスンが、まったくできなかった。しばらく書いていないとこのざま。
演習問題
テキスト解析−ある英文を解析してみましょう。
・文字数
File.open("rails.txt") do |content| p content.read.size end
・単語数
File.open("rails.txt") do |content| p content.read.split.size end
・行数
File.open("rails.txt") do |content| p content.readlines.size end
手抜きだが、WCと一致すればOKとのことなので、このまま放置。
・文字列の出現頻度
文字列じゃなくて文字でやってます。大文字と小文字は別に数えています。
hash = Hash.new{} File.open("rails.txt") do |content| content.read.scan(/./) do |chara| if hash.member?(chara) then hash[chara] += 1 else hash[chara] = 1 end end end hash.keys.sort.each{|chara| p "#{chara} => #{hash[chara]}"}
・単語別出現頻度
File.open("rails.txt") do |content| content.read.split.each do |word| word.downcase! word.gsub!(/\,|\.|\(|\)/, "") if hash.member?(word) then hash[word] += 1 else hash[word] = 1 end end end hash.keys.sort.each{|word| p "#{word} => #{hash[word]}"}
ログ解析−Apacheのログを解析してみましょう。
・'/index.php'へのアクセスは何回?
File.open("access_log") do |content| p content.readlines.delete_if{|line| !line.index("/index.php")}.size end
上をやってから、まず、フィールド毎に分けたほうがやりやすそうと気づき、無理やり分けた。
・最初のMacユーザのアクセスはいつ?
File.open("access_log") do |content| lines = content.readlines.map{|line| line.gsub!(/\[.*?\]|\".*?\"/){$&.gsub(/\s/, '_')}.split} lines.each do |feild| if feild[8].index("Macintosh") p feild[3]; break end end end
・Google経由のアクセスは何回?
File.open("access_log") do |content| lines = content.readlines.map{|line| line.gsub!(/\[.*?\]|\".*?\"/){$&.gsub(/\s/, '_')}.split} p lines.delete_if{|field| !field[7].index("google")}.size end
・何曜日のアクセスが一番多い?
require 'parsedate' hash = Hash.new{} File.open("access_log") do |content| lines = content.readlines.map{|line| line.gsub!(/\[.*?\]|\".*?\"/){$&.gsub(/\s/, '_')}.split} lines.each do |field| field[3].gsub(/\[(.*?):/){ ary = ParseDate::parsedate($&) w = Time.local(ary[0], ary[1], ary[2]).wday if hash.member?(w) then hash[w] += 1 else hash[w] = 1 end } end end wdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] p wdays[hash.to_a.sort{|a, b| b[1] <=> a[1]}[0][0]]
・ブラウザ毎のアクセス数ランキング
hash = Hash.new{} File.open("access_log") do |content| lines = content.readlines.map{|line| line.gsub!(/\[.*?\]|\".*?\"/){$&.gsub(/\s/, '_')}.split} lines.each do |field| if hash.member?(field[8]) then hash[field[8]] += 1 else hash[field[8]] = 1 end end end hash.to_a.sort{|a, b| b[1] <=> a[1]}.each{|browser| p browser}
ふう。
トラックバック - http://d.hatena.ne.jp/winplus/20080316/1205646973
リンク元
- 85 https://www.google.co.jp/
- 60 http://jp.rubyist.net/?KansaiWorkshop24
- 38 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja:official&hs=qPm&q=ubuntu+webdav+&btnG=検索&lr=lang_ja
- 33 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=18&ved=0CGkQFjAHOAo&url=http://d.hatena.ne.jp/winplus/20080316/1205655212&ei=qRl9T-iHGZCbiQfL94GKCQ&usg=AFQjCNF47ejtShov6vU4JBDqP6cf8sd2oQ&sig2=HZYvw1FiHbz2o0_-qrIGRw
- 33 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&sqi=2&ved=0CDMQFjAC&url=http://d.hatena.ne.jp/winplus/20080316/1205646973&ei=POtCT93FG6f4mAX4n7DHBw&usg=AFQjCNH_1YK0kWgWlIS4XSrisULYJbDUsA
- 24 http://www.google.co.jp/search?hl=ja&lr=lang_ja&client=firefox-a&rls=org.mozilla:ja:official&hs=nHM&q=rails+勉強会&revid=-1&sa=X&oi=revisions_inline&resnum=1&ct=broad-revision&cd=6
- 17 http://www.google.co.jp/search?q=rails+restful&lr=lang_ja&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:ja:official&client=firefox-a
- 17 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4GGLJ_jaJP174JP174&q=rails+file+open
- 16 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cts=1331096657146&ved=0CDgQFjAB&url=http://d.hatena.ne.jp/winplus/20080316/1205646973&ei=TuxWT4_ZOvDsmAXct4niCQ&usg=AFQjCNH_1YK0kWgWlIS4XSrisULYJbDUsA
- 15 http://www.google.co.jp/search?sourceid=chrome&ie=UTF-8&q=rails+collection+操作