papago 1.0.7 → 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/papago +25 -17
- data/lib/papago/actions/run.rb +42 -0
- data/lib/papago/actions/switch.rb +22 -0
- data/lib/papago/printer.rb +68 -0
- data/lib/papago/services/openai_service.rb +39 -0
- data/lib/papago/version.rb +1 -1
- data/lib/papago.rb +46 -8
- metadata +51 -21
- data/lib/papago/configuration.rb +0 -22
- data/lib/papago/translator.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 179b9cb39a88b08caef98a7dd26abb8f98c6d454bad566b22cab24c902cdce48
|
4
|
+
data.tar.gz: d69d05e43e61566daaf51a8aa4e642fad326400509bb7c393f5d6f0e240ae8d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79cf6f39c611b57d7885828c86b7d63a4f89f619e4331ebe10e042cebd4e9441b813a05055a991fc3f41d48330e630cdd484159b89e286e65bbea336d2075f2f
|
7
|
+
data.tar.gz: '013442539123adf7de53317b229df6b28d5432c003077e00d10afc94975cf84c19bb358a3f2628fe41770da83ee8993f681df44378f7d046336c51dc8c25d462'
|
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
|
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
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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,39 @@
|
|
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
|
+
response = HTTP
|
18
|
+
.auth("Token #{@token}")
|
19
|
+
.post(@endpoint, json: { prompt: text, user: user })
|
20
|
+
|
21
|
+
JSON.parse(response.body)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def generate_user_id
|
27
|
+
@user_id ||= @config.fetch('user_id')
|
28
|
+
|
29
|
+
if @user_id.nil?
|
30
|
+
@user_id = SecureRandom.hex
|
31
|
+
|
32
|
+
@config.set('user_id', value: @user_id)
|
33
|
+
@config.write(force: true)
|
34
|
+
end
|
35
|
+
|
36
|
+
@user_id
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/papago/version.rb
CHANGED
data/lib/papago.rb
CHANGED
@@ -1,18 +1,56 @@
|
|
1
1
|
require 'json'
|
2
|
-
require '
|
2
|
+
require 'http'
|
3
|
+
require 'lolcat'
|
4
|
+
require 'treely'
|
5
|
+
require 'stringio'
|
6
|
+
require 'fileutils'
|
7
|
+
require 'tty-config'
|
8
|
+
require 'tty-prompt'
|
9
|
+
require 'dolphin_kit'
|
3
10
|
|
4
11
|
require 'papago/version'
|
5
|
-
require 'papago/
|
6
|
-
require 'papago/
|
12
|
+
require 'papago/services/openai_service'
|
13
|
+
require 'papago/actions/switch'
|
14
|
+
require 'papago/actions/run'
|
15
|
+
require 'papago/printer'
|
7
16
|
|
8
17
|
module Papago
|
9
|
-
|
18
|
+
class Application
|
19
|
+
attr_reader :config
|
10
20
|
|
11
|
-
|
12
|
-
|
21
|
+
def initialize
|
22
|
+
@config = TTY::Config.new
|
23
|
+
@config_dir = File.dirname(config_file)
|
24
|
+
|
25
|
+
unless File.exist?(@config_dir)
|
26
|
+
FileUtils.mkdir(@config_dir)
|
27
|
+
end
|
28
|
+
|
29
|
+
@config.append_path @config_dir
|
30
|
+
|
31
|
+
if File.exist?(config_file)
|
32
|
+
@config.read
|
33
|
+
else
|
34
|
+
@config.write
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def config_file
|
41
|
+
@config_file ||= "#{Dir.home}/.papago/config.yml"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.service_name
|
46
|
+
config.fetch('translator.service_name')
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.application
|
50
|
+
@application ||= Application.new
|
13
51
|
end
|
14
52
|
|
15
|
-
def
|
16
|
-
|
53
|
+
def self.config
|
54
|
+
@config ||= application.config
|
17
55
|
end
|
18
56
|
end
|
metadata
CHANGED
@@ -1,73 +1,101 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: papago
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- razeos at tossdev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: http
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '5.
|
20
|
-
type: :
|
19
|
+
version: '5.1'
|
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.
|
26
|
+
version: '5.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: treely
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '1.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: '
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: lolcat
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '100.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '100.0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: mercenary
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
59
|
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0.
|
61
|
+
version: 0.4.0
|
48
62
|
type: :runtime
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version: 0.
|
68
|
+
version: 0.4.0
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
70
|
+
name: tty-config
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
73
|
- - "~>"
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
75
|
+
version: 0.6.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:
|
82
|
+
version: 0.6.0
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
84
|
+
name: tty-prompt
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.23.1
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.23.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: dolphin_kit
|
71
99
|
requirement: !ruby/object:Gem::Requirement
|
72
100
|
requirements:
|
73
101
|
- - "~>"
|
@@ -81,7 +109,7 @@ dependencies:
|
|
81
109
|
- !ruby/object:Gem::Version
|
82
110
|
version: '1.0'
|
83
111
|
description:
|
84
|
-
email:
|
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/
|
93
|
-
- lib/papago/
|
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://
|
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.
|
144
|
+
rubygems_version: 3.1.2
|
115
145
|
signing_key:
|
116
146
|
specification_version: 4
|
117
147
|
summary: A simple command-line translator
|
data/lib/papago/configuration.rb
DELETED
@@ -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
|
data/lib/papago/translator.rb
DELETED
@@ -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
|