英語で Wassr を楽しむ

つもりだったけれど、どうも英訳の精度があまりよくないので、そこそこな感じ。

日本語でヒトコトを入力して、英語に翻訳して、発言する。というもの。
twitter は、英語圏が多いので、そこでしゃべるのなら、面白みがでてくるかもしれない、と思って作ってみた。

環境は、ruby + Windows のみ。win32api を使って、inputbox を呼び出しているので。
英訳には、こちらを利用させてもらっています。
Yahoo! Pipesで作った翻訳APIをちょっと高速化(管理人日記) - むぅもぉ.jp

username と passwd は、自分の wassr のアカウントを設定してください。

以下、メインソース

$KCODE = 's'

require 'win32ole' 

script_ctrl = WIN32OLE.new('ScriptControl')
script_ctrl.language = 'VBScript'
input = script_ctrl.eval(%q{InputBox("ヒトコト", "Input")})

$KCODE = 'u'

require 'kconv'
require 'wassr_post'
require 'translation'

input = Translation.ja2en(input.toutf8)

username = 'wassr ゆーざめい'
password = 'wassr ぱすわーど'

wassr = Wassr.new(username, password)

wassr.update(input.toutf8)

翻訳

require 'net/http'
require 'uri'
require 'rexml/document'

$KCONV = 'u'

class Translation
  URI_JA2EN = 'http://pipes.yahoo.com/poolmmjp/je_translation_api?_render=rss&text='
  URI_EN2JA = 'http://pipes.yahoo.com/poolmmjp/ej_translation_api?_render=rss&text='

  def self.ja2en(message)
    http_get(URI(URI_JA2EN + URI.encode(message)))
  end

  def self.en2ja(message)
    http_get(URI(URI_EN2JA + URI.encode(message)))
  end

  class << self
    private
    def http_get(uri)
      Net::HTTP.start(uri.host, uri.port) do |http|
        response = http.get(uri.request_uri)
#       puts response.body

        doc = REXML::Document.new(response.body)

#       File.open('trans_response.xml', 'w') do |f|
#         f.print response.body
#       end

        doc.elements['/rss/channel/item/description'].text
      end
    end
  end
end

if $0 == __FILE__
  puts Translation.ja2en('あいうえお')
end

wassr の post

require 'net/http'
require 'uri'
require 'kconv'

$KCODE = 'u'

Net::HTTP.version_1_2
	
class Wassr
  AGENT_ID = 'wassr-agent/0.0.1 (Ruby)'

  def initialize(username, password)
    @username = username
    @password = password
  end

  def update(message)
    post_data = "source=#{URI.encode(AGENT_ID)}&status=#{URI.encode(message)}"

    Net::HTTP.start('api.wassr.jp') do |http|
    #Net::HTTP.start('localhost', 8080) do |http|
      response = http.post('/statuses/update.json', post_data, 'Authorization' => 'Basic ' + ["#{@username}:#{@password}"].pack('m').strip)

#     puts response.body
#     puts response.code

#     File.open('response.xml', 'w') do |f|
#       f.print response.body
#     end
    end
  end
end