Class: Struggle::Translate
- Inherits:
-
Object
- Object
- Struggle::Translate
- Defined in:
- lib/struggle/translate.rb
Constant Summary collapse
- ICIBA_URL =
"http://dict-co.iciba.com/api/dictionary.php"
- ICIBA_KEY =
"36CC7638D81785E7006BB5B29DBA1FEF"
- BAIDU_URL =
"http://api.fanyi.baidu.com/api/trans/vip/translate"
- BAIDU_APPID =
"20180719000186861"
- BAIDU_KEY =
"QvOMjlxoc3xY1ComqF7P"
- YOUDAO_URL =
"http://fanyi.youdao.com/openapi.do"
- YOUDAO_KEY =
"29324956"
- YOUDAO_KEYFROM =
"wordtest"
- GOOGLE_URL =
"https://translate.google.com.hk/translate_a/t"
Class Method Summary collapse
-
.baidu(word) ⇒ Object
百度翻译,同样没有音标,只返回翻译结果 参数: word => 单词 返回: string,翻译的中文结果.
-
.google(word) ⇒ Object
google翻译,eval出错,不能转换成数组,因为有存在俩个逗号中没有值的情况,可以替换双逗号解决,但是没有音标 参数: word => 单词 返回: string,翻译的中文结果.
- .httpget(url) ⇒ Object
-
.iciba(word) ⇒ Object
获取爱词霸翻译结果 参数: word => 单词 返回: 成功返回hash, key:单词, ps:音标, pron:英语发音, pron2:美式发音, zn:翻译.
-
.youdao(word) ⇒ Object
有道翻译,只支持英文翻译 参数: word => 单词 返回: 成功返回hash, key:单词, ps:音标, zn:翻译;失败返回nil.
Class Method Details
.baidu(word) ⇒ Object
百度翻译,同样没有音标,只返回翻译结果 参数: word => 单词 返回: string,翻译的中文结果
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/struggle/translate.rb', line 87 def self.baidu(word) salt = Random.new.rand(10000..99999) #appid+q+salt+密钥 sign = Digest::MD5.new.hexdigest "#{self::BAIDU_APPID}#{word}#{salt}#{self::BAIDU_KEY}" begin url = "#{self::BAIDU_URL}?appid=#{self::BAIDU_APPID}&q=#{URI.escape(word)}&from=auto&to=auto&salt=#{salt}&sign=#{sign}" content = self.httpget(url) contenthash = eval content.gsub(":", "=>") return contenthash["trans_result"][0]["dst"] rescue return nil end end |
.google(word) ⇒ Object
google翻译,eval出错,不能转换成数组,因为有存在俩个逗号中没有值的情况,可以替换双逗号解决,但是没有音标 参数: word => 单词 返回: string,翻译的中文结果
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/struggle/translate.rb', line 70 def self.google(word) begin url = "#{self::GOOGLE_URL}?client=t&sl=en&tl=zh-CN&hl=zh-CN&sc=2&ie=UTF-8&oe=UTF-8&oc=1&otf=2&ssel=0&tsel=0&q=#{URI.escape(word)}" content = self.httpget(url) if content return eval(content.gsub(',,', ',').gsub(',,', ',').gsub(',,', ','))[0][0][0] #有没有值的,arr[0][0][0]是翻译,但是谷歌不支持音标,只支持翻译后的中文拼音 else return nil end rescue return nil end end |
.httpget(url) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/struggle/translate.rb', line 20 def self.httpget(url) content = "" open(url) do |http| content << http.read end return content end |
.iciba(word) ⇒ Object
获取爱词霸翻译结果 参数: word => 单词 返回: 成功返回hash, key:单词, ps:音标, pron:英语发音, pron2:美式发音, zn:翻译
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/struggle/translate.rb', line 31 def self.iciba(word) begin url = "#{self::ICIBA_URL}?w=#{URI.escape(word)}&key=#{self::ICIBA_KEY}" content = self.httpget(url) xml = REXML::Document.new(content) key = xml.get_elements("/dict/key")[0] if key && word == key.text #判断返回成功 psxml = xml.get_elements("/dict/ps")[0] #音标 ps = psxml.text if psxml pronxml = xml.get_elements("/dict/pron")[0] pron = pronxml.text if pronxml pron2xml = xml.get_elements("/dict/pron")[1] pron2 = pron2xml.text if pron2xml acceptations = xml.get_elements("/dict/acceptation") #中文翻译 poses = xml.get_elements("/dict/pos") #动词,形容词。。 zn = "" if poses && acceptations 1.upto(acceptations.count) do |i| zn = zn + poses[i - 1].text + acceptations[i - 1].text + "|" end if ps && zn return {key: key.text, ps: ps, pron: pron, pron2: pron2, zn: zn} else return nil end else return nil end else return nil end rescue return nil end end |
.youdao(word) ⇒ Object
有道翻译,只支持英文翻译 参数: word => 单词 返回: 成功返回hash, key:单词, ps:音标, zn:翻译;失败返回nil
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/struggle/translate.rb', line 104 def self.youdao(word) begin url = "#{self::YOUDAO_URL}?keyfrom=#{self::YOUDAO_KEYFROM}&key=#{self::YOUDAO_KEY}&type=data&doctype=json&version=1.1&q=#{URI.escape(word)}" content = self.httpget(url) contenthash = eval(content.gsub(":", "=>")) return {'key' => contenthash["query"], 'ps' => contenthash["basic"]["phonetic"], 'zn' => contenthash["basic"]["explains"].join("|")} rescue return nil end end |