jekyll-chatgpt-translate 0.0.14 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4b8a34c60ab2378b8dc26ab4b189ff3fa168cbb864c80db92a854d68b58def59
4
- data.tar.gz: 3654fe32f201e8a155cd5f154129c24350333b6f2459cd86bb511c0acc22c0c1
3
+ metadata.gz: 0104da23aeb28e61fb3c2eb4599684accff5945175001b86a49cc821f5e49be6
4
+ data.tar.gz: 05a05b676a664ec01567f15c49c24bbc2809bd3aeb4dd11666eeea123ec19a7e
5
5
  SHA512:
6
- metadata.gz: ea02f9faba8ffedaa3e25bd428bde6aaba7a7a484bda745cc270a9f97f1a40211b776cff7d5a8c5d297854f107ba311b10dd5885bb6eff8de353f4a8aa9778d6
7
- data.tar.gz: 6f3cc93df6fff8290f3a6d45bc980a9c5b273154a98ab7c18ddff1571c55dd8c2796e9ef965c3a0df72ff175c42ed54b72f4b4ecb5d2d8bb21864ae3b4aa0c7c
6
+ metadata.gz: f9e400ffbf49216617b0a10cf6600a8786c891dbb73d950da2bdd363acf2c67e1d8e3e86c119a26be56aaed2226bd3b64d1dfb136c7b9272ac4e993ec7c83e81
7
+ data.tar.gz: 1b4d7a1da595f9bf4829b739d786268c66bac31d9b3b42d9cd9a422bcb1b626af9aa54eeb0088be7e33c2fe39aaa787e972d6c136df20769e55f39f290d6285d
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
29
29
  s.required_ruby_version = '>= 2.6'
30
30
  s.name = 'jekyll-chatgpt-translate'
31
- s.version = '0.0.14'
31
+ s.version = '0.0.15'
32
32
  s.license = 'MIT'
33
33
  s.summary = 'Translate Jekyll Pages Through ChatGPT'
34
34
  s.description = [
@@ -25,6 +25,7 @@
25
25
  require 'jekyll'
26
26
  require 'openai'
27
27
  require 'iso-639'
28
+ require_relative 'prompt'
28
29
 
29
30
  # The module we are in.
30
31
  module GptTranslate; end
@@ -66,38 +67,27 @@ class GptTranslate::ChatGPT
66
67
 
67
68
  def translate_par(par)
68
69
  start = Time.now
69
- t = nil
70
+ answer = nil
70
71
  attempt = 0
71
72
  begin
73
+ prompt = GptTranslate::Prompt.new(par, @source, @target).to_s
72
74
  response = OpenAI::Client.new(access_token: @key).chat(
73
75
  parameters: {
74
76
  model: @model,
75
- messages: [{
76
- role: 'user',
77
- content: "#{prompt}:\n\n#{par}"
78
- }],
77
+ messages: [{ role: 'user', content: prompt }],
79
78
  temperature: 0.7
80
79
  }
81
80
  )
82
- t = response.dig('choices', 0, 'message', 'content')
81
+ answer = response.dig('choices', 0, 'message', 'content')
82
+ Jekyll.logger.debug("ChatGPT prompt: \"#{prompt}\", ChatGPT answer: \"#{answer}\"")
83
83
  rescue StandardError => e
84
84
  attempt += 1
85
85
  retry if attempt < 4
86
86
  raise e
87
87
  end
88
88
  Jekyll.logger.info("Translated #{par.split.count} #{@source.upcase} words \
89
- to #{t.split.count} #{@target.upcase} words \
89
+ to #{answer.split.count} #{@target.upcase} words \
90
90
  through #{@model} in #{(Time.now - start).round(2)}s")
91
- t
92
- end
93
-
94
- def prompt
95
- [
96
- 'Please, translate this paragraph from',
97
- ISO_639.find_by_code(@source),
98
- 'to',
99
- ISO_639.find_by_code(@target),
100
- ', don\'t change proper nouns'
101
- ].join(' ')
91
+ answer
102
92
  end
103
93
  end
@@ -95,7 +95,7 @@ class GptTranslate::Generator < Jekyll::Generator
95
95
  '',
96
96
  translated,
97
97
  '',
98
- "#{marker}\n{: .jekyll-chatgpt-translate}"
98
+ "#{marker} on #{Time.now.strftime('%d/%m/%Y %H:%M')}\n{: .jekyll-chatgpt-translate}"
99
99
  ].join("\n")
100
100
  )
101
101
  site.pages << Jekyll::Page.new(site, site.source, File.dirname(path), File.basename(path))
@@ -40,8 +40,8 @@ class GptTranslate::Plain
40
40
  def to_s
41
41
  # To turn compact lists into proper lists
42
42
  @markdown.gsub(/([^\n])\n(\s*\*)/, "\\1\n\n\\2").split(/\n{2,}/).compact.map do |par|
43
- # par.gsub!("\n", ' ')
44
43
  par.gsub!("\t", ' ')
44
+ par.gsub!(/\n+/, ' ')
45
45
  par.gsub!(/ {2,}/, ' ')
46
46
  # Liquid tags are removed, but this implementation is primitive
47
47
  # Seehttps://stackoverflow.com/questions/
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ # (The MIT License)
4
+ #
5
+ # Copyright (c) 2023 Yegor Bugayenko
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the 'Software'), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ require 'iso-639'
26
+
27
+ # The module we are in.
28
+ module GptTranslate; end
29
+
30
+ # Prompt for ChatGPT.
31
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
32
+ # Copyright:: Copyright (c) 2023 Yegor Bugayenko
33
+ # License:: MIT
34
+ class GptTranslate::Prompt
35
+ # Ctor.
36
+ # +par+ Text to translate
37
+ # +source+ The language to translate from
38
+ # +target+ The language to translate into
39
+ def initialize(par, source, target)
40
+ @par = par
41
+ @source = source
42
+ @target = target
43
+ end
44
+
45
+ def to_s
46
+ from = ISO_639.find_by_code(@source)
47
+ raise "Unknown source language ISO-639 code: \"#{@source}\"" if from.nil?
48
+ to = ISO_639.find_by_code(@target)
49
+ raise "Unknown source language ISO-639 code: \"#{@target}\"" if to.nil?
50
+ head = [
51
+ 'Please, translate the following paragraph from ',
52
+ from[3],
53
+ ' to ',
54
+ to[3],
55
+ ', don\'t change proper nouns'
56
+ ].join
57
+ "#{head}:\n\n#{@par}"
58
+ end
59
+ end
@@ -23,5 +23,5 @@
23
23
  # SOFTWARE.
24
24
 
25
25
  module GptTranslate
26
- VERSION = '0.0.14'
26
+ VERSION = '0.0.15'
27
27
  end
data/test/test_plain.rb CHANGED
@@ -34,6 +34,7 @@ class GptTranslate::PlainTest < Minitest::Test
34
34
  assert_equal('Hello, world!', GptTranslate::Plain.new('Hello, **world**!').to_s)
35
35
  assert_equal('Hello, Jeff!', GptTranslate::Plain.new('Hello, _Jeff_!').to_s)
36
36
  assert_equal("Hi\n\nBye", GptTranslate::Plain.new(" Hi\n\nBye\n\n\n").to_s)
37
+ assert_equal('Hi, dude!', GptTranslate::Plain.new(" Hi,\ndude!\n").to_s)
37
38
  end
38
39
 
39
40
  def test_lists
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ # (The MIT License)
4
+ #
5
+ # Copyright (c) 2023 Yegor Bugayenko
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the 'Software'), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ require 'minitest/autorun'
26
+ require_relative '../lib/jekyll-chatgpt-translate/prompt'
27
+
28
+ # Prompt test.
29
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
30
+ # Copyright:: Copyright (c) 2023 Yegor Bugayenko
31
+ # License:: MIT
32
+ class GptTranslate::PromptTest < Minitest::Test
33
+ def par(body, source, target)
34
+ "Please, translate the following paragraph from #{source} to #{target}, don't change proper nouns:\n\n#{body}"
35
+ end
36
+
37
+ def test_english_to_russian
38
+ assert_equal(
39
+ par('Hello, dude!', 'English', 'Russian'),
40
+ GptTranslate::Prompt.new('Hello, dude!', 'en', 'ru').to_s
41
+ )
42
+ end
43
+
44
+ def test_english_to_chinese
45
+ assert_equal(
46
+ par('Hello, Jeff!', 'English', 'Chinese'),
47
+ GptTranslate::Prompt.new('Hello, Jeff!', 'en', 'zh').to_s
48
+ )
49
+ end
50
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-chatgpt-translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -110,6 +110,7 @@ files:
110
110
  - lib/jekyll-chatgpt-translate/permalink.rb
111
111
  - lib/jekyll-chatgpt-translate/ping.rb
112
112
  - lib/jekyll-chatgpt-translate/plain.rb
113
+ - lib/jekyll-chatgpt-translate/prompt.rb
113
114
  - lib/jekyll-chatgpt-translate/version.rb
114
115
  - logo.png
115
116
  - renovate.json
@@ -119,6 +120,7 @@ files:
119
120
  - test/test_permalink.rb
120
121
  - test/test_ping.rb
121
122
  - test/test_plain.rb
123
+ - test/test_prompt.rb
122
124
  homepage: https://github.com/yegor256/jekyll-chatgpt-translate
123
125
  licenses:
124
126
  - MIT