tli 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/tli +6 -32
- data/lib/application.rb +19 -0
- data/lib/config.rb +25 -0
- data/lib/dictionaries/dictionary.rb +11 -0
- data/lib/dictionaries/google_dictionary.rb +88 -0
- data/lib/google.rb +31 -0
- data/lib/help.rb +6 -0
- data/lib/player.rb +8 -0
- data/lib/string_util.rb +14 -0
- data/lib/tli.rb +163 -4
- data/lib/translation.rb +8 -0
- data/lib/translators/google_translator.rb +62 -34
- data/lib/translators/translator.rb +10 -2
- metadata +15 -5
- data/lib/text_decoder.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 730a60e8950b873bbc345d1895d469f73f7f3558
|
4
|
+
data.tar.gz: abb4e3490296da6ee68cea8f16280b6b19b8a54c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d32ee6f5b694a17a618a3b7206c4024bb8a2cc60c2b026028cb6d5bf96a8cb467182780c36ee919e3995b5be0d8db40ef3df26ed6662aa3dfb6f28b655671096
|
7
|
+
data.tar.gz: b4cc9323ad4b1c4bfa0b6d181694b93a852ab9d323a0c1f7f6a037e52fa951f2ebdb9a654cc9ed96b1c6b9eccdd767b731460ea747a8c5a0ab1e385b377b8e50
|
data/bin/tli
CHANGED
@@ -1,35 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require_relative '../lib/tli'
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
'--backend' => 'option_value',
|
10
|
-
'-b' => 'option_value',
|
11
|
-
'--play' => 'option',
|
12
|
-
'-p' => 'option'
|
13
|
-
}
|
14
|
-
|
15
|
-
|
16
|
-
length = ARGV.length
|
17
|
-
params = Hash.new('')
|
18
|
-
index = 0
|
19
|
-
while index < length
|
20
|
-
arg = ARGV[index]
|
21
|
-
if OPTIONS.include?(arg)
|
22
|
-
if OPTIONS[arg] == 'option_value'
|
23
|
-
raise "#{arg} requires a value." if index + 1 >= length
|
24
|
-
params[arg] = ARGV[index+1]
|
25
|
-
index += 1
|
26
|
-
else
|
27
|
-
params[arg] = true
|
28
|
-
end
|
29
|
-
else
|
30
|
-
params[:text] << ' ' + arg
|
31
|
-
end
|
32
|
-
index += 1
|
3
|
+
begin
|
4
|
+
Tli.new.invoke(ARGV)
|
5
|
+
exit(0)
|
6
|
+
rescue => e
|
7
|
+
STDERR.puts e.message
|
8
|
+
exit(1)
|
33
9
|
end
|
34
|
-
|
35
|
-
puts Tli.translate(params[:text], params['--source'], params['--target'])
|
data/lib/application.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative 'config'
|
2
|
+
# Provides configuration values for the application.
|
3
|
+
class Application
|
4
|
+
def self.app_dir
|
5
|
+
@app_dir ||= CONFIG[CONFIG[:env]][:app_dir]
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.db_file
|
9
|
+
@db_file ||= CONFIG[CONFIG[:env]][:db_file]
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.strings_dir
|
13
|
+
@strings_dir ||= CONFIG[CONFIG[:env]][:strings_dir]
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.player
|
17
|
+
@player ||= CONFIG[CONFIG[:env]][:player]
|
18
|
+
end
|
19
|
+
end
|
data/lib/config.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
working_directory = File.expand_path(File.dirname(__FILE__))
|
2
|
+
CONFIG = {
|
3
|
+
env: :production,
|
4
|
+
development: {
|
5
|
+
app_dir: working_directory + '/../app_dir',
|
6
|
+
strings_dir: working_directory + '/../assets/strings',
|
7
|
+
db_file: working_directory + '/../app_dir/db/translations.db',
|
8
|
+
cache_results: false,
|
9
|
+
player: 'touch'
|
10
|
+
},
|
11
|
+
test: {
|
12
|
+
app_dir: working_directory + '/../app_dir',
|
13
|
+
strings_dir: working_directory + '/../assets/strings',
|
14
|
+
db_file: working_directory + '/../app_dir/db/translations.db',
|
15
|
+
cache_results: false,
|
16
|
+
player: 'touch'
|
17
|
+
},
|
18
|
+
production: {
|
19
|
+
app_dir: Dir.home + '/.tli',
|
20
|
+
strings_dir: working_directory + '/../assets/strings',
|
21
|
+
db_file: Dir.home + '/.tli/db/translations.db',
|
22
|
+
cache_results: true,
|
23
|
+
player: 'mplayer'
|
24
|
+
}
|
25
|
+
}
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require_relative 'dictionary'
|
2
|
+
require_relative '../google'
|
3
|
+
|
4
|
+
# Class for the Google Dictionary (actually Translate)
|
5
|
+
class GoogleDictionary < Dictionary
|
6
|
+
include Google
|
7
|
+
API_URL = 'http://translate.google.com/translate_a/t'
|
8
|
+
|
9
|
+
def self.name
|
10
|
+
'Google Translate'
|
11
|
+
end
|
12
|
+
|
13
|
+
def provide_tts?
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
def langs
|
18
|
+
{ 'af' => 'Afrikaans', 'sq' => 'Albanian', 'ar' => 'Arabic',
|
19
|
+
'hy' => 'Armenian', 'az' => 'Azerbaijani', 'eu' => 'Basque',
|
20
|
+
'be' => 'Belarusian', 'bg' => 'Bulgarian', 'ca' => 'Catalan',
|
21
|
+
'hr' => 'Croatian', 'cs' => 'Czech', 'et' => 'Estonian',
|
22
|
+
'da' => 'Danish', 'nl' => 'Dutch', 'en' => 'English',
|
23
|
+
'tl' => 'Filipino', 'fi' => 'Finnish', 'fr' => 'French',
|
24
|
+
'gl' => 'Galician', 'ht' => 'Haitian, Creole', 'is' => 'Icelandic',
|
25
|
+
'ka' => 'Georgian', 'de' => 'German', 'el' => 'Greek',
|
26
|
+
'iw' => 'Hebrew', 'hi' => 'Hindi', 'hu' => 'Hungarian',
|
27
|
+
'id' => 'Indonesian', 'ga' => 'Irish', 'it' => 'Italian',
|
28
|
+
'ko' => 'Korean', 'la' => 'Latin', 'lv' => 'Latvian',
|
29
|
+
'mk' => 'Macedonian', 'ms' => 'Malay', 'mt' => 'Maltese',
|
30
|
+
'fa' => 'Persian', 'pl' => 'Polish', 'pt' => 'Portuguese',
|
31
|
+
'ro' => 'Romanian', 'sl' => 'Slovenian', 'th' => 'Thai',
|
32
|
+
'ja' => 'Japanese', 'lt' => 'Lithuanian', 'no' => 'Norwegian',
|
33
|
+
'vi' => 'Vietnamese', 'cy' => 'Welsh', 'yi' => 'Yiddish',
|
34
|
+
'ru' => 'Russian', 'sr' => 'Serbian', 'sk' => 'Slovak',
|
35
|
+
'es' => 'Spanish', 'sw' => 'Swahili', 'sv' => 'Swedish',
|
36
|
+
'tr' => 'Turkish', 'uk' => 'Ukrainian', 'ur' => 'Urdu',
|
37
|
+
'zh-TW' => 'Chinese, (Traditional)', 'zh-CN' => 'Chinese, (Simplified)' }
|
38
|
+
end
|
39
|
+
|
40
|
+
def define(word, source, target, options = {})
|
41
|
+
fail "Unknown language code '#{source}'" unless langs.include?(source)
|
42
|
+
fail "Unknown language code '#{target}'" unless langs.include?(target)
|
43
|
+
|
44
|
+
if options[:tts]
|
45
|
+
audio_file = StringUtil.tts_file_name(word, source, 'google')
|
46
|
+
get_pronunciation(word, source, audio_file) unless File.exist?(audio_file)
|
47
|
+
end
|
48
|
+
|
49
|
+
json = JSON.parse(get_data(word, source, target, options))
|
50
|
+
render extract_definitions(json)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def extract_definitions(json)
|
56
|
+
result = []
|
57
|
+
if json.include?('dict')
|
58
|
+
json['dict'].each do |item|
|
59
|
+
definition = { class: item['pos'], entries: [] }
|
60
|
+
item['entry'].each do |e|
|
61
|
+
data = { word: e['word'], score: (e['score'] || 1e9) }
|
62
|
+
definition[:entries] << data
|
63
|
+
end
|
64
|
+
result << definition
|
65
|
+
end
|
66
|
+
elsif json.include?('sentences')
|
67
|
+
json['sentences'].each do |item|
|
68
|
+
if item.include?('trans')
|
69
|
+
definition = { class: 'translation', entries: [] }
|
70
|
+
definition[:entries] << { word: item['trans'], score: 0 }
|
71
|
+
result << definition
|
72
|
+
break
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
result
|
77
|
+
end
|
78
|
+
|
79
|
+
def render(result)
|
80
|
+
output = ''
|
81
|
+
result.each do |entry|
|
82
|
+
output << "#{entry[:class]}\n#{'-' * entry[:class].length}\n"
|
83
|
+
entry[:entries].sort! { |x, y| x[:score] <=> y[:score] }
|
84
|
+
output << entry[:entries].map { |e| e[:word] }.join(', ') + "\n\n"
|
85
|
+
end
|
86
|
+
output
|
87
|
+
end
|
88
|
+
end
|
data/lib/google.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Provides common functionality for both GoogleTranslator and GoogleDictionary
|
2
|
+
module Google
|
3
|
+
TEXT_API_URL = 'http://translate.google.com/translate_a/t'
|
4
|
+
SPEECH_API_URL = 'http://translate.google.com/translate_tts'
|
5
|
+
|
6
|
+
def get_pronunciation(text, source, audio_file)
|
7
|
+
File.open(audio_file, 'w') do |f|
|
8
|
+
f.write(RestClient.get(SPEECH_API_URL, params: { tl: source, q: text }))
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_translation(text, source, target)
|
13
|
+
headers = {
|
14
|
+
params: { client: 'p', text: text, sl: source, tl: target },
|
15
|
+
user_agent: 'Mozilla/5.0'
|
16
|
+
}
|
17
|
+
RestClient.get(TEXT_API_URL, headers).to_s.force_encoding(Encoding::UTF_8)
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_data(text, source, target, options = {})
|
21
|
+
return get_translation(text, source, target) unless options[:cache_results]
|
22
|
+
|
23
|
+
data = { text: text, source: source, target: target, service: 'google' }
|
24
|
+
entry = Translation.find_by(data)
|
25
|
+
if entry.nil?
|
26
|
+
data[:response] = get_translation(text, source, target)
|
27
|
+
entry = Translation.create(data)
|
28
|
+
end
|
29
|
+
entry.response
|
30
|
+
end
|
31
|
+
end
|
data/lib/help.rb
ADDED
data/lib/player.rb
ADDED
data/lib/string_util.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
require_relative 'application'
|
3
|
+
|
4
|
+
# String-related helper functions
|
5
|
+
class StringUtil
|
6
|
+
def self.tts_file_name(text, target, service)
|
7
|
+
hash = StringUtil.tts_hash(text, target, service)
|
8
|
+
"#{Application.app_dir}/pronunciations/#{hash}.mp3"
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.tts_hash(text, target, service)
|
12
|
+
Digest::SHA1.hexdigest("#{text}_#{target}_#{service}")
|
13
|
+
end
|
14
|
+
end
|
data/lib/tli.rb
CHANGED
@@ -1,7 +1,166 @@
|
|
1
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'readline'
|
3
|
+
require_relative 'translators/google_translator'
|
4
|
+
require_relative 'dictionaries/google_dictionary'
|
5
|
+
require_relative 'help'
|
6
|
+
require_relative 'translation'
|
7
|
+
|
8
|
+
# Command Line Interface for the translator
|
2
9
|
class Tli
|
3
|
-
|
4
|
-
|
5
|
-
|
10
|
+
include Help
|
11
|
+
DEFAULTS = {
|
12
|
+
service: 'google',
|
13
|
+
source: 'en',
|
14
|
+
target: 'es',
|
15
|
+
player: Application.player
|
16
|
+
}
|
17
|
+
|
18
|
+
OPTIONS = {
|
19
|
+
source: :key_value,
|
20
|
+
target: :key_value,
|
21
|
+
service: :key_value,
|
22
|
+
info: :key_value,
|
23
|
+
player: :key_value,
|
24
|
+
cache_results: :flag,
|
25
|
+
play: :flag,
|
26
|
+
help: :flag,
|
27
|
+
lts: :flag
|
28
|
+
}
|
29
|
+
|
30
|
+
SERVICES = {
|
31
|
+
'google' => GoogleTranslator
|
32
|
+
}
|
33
|
+
|
34
|
+
TRANSLATORS = {
|
35
|
+
'google' => GoogleTranslator.new
|
36
|
+
}
|
37
|
+
|
38
|
+
DICTIONARIES = {
|
39
|
+
'google' => GoogleDictionary.new
|
40
|
+
}
|
41
|
+
|
42
|
+
attr_reader :stdin, :stdout, :stderr
|
43
|
+
|
44
|
+
def initialize(readline = Readline, stdin = $stdin,
|
45
|
+
stdout = $stdout, stderr = $stderr)
|
46
|
+
@readline = readline
|
47
|
+
@stdin = stdin
|
48
|
+
@stdout = stdout
|
49
|
+
@stderr = stderr
|
50
|
+
end
|
51
|
+
|
52
|
+
def invoke(args)
|
53
|
+
params = parse_options(args)
|
54
|
+
params = read_config_file(params)
|
55
|
+
|
56
|
+
return stdout.puts help if params[:help] == true
|
57
|
+
return stdout.puts list_services if params[:lts] == true
|
58
|
+
return stdout.puts info(params[:info]) unless params[:info].empty?
|
59
|
+
|
60
|
+
params[:source] = DEFAULTS[:source] if params[:source].empty?
|
61
|
+
params[:target] = DEFAULTS[:target] if params[:target].empty?
|
62
|
+
params[:service] = DEFAULTS[:service] if params[:service].empty?
|
63
|
+
params[:player] = DEFAULTS[:player] if params[:player].empty?
|
64
|
+
|
65
|
+
if !params[:words].empty?
|
66
|
+
process_input(params)
|
67
|
+
else
|
68
|
+
while buf = @readline.readline('> ', true)
|
69
|
+
params[:words] = buf.split(/\s+/)
|
70
|
+
process_input(params)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def process_input(params)
|
76
|
+
options = {
|
77
|
+
tts: params[:play] == true,
|
78
|
+
cache_results: params[:cache_results] == true,
|
79
|
+
player: params[:player]
|
80
|
+
}
|
81
|
+
if params[:words].length == 1
|
82
|
+
define(params[:words].join(' '), params[:source],
|
83
|
+
params[:target], params[:service], options)
|
84
|
+
elsif params[:words].length > 1
|
85
|
+
translate(params[:words].join(' '), params[:source],
|
86
|
+
params[:target], params[:service], options)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def translate(text, source, target, service, options = {})
|
91
|
+
result = TRANSLATORS[service].translate(text, source, target, options)
|
92
|
+
if options[:tts] && TRANSLATORS[service].provide_tts?
|
93
|
+
stdout.puts '♬'
|
94
|
+
stdout.puts result
|
95
|
+
audio_file = StringUtil.tts_file_name(text, source, service)
|
96
|
+
Player.play(audio_file, options[:player])
|
97
|
+
else
|
98
|
+
stdout.puts result
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def define(word, source, target, service, options = {})
|
103
|
+
result = DICTIONARIES[service].define(word, source, target, options)
|
104
|
+
if options[:tts] && DICTIONARIES[service].provide_tts?
|
105
|
+
stdout.puts '♬'
|
106
|
+
stdout.puts result
|
107
|
+
audio_file = StringUtil.tts_file_name(word, source, service)
|
108
|
+
Player.play(audio_file, options[:player])
|
109
|
+
else
|
110
|
+
stdout.puts result
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def parse_options(args)
|
115
|
+
length = args.length
|
116
|
+
params = Hash.new('')
|
117
|
+
params[:words] = []
|
118
|
+
index = 0
|
119
|
+
while index < length
|
120
|
+
arg = args[index]
|
121
|
+
unless arg.start_with?('--')
|
122
|
+
params[:words] << arg
|
123
|
+
index += 1
|
124
|
+
next
|
125
|
+
end
|
126
|
+
|
127
|
+
sym = arg[2..-1].to_sym
|
128
|
+
fail "#{arg}: not a valid option" unless OPTIONS.include?(sym)
|
129
|
+
if OPTIONS[sym] == :key_value
|
130
|
+
fail "#{arg} requires a value" if index + 1 >= length
|
131
|
+
params[sym] = args[index + 1]
|
132
|
+
index += 1
|
133
|
+
else
|
134
|
+
params[sym] = true
|
135
|
+
end
|
136
|
+
index += 1
|
137
|
+
end
|
138
|
+
params
|
139
|
+
end
|
140
|
+
|
141
|
+
def read_config_file(params)
|
142
|
+
if File.exist?(Application.app_dir + '/tli.conf')
|
143
|
+
config = JSON.parse(File.read(Application.app_dir + '/tli.conf'))
|
144
|
+
OPTIONS.each do |key, _|
|
145
|
+
if config['settings'][key.to_s] && params[key].empty?
|
146
|
+
params[key] = config['settings'][key.to_s]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
params
|
151
|
+
end
|
152
|
+
|
153
|
+
def info(service)
|
154
|
+
SERVICES[service].info
|
155
|
+
end
|
156
|
+
|
157
|
+
def list_services
|
158
|
+
list = "Available translation services\n\n"
|
159
|
+
list += "id\t\tName\n"
|
160
|
+
list += "--\t\t----\n"
|
161
|
+
SERVICES.each do |key, value|
|
162
|
+
list += "#{key}\t\t#{value.name}"
|
163
|
+
end
|
164
|
+
list
|
6
165
|
end
|
7
166
|
end
|
data/lib/translation.rb
ADDED
@@ -1,46 +1,74 @@
|
|
1
1
|
require 'rest_client'
|
2
2
|
require_relative 'translator'
|
3
|
-
require_relative '../
|
3
|
+
require_relative '../player'
|
4
|
+
require_relative '../string_util'
|
5
|
+
require_relative '../google'
|
4
6
|
|
7
|
+
# Class for the Google Translate service.
|
5
8
|
class GoogleTranslator < Translator
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
'
|
19
|
-
'
|
20
|
-
'
|
21
|
-
'
|
22
|
-
'
|
23
|
-
'
|
24
|
-
'
|
25
|
-
'
|
26
|
-
'
|
27
|
-
'
|
9
|
+
include Google
|
10
|
+
|
11
|
+
def self.name
|
12
|
+
'Google Translate'
|
13
|
+
end
|
14
|
+
|
15
|
+
def provide_tts?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
def langs
|
20
|
+
{ 'af' => 'Afrikaans', 'sq' => 'Albanian', 'ar' => 'Arabic',
|
21
|
+
'hy' => 'Armenian', 'az' => 'Azerbaijani', 'eu' => 'Basque',
|
22
|
+
'be' => 'Belarusian', 'bg' => 'Bulgarian', 'ca' => 'Catalan',
|
23
|
+
'hr' => 'Croatian', 'cs' => 'Czech', 'et' => 'Estonian',
|
24
|
+
'da' => 'Danish', 'nl' => 'Dutch', 'en' => 'English',
|
25
|
+
'tl' => 'Filipino', 'fi' => 'Finnish', 'fr' => 'French',
|
26
|
+
'gl' => 'Galician', 'ht' => 'Haitian, Creole', 'is' => 'Icelandic',
|
27
|
+
'ka' => 'Georgian', 'de' => 'German', 'el' => 'Greek',
|
28
|
+
'iw' => 'Hebrew', 'hi' => 'Hindi', 'hu' => 'Hungarian',
|
29
|
+
'id' => 'Indonesian', 'ga' => 'Irish', 'it' => 'Italian',
|
30
|
+
'ko' => 'Korean', 'la' => 'Latin', 'lv' => 'Latvian',
|
31
|
+
'mk' => 'Macedonian', 'ms' => 'Malay', 'mt' => 'Maltese',
|
32
|
+
'fa' => 'Persian', 'pl' => 'Polish', 'pt' => 'Portuguese',
|
33
|
+
'ro' => 'Romanian', 'sl' => 'Slovenian', 'th' => 'Thai',
|
34
|
+
'ja' => 'Japanese', 'lt' => 'Lithuanian', 'no' => 'Norwegian',
|
35
|
+
'vi' => 'Vietnamese', 'cy' => 'Welsh', 'yi' => 'Yiddish',
|
36
|
+
'ru' => 'Russian', 'sr' => 'Serbian', 'sk' => 'Slovak',
|
37
|
+
'es' => 'Spanish', 'sw' => 'Swahili', 'sv' => 'Swedish',
|
38
|
+
'tr' => 'Turkish', 'uk' => 'Ukrainian', 'ur' => 'Urdu',
|
39
|
+
'zh-TW' => 'Chinese, (Traditional)', 'zh-CN' => 'Chinese, (Simplified)' }
|
28
40
|
end
|
29
41
|
|
30
|
-
def translate(text, source, target)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
42
|
+
def translate(text, source, target, options = {})
|
43
|
+
fail "Unknown language code '#{source}'" unless langs.include?(source)
|
44
|
+
fail "Unknown language code '#{target}'" unless langs.include?(target)
|
45
|
+
|
46
|
+
if options[:tts]
|
47
|
+
audio_file = StringUtil.tts_file_name(text, source, 'google')
|
48
|
+
unless File.exist?(audio_file)
|
49
|
+
get_pronunciation(text, source, audio_file)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
json = JSON.parse(get_data(text, source, target, options))
|
53
|
+
extract_translation(json)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.info
|
57
|
+
File.open(Application.strings_dir + '/google_info.txt').read
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def extract_translation(json)
|
35
63
|
translation = ''
|
36
|
-
json
|
37
|
-
|
38
|
-
|
39
|
-
|
64
|
+
if json.include?('sentences')
|
65
|
+
json['sentences'].each do |entry|
|
66
|
+
if entry.include?('trans')
|
67
|
+
translation = entry['trans']
|
68
|
+
break
|
69
|
+
end
|
40
70
|
end
|
41
71
|
end
|
42
72
|
translation
|
43
73
|
end
|
44
74
|
end
|
45
|
-
|
46
|
-
|
@@ -1,4 +1,12 @@
|
|
1
|
+
# Base class for translators
|
1
2
|
class Translator
|
2
|
-
def translate(text, source, target); end
|
3
|
-
|
3
|
+
def translate(text, source, target, options = {}); end
|
4
|
+
|
5
|
+
def langs; end
|
6
|
+
|
7
|
+
def provide_tts?; end
|
8
|
+
|
9
|
+
def info; end
|
10
|
+
|
11
|
+
def self.name; end
|
4
12
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafael Rendón Pablo
|
@@ -10,8 +10,10 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2015-02-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
14
|
-
|
13
|
+
description: |-
|
14
|
+
With tli you can translate text and find definitions using\
|
15
|
+
services like Google Translate, Yandex, Cambridge \
|
16
|
+
dictionaries, etc. All in one.
|
15
17
|
email: rafaelrendonpablo@gmail.com
|
16
18
|
executables:
|
17
19
|
- tli
|
@@ -19,8 +21,16 @@ extensions: []
|
|
19
21
|
extra_rdoc_files: []
|
20
22
|
files:
|
21
23
|
- bin/tli
|
22
|
-
- lib/
|
24
|
+
- lib/application.rb
|
25
|
+
- lib/config.rb
|
26
|
+
- lib/dictionaries/dictionary.rb
|
27
|
+
- lib/dictionaries/google_dictionary.rb
|
28
|
+
- lib/google.rb
|
29
|
+
- lib/help.rb
|
30
|
+
- lib/player.rb
|
31
|
+
- lib/string_util.rb
|
23
32
|
- lib/tli.rb
|
33
|
+
- lib/translation.rb
|
24
34
|
- lib/translators/google_translator.rb
|
25
35
|
- lib/translators/translator.rb
|
26
36
|
homepage: https://github.com/rendon/tli
|
@@ -46,6 +56,6 @@ rubyforge_project:
|
|
46
56
|
rubygems_version: 2.4.3
|
47
57
|
signing_key:
|
48
58
|
specification_version: 4
|
49
|
-
summary: TransLate It! A command line tool to translate text from (and to) almost
|
59
|
+
summary: TransLate It! A command line tool to translate text from \ (and to) almost
|
50
60
|
any language.
|
51
61
|
test_files: []
|
data/lib/text_decoder.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
class TextDecoder
|
2
|
-
def self.decode(text, lang_code)
|
3
|
-
result = ''
|
4
|
-
case lang_code
|
5
|
-
when 'en', 'es', 'fr'
|
6
|
-
result = text.force_encoding('ISO-8859-1').encode('UTF-8')
|
7
|
-
when 'ru'
|
8
|
-
result = text.force_encoding('Windows-1251').encode('UTF-8')
|
9
|
-
end
|
10
|
-
result
|
11
|
-
end
|
12
|
-
end
|