youdao_fanyi 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +25 -0
- data/CHANGELOG +4 -0
- data/Gemfile +4 -0
- data/Guardfile +22 -0
- data/README.markdown +79 -0
- data/Rakefile +2 -0
- data/bin/youdao_fanyi +48 -0
- data/examples/translate.rb +170 -0
- data/lib/generators/youdao_fanyi/install/USAGE +2 -0
- data/lib/generators/youdao_fanyi/install/install_generator.rb +14 -0
- data/lib/generators/youdao_fanyi/install/templates/youdao_fanyi.rb +7 -0
- data/lib/generators/youdao_fanyi/install/templates/youdao_fanyi_api_keys.yml +9 -0
- data/lib/youdao_fanyi/configuration.rb +14 -0
- data/lib/youdao_fanyi/connector.rb +42 -0
- data/lib/youdao_fanyi/errors.rb +15 -0
- data/lib/youdao_fanyi/translator.rb +43 -0
- data/lib/youdao_fanyi/version.rb +6 -0
- data/lib/youdao_fanyi.rb +37 -0
- data/spec/rails3_0_app/.gitignore +4 -0
- data/spec/rails3_0_app/Gemfile +7 -0
- data/spec/rails3_0_app/Rakefile +7 -0
- data/spec/rails3_0_app/app/controllers/application_controller.rb +3 -0
- data/spec/rails3_0_app/app/helpers/application_helper.rb +2 -0
- data/spec/rails3_0_app/app/views/layouts/application.html.erb +14 -0
- data/spec/rails3_0_app/config/application.rb +42 -0
- data/spec/rails3_0_app/config/boot.rb +6 -0
- data/spec/rails3_0_app/config/database.yml +22 -0
- data/spec/rails3_0_app/config/environment.rb +5 -0
- data/spec/rails3_0_app/config/environments/development.rb +26 -0
- data/spec/rails3_0_app/config/environments/production.rb +49 -0
- data/spec/rails3_0_app/config/environments/test.rb +35 -0
- data/spec/rails3_0_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rails3_0_app/config/initializers/inflections.rb +10 -0
- data/spec/rails3_0_app/config/initializers/mime_types.rb +5 -0
- data/spec/rails3_0_app/config/initializers/secret_token.rb +7 -0
- data/spec/rails3_0_app/config/initializers/session_store.rb +8 -0
- data/spec/rails3_0_app/config/initializers/youdao_fanyi.rb +7 -0
- data/spec/rails3_0_app/config/locales/en.yml +5 -0
- data/spec/rails3_0_app/config/routes.rb +58 -0
- data/spec/rails3_0_app/config.ru +4 -0
- data/spec/rails3_0_app/db/seeds.rb +7 -0
- data/spec/rails3_0_app/lib/tasks/.gitkeep +0 -0
- data/spec/rails3_0_app/public/404.html +26 -0
- data/spec/rails3_0_app/public/422.html +26 -0
- data/spec/rails3_0_app/public/500.html +26 -0
- data/spec/rails3_0_app/public/favicon.ico +0 -0
- data/spec/rails3_0_app/public/images/rails.png +0 -0
- data/spec/rails3_0_app/public/index.html +239 -0
- data/spec/rails3_0_app/public/javascripts/.gitkeep +0 -0
- data/spec/rails3_0_app/public/javascripts/application.js +0 -0
- data/spec/rails3_0_app/public/robots.txt +5 -0
- data/spec/rails3_0_app/public/stylesheets/.gitkeep +0 -0
- data/spec/rails3_0_app/script/rails +6 -0
- data/spec/rails3_0_app/vendor/plugins/.gitkeep +0 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/support/youdao_fanyi_api_keys.yml.example +9 -0
- data/spec/youdao_fanyi/configuration_spec.rb +16 -0
- data/spec/youdao_fanyi/connector_spec.rb +86 -0
- data/spec/youdao_fanyi/translator_spec.rb +39 -0
- data/spec/youdao_fanyi_spec.rb +28 -0
- data/youdao_fanyi.gemspec +32 -0
- metadata +260 -0
data/.gitignore
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
|
19
|
+
spec/rails3_0_app/db/*.sqlite3
|
20
|
+
spec/rails3_0_app/log/*
|
21
|
+
spec/rails3_0_app/tmp/*
|
22
|
+
|
23
|
+
|
24
|
+
spec/support/youdao_fanyi_api_keys.yml
|
25
|
+
spec/rails3_0_app/config/youdao_fanyi_api_keys.yml
|
data/CHANGELOG
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :version => 2 do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^spec/.+_spec\.rb$})
|
11
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
12
|
+
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
13
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
14
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
15
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
16
|
+
watch('spec/spec_helper.rb') { "spec" }
|
17
|
+
watch('config/routes.rb') { "spec/routing" }
|
18
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
19
|
+
# Capybara request specs
|
20
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
|
21
|
+
end
|
22
|
+
|
data/README.markdown
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
#YoudaoFanyi
|
2
|
+
|
3
|
+
YoudaoFanyi is a http://fanyi.youdao.com api library for Ruby and Rails3. it only support en<=>zh-CN translate.
|
4
|
+
|
5
|
+
* https://github.com/vkill/youdao_fanyi
|
6
|
+
|
7
|
+
##Supported versions
|
8
|
+
|
9
|
+
* Ruby 1.8.7, 1.9.2, 1.9.3
|
10
|
+
|
11
|
+
* Rails 3.0.x, 3.1
|
12
|
+
|
13
|
+
|
14
|
+
##Use in Ruby scripts
|
15
|
+
|
16
|
+
more see `examples/translate.rb`.
|
17
|
+
|
18
|
+
#encoding: utf-8
|
19
|
+
require "pp"
|
20
|
+
require "youdao_fanyi"
|
21
|
+
|
22
|
+
YoudaoFanyi.configure do |config|
|
23
|
+
config.api_keys = [
|
24
|
+
{ "keyfrom" => "blog-vkill-net", "key" => "2022446265" }
|
25
|
+
]
|
26
|
+
end
|
27
|
+
|
28
|
+
puts YoudaoFanyi.t("china")
|
29
|
+
|
30
|
+
YoudaoFanyi::Translator.translate("china", :i, nil, true, false, "") do |results|
|
31
|
+
p [results.error_code, results.translation, results.query]
|
32
|
+
pp results.results
|
33
|
+
end
|
34
|
+
|
35
|
+
##Use in Rails3
|
36
|
+
|
37
|
+
###Installation
|
38
|
+
|
39
|
+
In your app's `Gemfile`, add:
|
40
|
+
|
41
|
+
gem "youdao_fanyi", :group => [:development]
|
42
|
+
|
43
|
+
Then run:
|
44
|
+
|
45
|
+
> bundle
|
46
|
+
> rails generate youdao_fanyi:install
|
47
|
+
|
48
|
+
Configure your http://fanyi.youdao.com api keys, edit `config/youdao_fanyi_api_keys.yml`
|
49
|
+
|
50
|
+
Configure use proxy, edit `config/initializers/youdao_fanyi.rb`
|
51
|
+
|
52
|
+
|
53
|
+
###Uninstallation from Rails3
|
54
|
+
|
55
|
+
Run:
|
56
|
+
|
57
|
+
> rails destroy youdao_fanyi:install
|
58
|
+
|
59
|
+
|
60
|
+
###Usage Examples
|
61
|
+
|
62
|
+
basic example in the rails console, more see `examples/translate.rb`.
|
63
|
+
|
64
|
+
> YoudaoFanyi.t(:i) => ["我"]
|
65
|
+
> YoudaoFanyi.t(:i, :china) => ["我", "中国"]
|
66
|
+
|
67
|
+
|
68
|
+
When your want translate models attributes, please use https://github.com/vkill/i18n_attributes, it auto use this gem. (WARN: this function wait support!!!)
|
69
|
+
|
70
|
+
|
71
|
+
##Use in shell
|
72
|
+
|
73
|
+
> # youdao_fanyi --key 2022446265 --keyfrom blog-vkill-net "详细" i you
|
74
|
+
|
75
|
+
|
76
|
+
##Copyright
|
77
|
+
|
78
|
+
Copyright (c) 2011 vkill.net .
|
79
|
+
|
data/Rakefile
ADDED
data/bin/youdao_fanyi
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "optparse"
|
3
|
+
|
4
|
+
require "pp"
|
5
|
+
$:.push File.expand_path("../../lib", __FILE__)
|
6
|
+
require "youdao_fanyi"
|
7
|
+
|
8
|
+
options = {}
|
9
|
+
OptionParser.new do |opts|
|
10
|
+
opts.banner = "Usage: youdao_fanyi [--key API_Key] [--keyfrom API_KeyFrom] STRING [STRING, ...]"
|
11
|
+
|
12
|
+
opts.on('--key API_Key', "A 'http://fanyi.youdao.com/fanyiapi?path=data-mode' API key") do |key|
|
13
|
+
options[:key] = key
|
14
|
+
end
|
15
|
+
|
16
|
+
opts.on('--keyfrom API_KeyFrom', "A 'http://fanyi.youdao.com/fanyiapi?path=data-mode' API keyfrom") do |keyfrom|
|
17
|
+
options[:keyfrom] = keyfrom
|
18
|
+
end
|
19
|
+
|
20
|
+
opts.on("-v", "--version", "The current version of YoudaoFanyi") do
|
21
|
+
puts "YoudaoFanyi v#{YoudaoFanyi::VERSION}"
|
22
|
+
exit
|
23
|
+
end
|
24
|
+
|
25
|
+
opts.on("-h", "--help", "This help screen") do
|
26
|
+
puts opts
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
|
30
|
+
end.parse!
|
31
|
+
|
32
|
+
STDERR.puts "fanyi.youdao.com API key is required" unless options[:key]
|
33
|
+
STDERR.puts "fanyi.youdao.com API keyfrom is required" unless options[:keyfrom]
|
34
|
+
STDERR.puts "At least one string to translate is required" unless ARGV.size >= 1
|
35
|
+
|
36
|
+
YoudaoFanyi.configure do |config|
|
37
|
+
config.api_keys = [
|
38
|
+
{ "keyfrom" => options[:keyfrom], "key" => options[:key] }
|
39
|
+
]
|
40
|
+
end
|
41
|
+
|
42
|
+
YoudaoFanyi::Translator.translate(*ARGV) do |results|
|
43
|
+
# puts results.translation
|
44
|
+
puts "======================================="
|
45
|
+
pp results.results
|
46
|
+
puts "======================================="
|
47
|
+
end
|
48
|
+
|
@@ -0,0 +1,170 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
require "pp"
|
4
|
+
$:.push File.expand_path("../../lib", __FILE__)
|
5
|
+
require "youdao_fanyi"
|
6
|
+
|
7
|
+
YoudaoFanyi.configure do |config|
|
8
|
+
config.api_keys = [
|
9
|
+
{ "keyfrom" => "blog-vkill-net", "key" => "2022446265" }
|
10
|
+
]
|
11
|
+
end
|
12
|
+
|
13
|
+
puts YoudaoFanyi.t("china")
|
14
|
+
puts "======================================="
|
15
|
+
|
16
|
+
YoudaoFanyi::Translator.translate("中国", "china", :i, nil, true, false, "") do |results|
|
17
|
+
p [results.error_code, results.translation, results.query]
|
18
|
+
pp results.results
|
19
|
+
puts "======================================="
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
=begin
|
25
|
+
|
26
|
+
[root@vkill-arch youdao_fanyi]# ruby examples/translate.rb
|
27
|
+
中国
|
28
|
+
=======================================
|
29
|
+
[0, "China", "中国"]
|
30
|
+
{"translation"=>["China"],
|
31
|
+
"basic"=>{"explains"=>["China"]},
|
32
|
+
"query"=>"中国",
|
33
|
+
"errorCode"=>0,
|
34
|
+
"web"=>
|
35
|
+
[{"value"=>["china", "C.h.i.n.a", "cn", "Chung Kuo, Cina"], "key"=>"中国"},
|
36
|
+
{"value"=>["中国"], "key"=>"中國"},
|
37
|
+
{"value"=>["China Daily", "China News", "Dhina Daily", "Chellona Daily"],
|
38
|
+
"key"=>"中国日报"},
|
39
|
+
{"value"=>
|
40
|
+
["Great Wall, China",
|
41
|
+
"Great all, Chna",
|
42
|
+
"the Great Wall of China",
|
43
|
+
"GreatWallChina"],
|
44
|
+
"key"=>"中国长城"},
|
45
|
+
{"value"=>["China Unicom", "CHU", "WCDMA", "Unicom"], "key"=>"中国联通"},
|
46
|
+
{"value"=>
|
47
|
+
["Red Cross Society of China",
|
48
|
+
"RCSC",
|
49
|
+
"Red Cro Society of China",
|
50
|
+
"Chinese Red Cross Society"],
|
51
|
+
"key"=>"中国红十字会"},
|
52
|
+
{"value"=>
|
53
|
+
["Mainland China", "cn", "the Chinese mainland", "China Mainland"],
|
54
|
+
"key"=>"中国大陆"},
|
55
|
+
{"value"=>["Chinaren", "Chinese people", "The Chinese", "Chinese person"],
|
56
|
+
"key"=>"中国人"},
|
57
|
+
{"value"=>
|
58
|
+
["Chinese Literature", "Chinese play", "CHI.LIT", "Chinese novelist"],
|
59
|
+
"key"=>"中国文学"},
|
60
|
+
{"value"=>["China News", "Chugoku Shimbun", "CCTV NEWS", "News of China"],
|
61
|
+
"key"=>"中国新闻"}]}
|
62
|
+
=======================================
|
63
|
+
[0, "中国", "china"]
|
64
|
+
{"translation"=>["中国"],
|
65
|
+
"basic"=>{"phonetic"=>"'tʃainə", "explains"=>["n. 瓷器", "adj. 瓷制的"]},
|
66
|
+
"query"=>"china",
|
67
|
+
"errorCode"=>0,
|
68
|
+
"web"=>
|
69
|
+
[{"value"=>["中国", "国际集成电路研讨会暨展览会", "安世亚太", "中华"], "key"=>"China"},
|
70
|
+
{"value"=>["中国日报", "中国日报社", "中国日报英文版", "中国报"], "key"=>"China Daily"},
|
71
|
+
{"value"=>["中国移动", "中国移动通信公司", "中国移动通信", "神州移动"], "key"=>"China Mobile"},
|
72
|
+
{"value"=>["周年", "瓷婚", "搪瓷婚", "瓷器婚"], "key"=>"China Wedding"},
|
73
|
+
{"value"=>["月季", "月季花", "中国玫瑰", "扶桑"], "key"=>"China rose"},
|
74
|
+
{"value"=>["中国新闻", "中国日报", "新闻", "中国"], "key"=>"China News"},
|
75
|
+
{"value"=>["中国报道", "中国述评"], "key"=>"China Report"},
|
76
|
+
{"value"=>["中国网通", "神州网通", "中国网通集团", "中国网通集团公司"], "key"=>"China Netcom"},
|
77
|
+
{"value"=>["中国各地", "各地"], "key"=>"Around China"},
|
78
|
+
{"value"=>["美丽中国", "锦绣中华", "野性中国", "纪录片"], "key"=>"Wild China"}]}
|
79
|
+
=======================================
|
80
|
+
[0, "我", "i"]
|
81
|
+
{"translation"=>["我"],
|
82
|
+
"basic"=>{"phonetic"=>"ai", "explains"=>["pron. 我", "n. 碘元素;英语字母I"]},
|
83
|
+
"query"=>"i",
|
84
|
+
"errorCode"=>0,
|
85
|
+
"web"=>
|
86
|
+
[{"value"=>["输入", "保单险", "所得税", "I"], "key"=>"I"},
|
87
|
+
{"value"=>["我明白", "我明白了", "我知道", "知道了"], "key"=>"I see"},
|
88
|
+
{"value"=>["工字梁", "工形梁"], "key"=>"I beam"},
|
89
|
+
{"value"=>["位输入", "输出恢复时间"], "key"=>"bit I"},
|
90
|
+
{"value"=>["有里知花", "我哭泣", "罗百吉", "我哭了"], "key"=>"i cry"},
|
91
|
+
{"value"=>["伊丽莎白一世", "伊莉莎白一世", "伊莉莎白女王", "伊利莎白一世"], "key"=>"Elizabeth I"},
|
92
|
+
{"value"=>["张力尹", "我愿意", "张力尹星愿", "我将要"], "key"=>"I Will"},
|
93
|
+
{"value"=>["学习精要", "模拟试卷及详解考试", "第一级", "考试"], "key"=>"Level I"},
|
94
|
+
{"value"=>["Samsung", "Samsung I8000 Omnia II", "Anycall"], "key"=>"三星I"},
|
95
|
+
{"value"=>["我认为", "我思故我在", "我想", "双子座"], "key"=>"I think"}]}
|
96
|
+
=======================================
|
97
|
+
[0, "零", "nil"]
|
98
|
+
{"translation"=>["零"],
|
99
|
+
"basic"=>{"phonetic"=>"nil", "explains"=>["n. 无,零"]},
|
100
|
+
"query"=>"nil",
|
101
|
+
"errorCode"=>0,
|
102
|
+
"web"=>
|
103
|
+
[{"value"=>["无", "近等基因系", "零点", "尼尔"], "key"=>"nil"},
|
104
|
+
{"value"=>["牵牛花", "大花牵牛"], "key"=>"Pharbitis nil"},
|
105
|
+
{"value"=>["幂零根基"], "key"=>"nil radical"},
|
106
|
+
{"value"=>["零耦合"], "key"=>"Nil Coupling"},
|
107
|
+
{"value"=>["给自己", "给本身", "给本人"], "key"=>"guoL niL"},
|
108
|
+
{"value"=>["非零溶解度"], "key"=>"nil solubility"},
|
109
|
+
{"value"=>["吉理曼"], "key"=>"Nil Guillemette"},
|
110
|
+
{"value"=>["尼尔酒店"], "key"=>"nil hotel"},
|
111
|
+
{"value"=>["硬摇滚"], "key"=>"T nil"},
|
112
|
+
{"value"=>["无货"], "key"=>"Cargo NIL"}]}
|
113
|
+
=======================================
|
114
|
+
[0, "真正的", "true"]
|
115
|
+
{"translation"=>["真正的"],
|
116
|
+
"basic"=>
|
117
|
+
{"phonetic"=>"tru:",
|
118
|
+
"explains"=>["adj. 真实的;正确的", "adv. 真实地;准确地", "n. 真实;准确", "vt. 装准"]},
|
119
|
+
"query"=>"true",
|
120
|
+
"errorCode"=>0,
|
121
|
+
"web"=>
|
122
|
+
[{"value"=>["真", "真实的", "校准", "真正的"], "key"=>"True"},
|
123
|
+
{"value"=>["真爱如血", "噬血真爱", "嗜血真爱", "第一季"], "key"=>"True Blood"},
|
124
|
+
{"value"=>["真地平", "真地平线", "真水平线", "真实水平"], "key"=>"true horizon"},
|
125
|
+
{"value"=>["真正的美", "真实之美", "真善美", "美丽审判"], "key"=>"True Beauty"},
|
126
|
+
{"value"=>["有效", "适用"], "key"=>"hold true"},
|
127
|
+
{"value"=>["真北", "正北", "真实北国", "真实北向"], "key"=>"TRUE NORTH"},
|
128
|
+
{"value"=>["苏乞儿"], "key"=>"True Legend"},
|
129
|
+
{"value"=>["位置度", "真位", "真船位", "真正位置的孔"], "key"=>"true position"},
|
130
|
+
{"value"=>["本来面目", "本色", "真向", "英雄正传"], "key"=>"true colours"},
|
131
|
+
{"value"=>["真实租赁", "正式租赁", "真事租赁"], "key"=>"true lease"}]}
|
132
|
+
=======================================
|
133
|
+
[0, "假", "false"]
|
134
|
+
{"translation"=>["假"],
|
135
|
+
"basic"=>{"phonetic"=>"fɔ:ls", "explains"=>["adj. 错误的;虚伪的;伪造的", "adv. 欺诈地"]},
|
136
|
+
"query"=>"false",
|
137
|
+
"errorCode"=>0,
|
138
|
+
"web"=>
|
139
|
+
[{"value"=>["错误", "假的", "虚假的", "不成立"], "key"=>"False"},
|
140
|
+
{"value"=>["假阴性", "伪阴性", "负误识", "错误否定"], "key"=>"False negative"},
|
141
|
+
{"value"=>["假阳性", "伪阳性", "正误识", "错误肯定"], "key"=>"False positive"},
|
142
|
+
{"value"=>["假市", "虚假市场"], "key"=>"false market"},
|
143
|
+
{"value"=>["假睫毛"], "key"=>"false eyelash"},
|
144
|
+
{"value"=>["骗人的外表", "假面具", "装饰正面", "假锋"], "key"=>"false front"},
|
145
|
+
{"value"=>["一种形式的试题"], "key"=>"false Exam"},
|
146
|
+
{"value"=>["假紫晶"], "key"=>"false amethyst"},
|
147
|
+
{"value"=>["假年轮", "伪年轮"], "key"=>"false ring"},
|
148
|
+
{"value"=>["假天花板", "假平顶", "假顶棚", "假天华板"], "key"=>"false ceiling"}]}
|
149
|
+
=======================================
|
150
|
+
[0, "零", "nil"]
|
151
|
+
{"translation"=>["零"],
|
152
|
+
"basic"=>{"phonetic"=>"nil", "explains"=>["n. 无,零"]},
|
153
|
+
"query"=>"nil",
|
154
|
+
"errorCode"=>0,
|
155
|
+
"web"=>
|
156
|
+
[{"value"=>["无", "近等基因系", "零点", "尼尔"], "key"=>"nil"},
|
157
|
+
{"value"=>["牵牛花", "大花牵牛"], "key"=>"Pharbitis nil"},
|
158
|
+
{"value"=>["幂零根基"], "key"=>"nil radical"},
|
159
|
+
{"value"=>["零耦合"], "key"=>"Nil Coupling"},
|
160
|
+
{"value"=>["给自己", "给本身", "给本人"], "key"=>"guoL niL"},
|
161
|
+
{"value"=>["非零溶解度"], "key"=>"nil solubility"},
|
162
|
+
{"value"=>["吉理曼"], "key"=>"Nil Guillemette"},
|
163
|
+
{"value"=>["尼尔酒店"], "key"=>"nil hotel"},
|
164
|
+
{"value"=>["硬摇滚"], "key"=>"T nil"},
|
165
|
+
{"value"=>["无货"], "key"=>"Cargo NIL"}]}
|
166
|
+
=======================================
|
167
|
+
|
168
|
+
|
169
|
+
=end
|
170
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class YoudaoFanyi::InstallGenerator < Rails::Generators::Base
|
2
|
+
source_root File.expand_path('../templates', __FILE__)
|
3
|
+
|
4
|
+
def create_yml_file
|
5
|
+
template "youdao_fanyi_api_keys.yml", 'config/youdao_fanyi_api_keys.yml'
|
6
|
+
end
|
7
|
+
|
8
|
+
def create_initializer_file
|
9
|
+
template "youdao_fanyi.rb", 'config/initializers/youdao_fanyi.rb'
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
end
|
14
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
# YoudaoFanyi::Configuration.http_proxy = 'http://127.0.0.1:8118'
|
4
|
+
|
5
|
+
module YoudaoFanyi
|
6
|
+
Configuration = Struct.new(
|
7
|
+
:api_keys,
|
8
|
+
:http_proxy
|
9
|
+
).new(
|
10
|
+
[{ "keyfrom" => "test.vkill.net", "key" => "1234567890" }],
|
11
|
+
nil
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
module YoudaoFanyi
|
4
|
+
class Connector
|
5
|
+
|
6
|
+
attr_accessor :key, :keyfrom
|
7
|
+
attr_reader :q, :results, :response, :error_code, :translation, :query
|
8
|
+
|
9
|
+
include HTTParty
|
10
|
+
|
11
|
+
base_uri 'http://fanyi.youdao.com/fanyiapi.do'
|
12
|
+
default_params :type => :data, :doctype => :json, :version => 1.1
|
13
|
+
|
14
|
+
# YoudaoFanyi::Configuration.http_proxy = 'http://127.0.0.1:8118'
|
15
|
+
#or
|
16
|
+
# > export http_proxy=http://localhost:8118
|
17
|
+
if proxy = YoudaoFanyi::Configuration.http_proxy || ENV['http_proxy']
|
18
|
+
proxy = URI.parse(proxy)
|
19
|
+
http_proxy proxy.host, proxy.port
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
api_key = YoudaoFanyi.api_key()
|
24
|
+
@key = api_key["key"] || api_key[:key]
|
25
|
+
@keyfrom = api_key["keyfrom"] || api_key[:keyfrom]
|
26
|
+
end
|
27
|
+
|
28
|
+
def request(word, *options)
|
29
|
+
options = options.extract_options!
|
30
|
+
@q = word.to_s
|
31
|
+
@response = self.class.get('', :query => {:q => word}.merge(:key => @key, :keyfrom => @keyfrom))
|
32
|
+
# require "pry"
|
33
|
+
# binding.pry
|
34
|
+
@results = JSON.load(@response.body)
|
35
|
+
@error_code, @query = @results['errorCode'], @results['query']
|
36
|
+
@translation = @error_code == 0 ? @results['translation'].first : YoudaoFanyi::Errors.error_message(@error_code)
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
module YoudaoFanyi
|
4
|
+
class Translator
|
5
|
+
attr_reader :queries
|
6
|
+
|
7
|
+
def initialize(*options)
|
8
|
+
options.extract_options!.each {|k,v| instance_variable_set :"@#{k.to_s}", v}
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.translate(*words, &block)
|
12
|
+
options = words.extract_options!
|
13
|
+
not_translate = options.delete(:not_translate) || false
|
14
|
+
words = format_words(words)
|
15
|
+
if not_translate
|
16
|
+
self.new(:queries => words)
|
17
|
+
else
|
18
|
+
words.map do |word|
|
19
|
+
connector = YoudaoFanyi::Connector.new.request(word)
|
20
|
+
block ? (yield connector) : connector
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.t(*words)
|
26
|
+
translate(*words) {|connector| connector.translation}
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def self.format_words(words)
|
31
|
+
words.map do |word|
|
32
|
+
case word
|
33
|
+
when nil, ""
|
34
|
+
'nil'
|
35
|
+
else
|
36
|
+
word.to_s
|
37
|
+
end
|
38
|
+
end.keep_if {|x| !x.to_s.empty?}
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
data/lib/youdao_fanyi.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
$KCODE = 'u' unless RUBY_VERSION >= "1.9"
|
3
|
+
|
4
|
+
require "youdao_fanyi/version"
|
5
|
+
|
6
|
+
require "active_support/core_ext/object"
|
7
|
+
require "yaml"
|
8
|
+
require "httparty"
|
9
|
+
require "json"
|
10
|
+
require "uri"
|
11
|
+
#require "cgi"
|
12
|
+
|
13
|
+
require "youdao_fanyi/errors"
|
14
|
+
require "youdao_fanyi/configuration"
|
15
|
+
require "youdao_fanyi/connector"
|
16
|
+
require "youdao_fanyi/translator"
|
17
|
+
|
18
|
+
module YoudaoFanyi
|
19
|
+
def self.configure
|
20
|
+
yield Configuration
|
21
|
+
reload!
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.reload!
|
25
|
+
load "youdao_fanyi/connector.rb"
|
26
|
+
load "youdao_fanyi/translator.rb"
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.api_key
|
30
|
+
YoudaoFanyi::Configuration.api_keys.sort_by!{rand()}.first
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.t(*words)
|
34
|
+
YoudaoFanyi::Translator.t(*words)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
|
+
|
4
|
+
require File.expand_path('../config/application', __FILE__)
|
5
|
+
require 'rake'
|
6
|
+
|
7
|
+
Rails30App::Application.load_tasks
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require 'rails/all'
|
4
|
+
|
5
|
+
# If you have a Gemfile, require the gems listed there, including any gems
|
6
|
+
# you've limited to :test, :development, or :production.
|
7
|
+
Bundler.require(:default, Rails.env) if defined?(Bundler)
|
8
|
+
|
9
|
+
module Rails30App
|
10
|
+
class Application < Rails::Application
|
11
|
+
# Settings in config/environments/* take precedence over those specified here.
|
12
|
+
# Application configuration should go into files in config/initializers
|
13
|
+
# -- all .rb files in that directory are automatically loaded.
|
14
|
+
|
15
|
+
# Custom directories with classes and modules you want to be autoloadable.
|
16
|
+
# config.autoload_paths += %W(#{config.root}/extras)
|
17
|
+
|
18
|
+
# Only load the plugins named here, in the order given (default is alphabetical).
|
19
|
+
# :all can be used as a placeholder for all plugins not explicitly named.
|
20
|
+
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
21
|
+
|
22
|
+
# Activate observers that should always be running.
|
23
|
+
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
24
|
+
|
25
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
26
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
27
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
28
|
+
|
29
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
30
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
31
|
+
# config.i18n.default_locale = :de
|
32
|
+
|
33
|
+
# JavaScript files you want as :defaults (application.js is always included).
|
34
|
+
config.action_view.javascript_expansions[:defaults] = %w()
|
35
|
+
|
36
|
+
# Configure the default encoding used in templates for Ruby 1.9.
|
37
|
+
config.encoding = "utf-8"
|
38
|
+
|
39
|
+
# Configure sensitive parameters which will be filtered from the log file.
|
40
|
+
config.filter_parameters += [:password]
|
41
|
+
end
|
42
|
+
end
|