4 # :title: Translator plugin for rbot
6 # Author:: Yaohan Chen <yaohan.chen@gmail.com>
7 # Copyright:: (C) 2007 Yaohan Chen
10 # This plugin allows using rbot to translate text on a few translation services
15 # base class for implementing a translation service
17 # direction:: supported translation directions, a hash where each key is a source
18 # language name, and each value is Set of target language names. The
19 # methods in the Direction module are convenient for initializing this
22 INFO = 'Some translation service'
24 class UnsupportedDirectionError < ArgumentError
27 class NoTranslationError < RuntimeError
30 attr_reader :directions, :cache
32 def initialize(directions, cache={})
33 @directions = directions
38 # whether the translator supports this direction
39 def support?(from, to)
40 from != to && @directions[from].include?(to)
43 # this implements argument checking and caching. subclasses should define the
44 # do_translate method to implement actual translation
45 def translate(text, from, to)
46 raise UnsupportedDirectionError unless support?(from, to)
47 raise ArgumentError, _("Cannot translate empty string") if text.empty?
48 request = [text, from, to]
49 unless @cache.has_key? request
50 translation = do_translate(text, from, to)
51 raise NoTranslationError if translation.empty?
52 @cache[request] = translation
59 # given the set of supported languages, return a hash suitable for the directions
60 # attribute which includes any language to any other language
61 def self.all_to_all(languages)
62 directions = all_to_none(languages)
63 languages.each {|l| directions[l] = languages.to_set}
67 # a hash suitable for the directions attribute which includes any language from/to
68 # the given set of languages (center_languages)
69 def self.all_from_to(languages, center_languages)
70 directions = all_to_none(languages)
71 center_languages.each {|l| directions[l] = languages - [l]}
72 (languages - center_languages).each {|l| directions[l] = center_languages.to_set}
76 # get a hash from a list of pairs
77 def self.pairs(list_of_pairs)
78 languages = list_of_pairs.flatten.to_set
79 directions = all_to_none(languages)
80 list_of_pairs.each do |(from, to)|
81 directions[from] << to
86 # an empty hash with empty sets as default values
87 def self.all_to_none(languages)
89 # always return empty set when the key is non-existent, but put empty set in the
90 # hash only if the key is one of the languages
91 if languages.include? k
102 class NiftyTranslator < Translator
103 INFO = '@nifty Translation <http://nifty.amikai.com/amitext/indexUTF8.jsp>'
105 def initialize(cache={})
107 super(Translator::Direction.all_from_to(%w[ja en zh_CN ko], %w[ja]), cache)
108 @form = WWW::Mechanize.new.
109 get('http://nifty.amikai.com/amitext/indexUTF8.jsp').
110 forms.name('translateForm').first
113 def do_translate(text, from, to)
114 @form.radiobuttons.name('langpair').value = "#{from},#{to}".upcase
115 @form.fields.name('sourceText').value = text
117 @form.submit(@form.buttons.name('translate')).
118 forms.name('translateForm').fields.name('translatedText').value
123 class ExciteTranslator < Translator
124 INFO = 'Excite.jp Translation <http://www.excite.co.jp/world/>'
126 def initialize(cache={})
130 super(Translator::Direction.all_from_to(%w[ja en zh_CN zh_TW ko], %w[ja]), cache)
132 @forms = Hash.new do |h, k|
135 h[k] = open_form('english')
136 when 'zh_CN', 'zh_TW'
137 # this way we don't need to fetch the same page twice
138 h['zh_CN'] = h['zh_TW'] = open_form('chinese')
140 h[k] = open_form('korean')
146 WWW::Mechanize.new.get("http://www.excite.co.jp/world/#{name}").
147 forms.name('world').first
150 def do_translate(text, from, to)
151 non_ja_language = from != 'ja' ? from : to
152 form = @forms[non_ja_language]
154 if non_ja_language =~ /zh_(CN|TW)/
155 form.fields.name('wb_lp').value = "#{from}#{to}".sub(/_(?:CN|TW)/, '').upcase
156 form.fields.name('big5').value = ($1 == 'TW' ? 'yes' : 'no')
158 # the en<->ja page is in Shift_JIS while other pages are UTF-8
159 text = Iconv.iconv('Shift_JIS', 'UTF-8', text) if non_ja_language == 'en'
160 form.fields.name('wb_lp').value = "#{from}#{to}".upcase
162 form.fields.name('before').value = text
163 result = form.submit.forms.name('world').fields.name('after').value
164 # the en<->ja page is in Shift_JIS while other pages are UTF-8
165 if non_ja_language == 'en'
166 Iconv.iconv('UTF-8', 'Shift_JIS', result)
175 class GoogleTranslator < Translator
176 INFO = 'Google Translate <http://www.google.com/translate_t>'
178 def initialize(cache={})
181 language_pairs = @lang_list.options.map do |o|
182 # these options have values like "en|zh-CN"; map to things like ['en', 'zh_CN'].
183 o.value.split('|').map {|l| l.sub('-', '_')}
185 super(Translator::Direction.pairs(language_pairs), cache)
189 agent = WWW::Mechanize.new
190 # without faking the user agent, Google Translate will serve non-UTF-8 text
191 agent.user_agent_alias = 'Linux Konqueror'
192 @form = agent.get('http://www.google.com/translate_t').
193 forms.action('/translate_t').first
194 @lang_list = @form.fields.name('langpair')
197 def do_translate(text, from, to)
200 @lang_list.value = "#{from}|#{to}".sub('_', '-')
201 @form.fields.name('text').value = text
202 @form.submit.parser.search('div#result_box').inner_html
207 class BabelfishTranslator < Translator
208 INFO = 'AltaVista Babel Fish Translation <http://babelfish.altavista.com/babelfish/>'
210 def initialize(cache)
213 @form = WWW::Mechanize.new.get('http://babelfish.altavista.com/babelfish/').
214 forms.name('frmTrText').first
215 @lang_list = @form.fields.name('lp')
216 language_pairs = @lang_list.options.map {|o| o.value.split('_')}.
217 reject {|p| p.empty?}
218 super(Translator::Direction.pairs(language_pairs), cache)
221 def do_translate(text, from, to)
222 if @form.fields.name('trtext').empty?
223 @form.add_field!('trtext', text)
225 @form.fields.name('trtext').value = text
227 @lang_list.value = "#{from}_#{to}"
228 @form.submit.parser.search("td.s/div[@style]").inner_html
232 class WorldlingoTranslator < Translator
233 INFO = 'WorldLingo Free Online Translator <http://www.worldlingo.com/en/products_services/worldlingo_translator.html>'
235 LANGUAGES = %w[en fr de it pt es ru nl el sv ar ja ko zh_CN zh_TW]
236 def initialize(cache)
238 super(Translator::Direction.all_to_all(LANGUAGES), cache)
241 def translate(text, from, to)
242 response = Irc::Utils.bot.httputil.get_response(URI.escape(
243 "http://www.worldlingo.com/SEfpX0LV2xIxsIIELJ,2E5nOlz5RArCY,/texttranslate?wl_srcenc=utf-8&wl_trgenc=utf-8&wl_text=#{text}&wl_srclang=#{from.upcase}&wl_trglang=#{to.upcase}"))
244 # WorldLingo seems to respond an XML when error occurs
245 case response['Content-Type']
249 raise Translator::NoTranslationError
254 class TranslatorPlugin < Plugin
255 Config.register Config::IntegerValue.new('translator.timeout',
256 :default => 30, :validate => Proc.new{|v| v > 0},
257 :desc => _("Number of seconds to wait for the translation service before timeout"))
260 'nifty' => NiftyTranslator,
261 'excite' => ExciteTranslator,
262 'google_translate' => GoogleTranslator,
263 'babelfish' => BabelfishTranslator,
264 'worldlingo' => WorldlingoTranslator,
271 TRANSLATORS.each_pair do |name, c|
273 @translators[name] = c.new(@registry.sub_registry(name))
274 map "#{name} :from :to *phrase",
275 :action => :cmd_translate, :thread => true
277 warning _("Translator %{name} cannot be used: %{reason}") %
278 {:name => name, :reason => $!}
282 Config.register Config::ArrayValue.new('translator.default_list',
283 :default => TRANSLATORS.keys,
284 :validate => Proc.new {|l| l.all? {|t| TRANSLATORS.has_key?(t)}},
285 :desc => _("List of translators to try in order when translator name not specified"),
286 :on_change => Proc.new {|bot, v| update_default})
290 def help(plugin, topic=nil)
291 if @translators.has_key?(topic)
292 translator = @translators[topic]
293 _('%{info}, supported directions of translation: %{directions}') % {
294 :info => translator.class::INFO,
295 :directions => translator.directions.map do |source, targets|
296 _('%{source} -> %{targets}') %
297 {:source => source, :targets => targets.to_a.join(', ')}
301 _('Command: <translator> <from> <to> <phrase>, where <translator> is one of: %{translators}. If "translator" is used in place of the translator name, the first translator in translator.default_list which supports the specified direction will be picked automatically. Use "help translator <translator>" to look up supported from and to languages') %
302 {:translators => @translators.keys.join(', ')}
307 @default_translators = bot.config['translator.default_list'] & @translators.keys
310 def cmd_translator(m, params)
311 from, to = params[:from], params[:to]
312 translator = @default_translators.find {|t| @translators[t].support?(from, to)}
314 cmd_translate m, params.merge({:translator => translator})
316 m.reply _('None of the default translators (translator.default_list) supports translating from %{source} to %{target}') % {:source => from, :target => to}
320 def cmd_translate(m, params)
321 # get the first word of the command
322 tname = params[:translator] || m.message[/\A(\w+)\s/, 1]
323 translator = @translators[tname]
324 from, to, phrase = params[:from], params[:to], params[:phrase].to_s
327 translation = Timeout.timeout(@bot.config['translator.timeout']) do
328 translator.translate(phrase, from, to)
331 rescue Translator::UnsupportedDirectionError
332 m.reply _("%{translator} doesn't support translating from %{source} to %{target}") %
333 {:translator => tname, :source => from, :target => to}
334 rescue Translator::NoTranslationError
335 m.reply _('%{translator} failed to provide a translation') %
336 {:translator => tname}
337 rescue Timeout::Error
338 m.reply _('The translator timed out')
341 m.reply _('No translator called %{name}') % {:name => tname}
346 plugin = TranslatorPlugin.new
347 plugin.map 'translator :from :to *phrase',
348 :action => :cmd_translator, :thread => true