トップ «前の日記(2007-09-09) 最新 次の日記(2007-09-11)» 編集

日々の破片

著作一覧

2007-09-10

_ チェンの本

アスキーからもらったチェンの本が滅法面白かったので、後で書く。

同情もするが身から出た錆もあり。

_ おもちゃ

#!/usr/local/bin/ruby -Ks
require 'open-uri'
require 'cgi'
require 'iconv'
class GooDic
  class NulLog
    def write(s)
    end
    def close
    end
    def flush
    end
  end
  class Entry
    def initialize(title, desc)
      @title = title
      @desc = desc
    end
    attr_reader :title, :desc
  end
  def initialize
    @log = $DEBUG ? File.open('log.txt', 'wb') : NulLog.new
    @entries = {}
  end
  def add(word)
    query = CGI::escape(word)
    s = ''
    items = []
    open("http://dictionary.goo.ne.jp/search.php?&kind=all&MT=#{query}&PT=webtu&from=webtu",
         'Referer' => "http://search.goo.ne.jp/web.jsp?MT=#{query}&STYLE=web&IE=UTF-8&from=gootop") do |h|
      h.each_line do |line|
        @log.write line
        begin
          s << Iconv.conv('sjis', 'euc-jp', line.strip)
        rescue
          s << line.strip
        end
      end
    end
    @log.flush
    s.gsub('&nbsp;', ' ').scan(/<h2 class="ch04"><span>(.+?)<\/h2>.+?<div.+?>(.+?)<\/div>/) do |title, cont|
      items << Entry.new(remove_tag(title), remove_tag(cont))
    end
    @entries[word] = items
  end
  def [](key)
    @entries[key]
  end
  def size
    @entries.size
  end
  def each(&proc)
    @entries.each &proc
  end
  def remove_tag(s)
    s.gsub('</li>', "\n").gsub(/\[<a.+>\]/, '').gsub /<.+?>/, ''
  end
end
 
if __FILE__ == $0
  if ARGV.length == 0
    STDERR.puts 'usage: ruby dic.rb word [more words ...]'
    exit 1
  end
  dic = GooDic.new
  ARGV.each do |word|
    dic.add word
  end
  dic.each do |k, c|
    c.each do |e|
      puts '----------------------------------------'
      puts e.title
      puts '----------------------------------------'
      puts e.desc
    end
  end
end

_ 上のおもちゃについて

コンソールが利用する文字コ−ドはシフトJISと前提(Windows用なので)。


2003|06|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|11|12|
2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|

ジェズイットを見習え