natsukantou 0.1.3 → 0.2.1

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: d29dec07a03407ea767c53dce9760627262abf2fb35d3f9888659be3c2442efa
4
+ data.tar.gz: 36b30c5b5fff59a62293acdf660fd7f508c0b8bfc20c1882c3f7139b7ba4a06c
5
5
  SHA512:
6
- metadata.gz: 34b63359b8441b651ffc071c841eae8cb74a29b2c7ea0fb2a717794a16de5dcc2fa8b94c5abf9e59f600cc49c7c9716a668e456790e01bf709d70a39737ef435
7
- data.tar.gz: 951610545756fbe6b1b8384ba358b64b32f471ec6e5574f270b88f166ec70ef575270ac90b4ec2624f904d3d8bfb6ffe7b977a3aa4930ac82fc6ed04f1d90d70
6
+ metadata.gz: 07cb8f6e26187a971f0ff080767b7662a62ce7f0eb7730c647586ea351925ba94190156400ac37d3012d8bdaa7f848bd7456dc58b0216cb5e5ccb386ba9d4be5
7
+ data.tar.gz: da43175d1d13f63a0c2255cdc110257e35e54d8164ec65fbbdab1c81c1eff01b146db8e5239cef8312bfea87802e6be9258dcf42401991a72e379f35d5ba4288
data/CHANGELOG.md CHANGED
@@ -17,3 +17,11 @@
17
17
  ## [0.1.3] - 2023-02-27
18
18
 
19
19
  - Skip interlacing if nodes' text() are identical
20
+
21
+ ## [0.2.0] - 2023-03-05
22
+
23
+ - Add ChatGPT
24
+
25
+ ## [0.2.1] - 2023-04-30
26
+
27
+ - Remove newline characters in HandleRubyMarkup, which helps improving translation
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
@@ -26,7 +26,7 @@ module Natsukantou
26
26
 
27
27
  def process_node(node)
28
28
  node.css('rt').each(&:remove)
29
- node.inner_text = node.text.gsub(' ', '')
29
+ node.inner_text = node.text.gsub(/[[:space:]]/, '')
30
30
  end
31
31
  end
32
32
  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.1"
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
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ["mark@goodlife.tw"]
10
10
 
11
11
  spec.summary = "human language translation library for XML documents"
12
- spec.description = "human language translation library for XML documents, supporting DeepL"
12
+ spec.description = "human language translation library for XML documents, supporting DeepL and ChatGPT"
13
13
  spec.homepage = "https://gitlab.com/lulalala/natsukantou"
14
14
  spec.license = "MIT"
15
15
  spec.required_ruby_version = ">= 2.7.0"
@@ -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.1
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-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: middleware
@@ -108,7 +108,22 @@ 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
126
+ and ChatGPT
112
127
  email:
113
128
  - mark@goodlife.tw
114
129
  executables:
@@ -128,6 +143,7 @@ files:
128
143
  - lib/monkey_patch/oga_document.rb
129
144
  - lib/monkey_patch/runner.rb
130
145
  - lib/natsukantou.rb
146
+ - lib/natsukantou/chat_gpt.rb
131
147
  - lib/natsukantou/deep_l.rb
132
148
  - lib/natsukantou/handle_ruby_markup.rb
133
149
  - lib/natsukantou/minhon.rb
@@ -167,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
183
  - !ruby/object:Gem::Version
168
184
  version: '0'
169
185
  requirements: []
170
- rubygems_version: 3.4.6
186
+ rubygems_version: 3.4.12
171
187
  signing_key:
172
188
  specification_version: 4
173
189
  summary: human language translation library for XML documents