jekyll-chatgpt-translate 0.0.36 → 0.0.38

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 297a8b46d6e777cfc060956768b7807acf69b4fa1f52c65f4fbed3a0084c6268
4
- data.tar.gz: a81769edc12e05ad6aecb14b31bdec6ee2dcdc797ee27e6e3d8b03f8dc4c975a
3
+ metadata.gz: 80751d33457d149991f93ebca9daf89429ba1709b7345e58d575968b789518a1
4
+ data.tar.gz: b85c071213cf44098791d31d86fdf92cc386664bba2f15d16c04e8aed5edcb11
5
5
  SHA512:
6
- metadata.gz: ebb2e3a583e2d2e0282b6d89ad09479aebd33c62b5fa20ae681d89a2b864dfb9f4af6e2d293e8c3817225331e1a47a6196014b9ee4f2b9080f04b3146a1fe738
7
- data.tar.gz: dcf28aec1603c0b2cece95fe06085e3fe028e288bf1ca60a3388405395906df5a5a123dc0b23e8146bc3689ec82d0b96baece8744f8c003b0f89b87a43ed5e3b
6
+ metadata.gz: fb1a09e8c6521002e20f664bdaf4ac343dbbb1c838e88bd596271c9f25ec06ac462d40b454a72233dcb8f3be344483ae94cc4621dd0ea0a2e4feb64976c14245
7
+ data.tar.gz: 79b0d7871805fc4ce024135b9d8e52929dd06db1e39e325671fbc61be931084f7e72564a6a2d618b4fbe139b3cc6f5c0e4568d35a98bfe784a6436ece0f881b3
data/README.md CHANGED
@@ -79,6 +79,9 @@ Full list of options available to specify in `_config.yml`:
79
79
  doesn't translate short paragraphs pretty enough. It's better to keep this
80
80
  number big enough, to avoid silly translations. The default is 128.
81
81
 
82
+ * `window_length` (optional) — maximum number of words to be sent to OpenAI API in one
83
+ request. The default is 2048.
84
+
82
85
  * `layout` (optional) — is name of the file in `_layouts` directory, without the extension.
83
86
  This layout will be specified for the pages generated by this plugin.
84
87
  The default value is `translated` (expecting you to have `_layouts/translated.html` file available).
@@ -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.36'
31
+ s.version = '0.0.38'
32
32
  s.license = 'MIT'
33
33
  s.summary = 'Translate Jekyll Pages Through ChatGPT'
34
34
  s.description = [
@@ -51,7 +51,7 @@ class GptTranslate::ChatGPT
51
51
  @target = target
52
52
  end
53
53
 
54
- def translate(markdown, min: 32)
54
+ def translate(markdown, min: 32, window_length: 2000)
55
55
  pars = GptTranslate::Pars.new(markdown).to_a
56
56
  ready = []
57
57
  later = []
@@ -82,17 +82,28 @@ class GptTranslate::ChatGPT
82
82
  later[i] = par
83
83
  end
84
84
  end
85
- accum = []
86
- (0..pars.length).each do |i|
87
- if !ready[i].nil? && !accum.empty? && later[i].nil?
88
- ready[i - 1] = translate_pars(accum)
89
- accum = []
85
+ out = []
86
+ i = 0
87
+ while i < pars.length
88
+ unless ready[i].nil?
89
+ out << ready[i]
90
+ i += 1
90
91
  next
91
92
  end
92
- accum << later[i] unless later[i].nil?
93
+ accum = []
94
+ until later[i].nil?
95
+ already = accum.join.split.count
96
+ if already > window_length
97
+ Jekyll.logger.debug("Already #{already} words, over the window_length of #{window_length}")
98
+ break
99
+ end
100
+ accum << later[i]
101
+ i += 1
102
+ end
103
+ out << translate_pars(accum)
104
+ i += 1
93
105
  end
94
- ready[pars.length - 1] = translate_pars(accum) unless accum.empty?
95
- ready.join("\n\n")
106
+ out.join("\n\n")
96
107
  end
97
108
 
98
109
  private
@@ -44,6 +44,7 @@ class GptTranslate::Generator < Jekyll::Generator
44
44
 
45
45
  # Main plugin action, called by Jekyll-core
46
46
  def generate(site)
47
+ Jekyll.logger.info("jekyll-chatgpt-translate #{GptTranslate::VERSION} starting...")
47
48
  config ||= site.config['chatgpt-translate'] || {}
48
49
  home = config['tmpdir'] || '_chatgpt-translate'
49
50
  key = api_key(config)
@@ -113,7 +114,7 @@ class GptTranslate::Generator < Jekyll::Generator
113
114
  config['source'] || 'en',
114
115
  lang
115
116
  )
116
- foreign = gpt.translate(plain, min: min_chars)
117
+ foreign = gpt.translate(plain, min: min_chars, window_length: config['window_length'] || '2048')
117
118
  File.write(
118
119
  path,
119
120
  [
@@ -23,5 +23,5 @@
23
23
  # SOFTWARE.
24
24
 
25
25
  module GptTranslate
26
- VERSION = '0.0.36'
26
+ VERSION = '0.0.38'
27
27
  end
data/test/test_chatgpt.rb CHANGED
@@ -76,7 +76,8 @@ class GptTranslate::ChatGPTTest < Minitest::Test
76
76
 
77
77
  This is it.
78
78
  ",
79
- min: 40
79
+ min: 40,
80
+ window_length: 10
80
81
  )
81
82
  end
82
83
 
@@ -96,6 +97,18 @@ class GptTranslate::ChatGPTTest < Minitest::Test
96
97
  assert_equal('done!', chat.translate('This is the text to send to OpenAI'))
97
98
  end
98
99
 
100
+ def test_through_small_window
101
+ stub_it!
102
+ chat = GptTranslate::ChatGPT.new('fake-key', 'gpt-3.5-turbo', 'en', 'ru')
103
+ assert_equal(
104
+ "done!\n\ndone!",
105
+ chat.translate(
106
+ "This is the first paragraph\n\nThis is second\n\nThis is third",
107
+ min: 1, window_length: 4
108
+ )
109
+ )
110
+ end
111
+
99
112
  private
100
113
 
101
114
  def stub_it!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-chatgpt-translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.36
4
+ version: 0.0.38
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-29 00:00:00.000000000 Z
11
+ date: 2023-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: humanize