arb-dict 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/arb-dict +11 -30
- data/lib/arb/dict/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff8c1fa03b78d918c55d8b7192c73c0286d0d2e5
|
4
|
+
data.tar.gz: e3dea50d00516853f8ba872ee3236e49066651c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca187d3e303a2a0c43118536ca449399b2a3dc7e48fbb6a89ba984ffc5b6c88a41b91b09f49d6a2f1c2069dc6135b4ed888aa998534d499a3621a2ac72ccf91f
|
7
|
+
data.tar.gz: 76e7ef3204cefee61be98a6880bf664d2a3a5bf48d4e6ed4d4ae36d960ea6e0b365c6c5025b347286e32f261598cb70dabcc9db3a5e5f8f230d230679e9b3187
|
data/bin/arb-dict
CHANGED
@@ -1,38 +1,31 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
require 'uri'
|
2
3
|
require 'csv'
|
3
4
|
require 'slop'
|
4
5
|
require 'json'
|
5
6
|
require 'open-uri'
|
6
7
|
|
7
|
-
|
8
|
-
define_method :fmt_json do |main_hash|
|
8
|
+
define_method :fmt_json do |main_hash,*args|
|
9
9
|
puts main_hash.to_json
|
10
10
|
end
|
11
11
|
|
12
|
-
define_method :fmt_csv do |main_hash|
|
13
|
-
tmp_arr = [main_hash[:entity], main_hash[:phonetic_us], main_hash[:phonetic_uk],main_hash[:translation]
|
12
|
+
define_method :fmt_csv do |main_hash,separator|
|
13
|
+
tmp_arr = [main_hash[:entity], main_hash[:phonetic_us], main_hash[:phonetic_uk],main_hash[:translation].join(separator)]
|
14
14
|
puts tmp_arr.to_csv
|
15
15
|
end
|
16
16
|
|
17
|
-
define_method :fmt_raw do |main_hash|
|
17
|
+
define_method :fmt_raw do |main_hash,*args|
|
18
18
|
puts main_hash[:entity], ''
|
19
|
-
puts "US[#{main_hash[:phonetic_us]}]"
|
20
|
-
puts "UK[#{main_hash[:phonetic_uk]}]"
|
21
|
-
puts ''
|
19
|
+
puts "US[#{main_hash[:phonetic_us]}]"
|
20
|
+
puts "UK[#{main_hash[:phonetic_uk]}]"
|
21
|
+
puts ''
|
22
22
|
puts main_hash[:translation]
|
23
|
-
if main_hash[:sentences].size>0
|
24
|
-
puts ''
|
25
|
-
main_hash[:sentences].each do |sentence|
|
26
|
-
puts sentence[:entity], sentence[:translation], ''
|
27
|
-
end
|
28
|
-
end
|
29
23
|
end
|
30
24
|
|
31
|
-
|
32
25
|
begin
|
33
26
|
opts = Slop.parse do |o|
|
34
|
-
o.bool *%w{-s --sentence}, 'show sentences'
|
35
27
|
o.string *%w{-f --format}, 'available choices: json,csv,raw(default)', default: 'raw'
|
28
|
+
o.string *%w{-s --separator},'translations separator used in csv format(default: NEWLINE)',default: "\n"
|
36
29
|
end
|
37
30
|
rescue Slop::MissingArgument
|
38
31
|
puts 'Missing arguments!'
|
@@ -43,28 +36,16 @@ end
|
|
43
36
|
if opts
|
44
37
|
entity = opts.args.first
|
45
38
|
if entity
|
46
|
-
tmp_hash = JSON.parse(open("http://dict.youdao.com/jsonresult?q=#{entity}&type=1").read)
|
39
|
+
tmp_hash = JSON.parse(open("http://dict.youdao.com/jsonresult?q=#{URI.encode(entity)}&type=1").read)
|
47
40
|
main_hash = {
|
48
41
|
entity: entity,
|
49
42
|
phonetic_us: (tmp_hash['sm'] || 'No US Phonetic'),
|
50
43
|
phonetic_uk: (tmp_hash['uksm'] || 'No UK Phonetic'),
|
51
44
|
translation: (tmp_hash['basic'] || 'No Translation')
|
52
45
|
}
|
53
|
-
|
54
|
-
if opts.sentence?
|
55
|
-
JSON.parse(open("http://dict.youdao.com/jsonapi?q=#{entity}").read)['blng_sents_part']['sentence-pair'].each do |inner_hash|
|
56
|
-
main_hash[:sentences] << {
|
57
|
-
entity: inner_hash['sentence'],
|
58
|
-
translation: inner_hash['sentence-translation']
|
59
|
-
}
|
60
|
-
end
|
61
|
-
end
|
62
|
-
send "fmt_#{opts[:format]}", main_hash
|
46
|
+
send "fmt_#{opts[:format]}", main_hash,opts[:separator]
|
63
47
|
else
|
64
48
|
puts 'Missing target, please specify a word or phrase!'
|
65
49
|
exit
|
66
50
|
end
|
67
51
|
end
|
68
|
-
|
69
|
-
|
70
|
-
|
data/lib/arb/dict/version.rb
CHANGED