papago 1.0.7 → 1.1.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: 5de9551002d06b2bba9bf63fdfaf8e0e516d002cdf95acebf238b180051d4b65
4
- data.tar.gz: e4a7fd4c044c558d85878268bc216cb3bfd5391690e99e4baabd00c7a9056535
3
+ metadata.gz: 1974f1980eecef75f2f0f095f4740294839c1eddbd6cae7a3fd59ae550116d83
4
+ data.tar.gz: 0074c89724c97de92e666ed9c81288f6b7fa9630c7bab48ffb530306d8bdd02c
5
5
  SHA512:
6
- metadata.gz: 42c4d22951d2f39f36cc5de9f485479f288e322d61f11a2640a98616b6bf5fc368fbc6f5aa08e0905fadd4bb5aaed31be0ed9d08eb62faf279a0b00fc533a35f
7
- data.tar.gz: 27e8ddb7ddfc5538fe6eb0237aefec35e7a5da5baa2f4a14fcf573912df2d92ba532d431eb19e946dadc4bc125de7499eb546ab06e331c13f0ee4b4851463ff7
6
+ metadata.gz: 15dbb550862faa7611d8586bf9bfb672fa24a5373de18d30005da07fb4d7f053bcfd5bb4ee753a2b5b8b91426548a94aba5114991f5a33cc2fec98f5ea028ed8
7
+ data.tar.gz: 272cc413c83aa0b1c4978d552274228ea88563a8e82aea6e8eda4056d75e079932cde5e05f8cb0b63b8e1702ab3b184924672c830c1260e6b0af517436a6e9f9
data/bin/papago CHANGED
@@ -1,27 +1,35 @@
1
1
  #!/usr/bin/env ruby
2
-
3
- require 'papago'
4
- require 'treely'
5
- require 'lolcat/lol'
6
- require 'active_support/core_ext/object/blank'
7
- require 'active_support/core_ext/string/filters'
8
2
  require 'mercenary'
9
-
10
- Papago.configure do |config|
11
- config.api_key = ENV['PAPAGO_API_KEY']
12
- end
3
+ require 'papago'
13
4
 
14
5
  Mercenary.program(:papago) do |program|
15
6
  program.version Papago::VERSION
16
- program.syntax 'papago netease'
7
+ program.syntax 'papago <text> [options]'
8
+
9
+ program.option 'switch', '-s', '--switch', 'Select a service'
10
+ program.option 'input', '-i', '--input PATH', String, 'Set file path'
11
+ program.option 'dest', '--dest IDENT', String, 'Set target language'
17
12
 
18
13
  program.action do |args, options|
19
- if args.present?
20
- contents = Papago.translate(args.join(%{ })).contents
21
- contents.then { |_| Treely.tree(_) }
22
- .then { |_| StringIO.new(_.to_s.remove(' []')) }
23
- .then { |_| Lol.cat(_, { freq: 0.25, spread: 8.0, os: rand(256) }) }
24
- puts %{}
14
+ if options['switch']
15
+ Papago::Actions::Switch
16
+ .process(config: Papago.config)
17
+ else
18
+ text = args.join(' ').strip
19
+ path = options['input']
20
+
21
+ if path and File.file?(path)
22
+ text = File.read(path)
23
+ .gsub(/\n/, ' ')
24
+ .strip
25
+ end
26
+
27
+ if text.empty?
28
+ puts 'No content'
29
+ else
30
+ Papago::Actions::Run
31
+ .process(text: text, dest: options['dest'])
32
+ end
25
33
  end
26
34
  end
27
35
  end
@@ -0,0 +1,42 @@
1
+ module Papago
2
+ module Actions
3
+ class Run
4
+ attr_reader :printer
5
+ attr_reader :service
6
+
7
+ def initialize
8
+ @printer = Papago::Printer.new
9
+ @service = create_service(Papago.service_name || 'youdao')
10
+ end
11
+
12
+ private
13
+
14
+ def create_service(name)
15
+ if name.to_sym == :openai
16
+ Papago::OpenAIService.new(config: Papago.config)
17
+ else
18
+ DolphinKit.create_service(name)
19
+ end
20
+ end
21
+
22
+ def self.process(text:, dest:)
23
+ instance = self.new
24
+ printer = instance.printer
25
+ service = instance.service
26
+
27
+ case service.name
28
+ when :baidu, :qcloud
29
+ printer.println(
30
+ service.name,
31
+ service.call(text: text, target: dest)
32
+ )
33
+ else
34
+ printer.println(
35
+ service.name,
36
+ service.call(text: text)
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,22 @@
1
+ module Papago
2
+ module Actions
3
+ class Switch
4
+ CHOICES = %w{baidu openai qcloud youdao}
5
+
6
+ def self.process(config:)
7
+ prompt = TTY::Prompt.new
8
+ choice = prompt.select('Choose a service?', CHOICES)
9
+
10
+ config.set('translator.service_name', value: choice)
11
+ config.write(force: true)
12
+
13
+ if choice == 'openai' and (config.fetch('openai.endpoint').nil? or prompt.yes?('Update endpoint?'))
14
+ if endpoint = prompt.ask('OpenAI endpoint:')
15
+ config.set('openai.endpoint', value: endpoint)
16
+ config.write(force: true)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,68 @@
1
+ module Papago
2
+ class Printer
3
+ def println(name, data)
4
+ puts send(:"result_#{name}", data)
5
+ end
6
+
7
+ private
8
+
9
+ def result_baidu(data)
10
+ result = data['trans_result']&.first
11
+ return data if result.nil?
12
+
13
+ tree_format({
14
+ name: result['src'],
15
+ contents: [
16
+ {
17
+ name: result['dst']
18
+ }
19
+ ]
20
+ })
21
+ end
22
+
23
+ def result_openai(data)
24
+ result = data['choices']&.first['text']
25
+ return data if result.nil?
26
+
27
+ tree_format({
28
+ name: result&.strip
29
+ })
30
+ end
31
+
32
+ def result_qcloud(data)
33
+ result = data['TargetText']
34
+ return data if result.nil?
35
+
36
+ tree_format({
37
+ name: result
38
+ })
39
+ end
40
+
41
+ def result_youdao(data)
42
+ return data unless data['errorCode'].zero?
43
+
44
+ explains = data.fetch('web', []).map { |explain|
45
+ {
46
+ name: "#{explain.fetch('key')}: #{explain.fetch('value').join(' ')}"
47
+ }
48
+ }
49
+
50
+ result = {
51
+ name: "#{data['query']} [#{data.dig('basic', 'phonetic')}]".gsub(' []', ''),
52
+ contents: [
53
+ {
54
+ name: data.fetch('translation').join(' '),
55
+ contents: (data.dig('basic', 'explains') || []).map { |explain| { name: explain } }
56
+ }
57
+ ] + explains
58
+ }
59
+
60
+ tree_format(result)
61
+ end
62
+
63
+ def tree_format(data)
64
+ result = StringIO.new(Treely.tree([data]).to_s)
65
+ Lol.cat(result, freq: 0.25, spread: 8.0, os: rand(128))
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,41 @@
1
+ require 'securerandom'
2
+
3
+ module Papago
4
+ class OpenAIService
5
+ attr_reader :name
6
+
7
+ def initialize(api_key: nil, config:)
8
+ @endpoint = config.fetch('openai.endpoint')
9
+ @token = api_key || ENV['OPENAI_FANYI_API_KEY']
10
+
11
+ @config = config
12
+ @name = :openai
13
+ end
14
+
15
+ def call(text:, user: nil)
16
+ user ||= generate_user_id
17
+ require 'http' unless defined?(HTTP)
18
+
19
+ response = HTTP
20
+ .auth("Token #{@token}")
21
+ .post(@endpoint, json: { prompt: text, user: user })
22
+
23
+ JSON.parse(response.body)
24
+ end
25
+
26
+ private
27
+
28
+ def generate_user_id
29
+ @user_id ||= @config.fetch('user_id')
30
+
31
+ if @user_id.nil?
32
+ @user_id = SecureRandom.hex
33
+
34
+ @config.set('user_id', value: @user_id)
35
+ @config.write(force: true)
36
+ end
37
+
38
+ @user_id
39
+ end
40
+ end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module Papago
2
- VERSION = '1.0.7'
2
+ VERSION = '1.1.0'
3
3
  end
data/lib/papago.rb CHANGED
@@ -1,18 +1,55 @@
1
1
  require 'json'
2
- require 'open-uri'
2
+ require 'lolcat'
3
+ require 'treely'
4
+ require 'stringio'
5
+ require 'fileutils'
6
+ require 'tty-config'
7
+ require 'tty-prompt'
8
+ require 'dolphin_kit'
3
9
 
4
10
  require 'papago/version'
5
- require 'papago/translator'
6
- require 'papago/configuration'
11
+ require 'papago/services/openai_service'
12
+ require 'papago/actions/switch'
13
+ require 'papago/actions/run'
14
+ require 'papago/printer'
7
15
 
8
16
  module Papago
9
- module_function
17
+ class Application
18
+ attr_reader :config
10
19
 
11
- def translate(text)
12
- Translator.new(text)
20
+ def initialize
21
+ @config = TTY::Config.new
22
+ @config_dir = File.dirname(config_file)
23
+
24
+ unless File.exist?(@config_dir)
25
+ FileUtils.mkdir(@config_dir)
26
+ end
27
+
28
+ @config.append_path @config_dir
29
+
30
+ if File.exist?(config_file)
31
+ @config.read
32
+ else
33
+ @config.write
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def config_file
40
+ @config_file ||= "#{Dir.home}/.papago/config.yml"
41
+ end
42
+ end
43
+
44
+ def self.service_name
45
+ config.fetch('translator.service_name')
46
+ end
47
+
48
+ def self.application
49
+ @application ||= Application.new
13
50
  end
14
51
 
15
- def [](name)
16
- configuration.send(name)
52
+ def self.config
53
+ @config ||= application.config
17
54
  end
18
55
  end
metadata CHANGED
@@ -1,71 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: papago
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - matark
7
+ - razeos at tossdev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-24 00:00:00.000000000 Z
11
+ date: 2023-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: minitest
14
+ name: mercenary
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '5.11'
20
- type: :development
19
+ version: 0.4.0
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '5.11'
26
+ version: 0.4.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: activesupport
28
+ name: tty-config
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '5.2'
33
+ version: 0.6.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '5.2'
40
+ version: 0.6.0
41
41
  - !ruby/object:Gem::Dependency
42
- name: mercenary
42
+ name: tty-prompt
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.3.6
47
+ version: 0.23.1
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.3.6
54
+ version: 0.23.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: dolphin_kit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: lolcat
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - "~>"
60
74
  - !ruby/object:Gem::Version
61
- version: '99.9'
75
+ version: '100.0'
62
76
  type: :runtime
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
- version: '99.9'
82
+ version: '100.0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: treely
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -80,8 +94,22 @@ dependencies:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: http
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '5.1'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '5.1'
83
111
  description:
84
- email: matark@naver.com
112
+ email:
85
113
  executables:
86
114
  - papago
87
115
  extensions: []
@@ -89,10 +117,12 @@ extra_rdoc_files: []
89
117
  files:
90
118
  - bin/papago
91
119
  - lib/papago.rb
92
- - lib/papago/configuration.rb
93
- - lib/papago/translator.rb
120
+ - lib/papago/actions/run.rb
121
+ - lib/papago/actions/switch.rb
122
+ - lib/papago/printer.rb
123
+ - lib/papago/services/openai_service.rb
94
124
  - lib/papago/version.rb
95
- homepage: https://opensource.matark.de/papago
125
+ homepage: https://papago.vercel.app
96
126
  licenses:
97
127
  - MIT
98
128
  metadata: {}
@@ -111,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
141
  - !ruby/object:Gem::Version
112
142
  version: '0'
113
143
  requirements: []
114
- rubygems_version: 3.0.3
144
+ rubygems_version: 3.1.2
115
145
  signing_key:
116
146
  specification_version: 4
117
147
  summary: A simple command-line translator
@@ -1,22 +0,0 @@
1
- module Papago
2
- class Configuration
3
- attr_accessor :key
4
- attr_accessor :keyfrom
5
-
6
- def api_key=(api_key)
7
- @keyfrom, @key = api_key&.gsub(/[\w]+/).to_a
8
- end
9
- end
10
-
11
- def self.configuration
12
- @configuration ||= Configuration.new
13
- end
14
-
15
- def self.configuration=(config)
16
- @configuration = config
17
- end
18
-
19
- def self.configure
20
- yield configuration
21
- end
22
- end
@@ -1,35 +0,0 @@
1
- module Papago
2
- class Translator
3
- def initialize(text)
4
- @request = URI('https://fanyi.youdao.com/openapi.do')
5
- @request.query = URI.encode_www_form({
6
- :keyfrom => Papago[:keyfrom],
7
- :key => Papago[:key],
8
- :type => 'data',
9
- :doctype => 'json',
10
- :version => '1.1',
11
- :q => text
12
- })
13
-
14
- build_context!
15
- end
16
-
17
- def contents
18
- [
19
- {
20
- name: "#{@context.fetch(:query)} [#{@context.dig(:basic, :phonetic)}]",
21
- contents: [
22
- {
23
- name: @context.fetch(:translation).join(%{ }),
24
- contents: (@context.dig(:basic, :explains) || []).map { |explain| { name: explain } }
25
- }
26
- ] + @context.fetch(:web, []).map { |explain| { name: "#{explain.fetch(:key)}: #{explain.fetch(:value).join(%{ })}" } }
27
- }
28
- ]
29
- end
30
-
31
- def build_context!
32
- @context = JSON.parse(@request.read, symbolize_names: true)
33
- end
34
- end
35
- end