natsukantou 0.1.2 → 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: c57ca088a6f550986f0d3d4244a5ec85ba4271b8034b76d9c389ebbce5a6219d
4
- data.tar.gz: 6ef86b82bf6ba37ef22e0235aa4219d3c0e91f6578ea783a53b808ac5753e9ef
3
+ metadata.gz: 1edac0c042ee1ad4c1051d15028d5c6ff9ac397160c5bc83c9cd8c7fe1730d3f
4
+ data.tar.gz: 9e8950028f71f90f4e1b141a63d74027c021b36a2231151e290b50fcd396ec34
5
5
  SHA512:
6
- metadata.gz: b405772e21b46d959122c0e9e4c1f24eb55bc75b34a3b5b64e21c13f645766a47cfcf8d26a5bb5e307d458c2aca11d34a3f75228464dda7d82e020c277eac31a
7
- data.tar.gz: 59cc9abd5cacf466de075265b9a30d867cf8cec019d9b272382975ab6dd08f9d5fc4c7e9be37e4cfae68583af90b87a91ae398c1378ca5c15077b7c3cc05892a
6
+ metadata.gz: 82d10f0790a4eb1899ec277b90b9fa3c66de282d08915fd5e9d2fe6e53d97a39b12205a9ec8389c6bdbf5587f492fb072ca70d02326bd991be7f94217f144b8e
7
+ data.tar.gz: 7c81870166e14a41957246cd266a8b0e58e6868421ed7115deb853746e036d3f913fed5880412d73e9b530f3dcab8304a430ca5475c1decbf7ad7f0876c458bb
data/CHANGELOG.md CHANGED
@@ -3,3 +3,17 @@
3
3
  ## [0.1.0] - 2022-07-05
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.1.1] - 2022-09-04
8
+
9
+ - Fix compatibility to Ruby 3
10
+
11
+ ## [0.1.2] - 2022-10-01
12
+
13
+ - Document interlacing
14
+ - Set lang attribute after translation
15
+ - Fix Oga error
16
+
17
+ ## [0.1.3] - 2023-02-27
18
+
19
+ - Skip interlacing if nodes' text() are identical
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
 
@@ -46,6 +46,8 @@ module Oga
46
46
  node.lang = root_lang
47
47
  node_other.lang = root_lang_other
48
48
 
49
+ next if node.text() == nodes_other[i].text()
50
+
49
51
  node.after(node_other)
50
52
  end
51
53
  end
@@ -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.2"
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.2
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: 2022-10-01 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.1.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