Class: TranslationAPI::Mediator

Inherits:
Object
  • Object
show all
Defined in:
lib/translation_api/mediator.rb

Instance Method Summary collapse

Constructor Details

#initialize(output_logs: true, language: "japanese", agent: :openai, except_words: []) ⇒ TranslationAPI::Mediator

Parameters:

  • output_logs (Boolean) (defaults to: true)

    ログを出力するかどうか

  • language (String) (defaults to: "japanese")

    翻訳先の言語

  • agent (Symbol) (defaults to: :openai)

    翻訳エージェント

  • except_words (Array<String>) (defaults to: [])

    除外する単語のリスト



14
15
16
17
18
19
20
21
# File 'lib/translation_api/mediator.rb', line 14

def initialize(
  output_logs: true, language: "japanese", agent: :openai, except_words: []
)
  @output_logs = output_logs
  @language = language
  @agent = agent
  @except_words = except_words
end

Instance Method Details

#agent_classClass

エージェントのクラスを返す

Returns:

  • (Class)

    エージェントのクラス



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/translation_api/mediator.rb', line 44

def agent_class
  case @agent
  when :openai
    OpenAI
  when :deepl
    DeepL
  else
    class_name = camelize(@agent.to_s)
    Object.const_get("TranslationAPI::#{class_name}")
  end
end

#camelize(str) ⇒ String

スネークケースの文字列をキャメルケースに変換する

Parameters:

  • str (String)

    スネークケースの文字列

Returns:

  • (String)

    キャメルケースの文字列



60
61
62
# File 'lib/translation_api/mediator.rb', line 60

def camelize(str)
  str.split("_").map(&:capitalize).join
end

#init_agentObject

エージェントのインスタンスを初期化する

Returns:

  • (Object)

    翻訳エージェントのインスタンス



35
36
37
38
39
# File 'lib/translation_api/mediator.rb', line 35

def init_agent
  agent_class.new(
    output_logs: @output_logs, except_words: @except_words, language: @language
  )
end

#translate(text) ⇒ String

テキストを翻訳する

Parameters:

  • text (String)

    翻訳するテキスト

Returns:

  • (String)

    翻訳されたテキスト



27
28
29
30
# File 'lib/translation_api/mediator.rb', line 27

def translate(text)
  agent = init_agent
  agent.translate(text)
end