natsukantou 0.1.3 → 0.2.0

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: 1b299497adbec2c0446350fc1085c4f17c6a9ebe165246dbba07fef1914348e5
4
- data.tar.gz: 8164f3a25f80bc7fde73fd8062563ecbdf34f5abc2f5b1b594e78a219bafe2b8
3
+ metadata.gz: 1edac0c042ee1ad4c1051d15028d5c6ff9ac397160c5bc83c9cd8c7fe1730d3f
4
+ data.tar.gz: 9e8950028f71f90f4e1b141a63d74027c021b36a2231151e290b50fcd396ec34
5
5
  SHA512:
6
- metadata.gz: 34b63359b8441b651ffc071c841eae8cb74a29b2c7ea0fb2a717794a16de5dcc2fa8b94c5abf9e59f600cc49c7c9716a668e456790e01bf709d70a39737ef435
7
- data.tar.gz: 951610545756fbe6b1b8384ba358b64b32f471ec6e5574f270b88f166ec70ef575270ac90b4ec2624f904d3d8bfb6ffe7b977a3aa4930ac82fc6ed04f1d90d70
6
+ metadata.gz: 82d10f0790a4eb1899ec277b90b9fa3c66de282d08915fd5e9d2fe6e53d97a39b12205a9ec8389c6bdbf5587f492fb072ca70d02326bd991be7f94217f144b8e
7
+ data.tar.gz: 7c81870166e14a41957246cd266a8b0e58e6868421ed7115deb853746e036d3f913fed5880412d73e9b530f3dcab8304a430ca5475c1decbf7ad7f0876c458bb
data/README.md CHANGED
@@ -36,6 +36,7 @@ If you choose to save the config (`translator_config.rb`), next time you can reu
36
36
 
37
37
  * [DeepL API](https://www.deepl.com/pro-api)
38
38
  * [みんなの自動翻訳@TexTra](https://mt-auto-minhon-mlt.ucri.jgn-x.jp/)
39
+ * [ChatGPT](https://openai.com/)
39
40
 
40
41
  ### Middlewares
41
42
 
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openai'
4
+
5
+ # Machine translator
6
+ #
7
+ # ChatGPT
8
+ module Natsukantou
9
+ class ChatGpt
10
+ include UtilityBase
11
+
12
+ # @param app [Hash]
13
+ # @param access_token [String] API access token
14
+ #
15
+ # @param translate_by_section [String] CSS selector to translate matched elements one by one.
16
+ # Specify a css path (e.g. "body>p") to translate in smaller chunks.
17
+ def initialize(
18
+ app, access_token:,
19
+ translate_by_section:
20
+ )
21
+ @app = app
22
+ @access_token = access_token
23
+
24
+ ### Non request related setting
25
+ @translate_by_section = translate_by_section
26
+ end
27
+
28
+ attr_reader :env
29
+ attr_reader :access_token
30
+ attr_reader :translate_by_section
31
+
32
+ def call(env)
33
+ @env = env
34
+
35
+ if translate_by_section
36
+ env[:dom].css(translate_by_section).each do |node|
37
+ next if node.text.empty?
38
+
39
+ translated_xml = translate(node.to_xml)
40
+ node.replace(dom_node(translated_xml))
41
+ end
42
+ else
43
+ # TODO
44
+ end
45
+
46
+ env[:dom].lang = env[:lang_to].code
47
+
48
+ @app.call(env)
49
+ end
50
+
51
+ private
52
+
53
+ def translate(text)
54
+ message = "Please help me to translate the XML `#{text}` from #{env.lang_from.code} to #{env.lang_to.code}, maintaining XML structure." # rubocop:disable Layout/LineLength
55
+
56
+ response = client.chat(
57
+ parameters: {
58
+ model: "gpt-3.5-turbo",
59
+ messages: [{ role: "user", content: message }],
60
+ temperature: 0.7,
61
+ }
62
+ )
63
+
64
+ translated_xml = response.dig("choices", 0, "message", "content")
65
+
66
+ translated_xml.strip!
67
+
68
+ translated_xml
69
+ rescue Net::OpenTimeout
70
+ sleep 10
71
+ retry
72
+ end
73
+
74
+ def client
75
+ @client ||= ::OpenAI::Client.new(access_token: access_token)
76
+ end
77
+ end
78
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Natsukantou
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/natsukantou.rb CHANGED
@@ -33,6 +33,7 @@ module Natsukantou
33
33
  # Translators
34
34
  autoload_and_register :translator, :DeepL, "natsukantou/deep_l"
35
35
  autoload_and_register :translator, :Minhon, "natsukantou/minhon"
36
+ autoload_and_register :translator, :ChatGpt, "natsukantou/chat_gpt"
36
37
 
37
38
  # Middlewares
38
39
  autoload_and_register :middleware, :SubstitudeGlossary, "natsukantou/substitude_glossary"
data/natsukantou.gemspec CHANGED
@@ -42,6 +42,9 @@ Gem::Specification.new do |spec|
42
42
  # Minhon
43
43
  spec.add_dependency 'oauth', '~> 0.5.1'
44
44
 
45
+ # ChatGPT
46
+ spec.add_dependency 'ruby-openai', '~> 3.5.0'
47
+
45
48
  # For more information and examples about making a new gem, check out our
46
49
  # guide at: https://bundler.io/guides/creating_gem.html
47
50
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: natsukantou
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - lulalala
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-27 00:00:00.000000000 Z
11
+ date: 2023-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: middleware
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: 0.5.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: ruby-openai
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 3.5.0
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 3.5.0
111
125
  description: human language translation library for XML documents, supporting DeepL
112
126
  email:
113
127
  - mark@goodlife.tw
@@ -128,6 +142,7 @@ files:
128
142
  - lib/monkey_patch/oga_document.rb
129
143
  - lib/monkey_patch/runner.rb
130
144
  - lib/natsukantou.rb
145
+ - lib/natsukantou/chat_gpt.rb
131
146
  - lib/natsukantou/deep_l.rb
132
147
  - lib/natsukantou/handle_ruby_markup.rb
133
148
  - lib/natsukantou/minhon.rb
@@ -167,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
182
  - !ruby/object:Gem::Version
168
183
  version: '0'
169
184
  requirements: []
170
- rubygems_version: 3.4.6
185
+ rubygems_version: 3.4.7
171
186
  signing_key:
172
187
  specification_version: 4
173
188
  summary: human language translation library for XML documents