英語と日本語どっちでも入力できるようにしておくともうちょっと便利になります。webster よりも良いのは、単語の訳だけでなくて翻訳もできること。かな。
#!/usr/bin/env ruby require 'xmpp4r/Client' include Jabber class GtalkClient def initialize(jid, pass) @en2ja = "en2ja@bot.talk.google.com" @ja2en = "ja2en@bot.talk.google.com" puts "now connecting..." @client = Client::new(JID::new(jid)) @client.connect puts "connected!" @client.auth(pass) puts "authenticated!" puts "hit 'exit' or 'quit' to escape" @client.add_message_callback do |msg| puts "#{msg.body.toutf8}" if msg.type != :error end start_chat end def start_chat @quit = false while not @quit do print "> " $stdout.flush line = gets @quit = true if line.nil? if not @quit arg = line.to_s.chomp case arg when 'exit' @quit = true when 'quit' @quit = true else if arg =~ /(?!\W)[\w\s]+/ message = Jabber::Message.new(@en2ja, arg).set_type(:chat) else message = Jabber::Message.new(@ja2en, arg).set_type(:chat) end @client.send message $stdout.flush end end end puts "Goodbye!" @client.close end end if __FILE__ == $0 CONFIGFILE = "~/.gtalkrc" load CONFIGFILE gc = GtalkClient.new(MYJID, MYJIDPASSWD) end
使用例
% ruby gtalk_bot.rb [~]
now connecting...
connected!
authenticated!
hit 'exit' or 'quit' to escape
> 東京特許許可局
> Tokyo Patent Licensing Authority> Tokyo Patent Licensing Authority
> 東京特許許可局> 人間は考える葦である
> Humans are thought of reeds> Human being is a thinking reed
> 人間は考える葦
> May the Force be with you.
> 5月の力にしています。> 理力と共にあらんことを
> Tsutomu Osamu with that ARAN
最後のは「力」=ツトム、「理」=オサム、「あらん」=ARAN と人名にひっぱられている模様。