Class: GptTranslate::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-chatgpt-translate/prompt.rb

Overview

Prompt for ChatGPT.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2023-2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(par, source, target) ⇒ Prompt

Ctor. par Text to translate source The language to translate from target The language to translate into



40
41
42
43
44
# File 'lib/jekyll-chatgpt-translate/prompt.rb', line 40

def initialize(par, source, target)
  @par = par
  @source = source
  @target = target
end

Instance Method Details

#to_sObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jekyll-chatgpt-translate/prompt.rb', line 46

def to_s
  from = ISO_639.find_by_code(@source)
  raise "Unknown source language ISO-639 code: #{@source.inspect}" if from.nil?
  to = ISO_639.find_by_code(@target)
  raise "Unknown source language ISO-639 code: #{@target.inspect}" if to.nil?
  md = @par
  parts = md.split("\n\n")
  label = parts.size > 1 ? "#{parts.size.humanize(locale: :en)} Markdown paragraphs" : 'Markdown paragraph'
  head = [
    "Please, translate the following #{label} from ",
    from[3],
    ' to ',
    to[3],
    ', don\'t translate technical terms and proper nouns'
  ].join
  if @par.include?('"') || @par.split.count >= 8
    "#{head}:\n\n#{@par}"
  else
    "#{head}: \"#{@par}\""
  end
end