diff --git a/.gitignore b/.gitignore index e69de29..461fa1f 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +lua/Trans/util/test/ diff --git a/lua/Trans/README.md b/lua/Trans/README.md index 8faec8a..6e02658 100644 --- a/lua/Trans/README.md +++ b/lua/Trans/README.md @@ -56,3 +56,52 @@ sentenceBold text 将查询内容加粗的例句 translation text 例句翻译 wfs text 单词形式变化 exam_type text 考试类型 + +## 百度 +from string 源语言 返回用户指定的语言,或者自动检测出的语种(源语言设为auto时) +to string 目标语言 返回用户指定的目标语言 +trans_result array 翻译结果 返回翻译结果,包括src和dst字段 +trans_result.*.src string 原文 接入举例中的“apple” +trans_result.*dst string 译文 接入举例中的“苹果” +error_code integer 错误码 仅当出现错误时显示 +以下字段仅开通了词典、tts用户可见 +src_tts string 原文tts链接 mp3格式,暂时无法指定发音 +dst_tts string 译文tts链接 mp3格式,暂时无法指定发音 +dict string 中英词典资源 返回中文或英文词典资源,包含音标;简明释义等内容 + +### 返回结果 +- 英-> 中 +```json +{ + "from": "en", + "to": "zh", + "trans_result": [ + { + "src": "apple", + "dst": "苹果" + } + ] +} +``` +- 中->英 +```json +{ + "from": "zh", + "to": "en", + "trans_result": [ + { + "src": "中国", + "dst": "China" + } + ] +} +``` +## 彩云小译 +句子翻译 +> sh xiaoyi.sh en2zh "You know some birds are not meant to be caged, their feathers are just too bright." +> 你知道有些鸟不应该被关在笼子里,它们的羽毛太亮了。 + +## 必应 + +## 腾讯翻译君 + diff --git a/lua/Trans/util/md5.lua b/lua/Trans/util/md5.lua new file mode 100644 index 0000000..4b3fdfb --- /dev/null +++ b/lua/Trans/util/md5.lua @@ -0,0 +1,431 @@ +local md5 = {} +-- local md5 = { +-- _VERSION = "md5.lua 1.1.0", +-- _DESCRIPTION = "MD5 computation in Lua (5.1-3, LuaJIT)", +-- _URL = "https://github.com/kikito/md5.lua", +-- _LICENSE = [[ +-- MIT LICENSE +-- +-- Copyright (c) 2013 Enrique García Cota + Adam Baldwin + hanzao + Equi 4 Software +-- +-- Permission is hereby granted, free of charge, to any person obtaining a +-- copy of this software and associated documentation files (the +-- "Software"), to deal in the Software without restriction, including +-- without limitation the rights to use, copy, modify, merge, publish, +-- distribute, sublicense, and/or sell copies of the Software, and to +-- permit persons to whom the Software is furnished to do so, subject to +-- the following conditions: +-- +-- The above copyright notice and this permission notice shall be included +-- in all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-- ]] +-- } + +-- bit lib implementions + +local char, byte, format, rep, sub = +string.char, string.byte, string.format, string.rep, string.sub +local bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift + +local ok, bit = pcall(require, 'bit') +local ok_ffi, ffi = pcall(require, 'ffi') +if ok then + bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift = bit.bor, bit.band, bit.bnot, bit.bxor, bit.rshift, + bit.lshift +else + ok, bit = pcall(require, 'bit32') + + if ok then + + bit_not = bit.bnot + + local tobit = function(n) + return n <= 0x7fffffff and n or -(bit_not(n) + 1) + end + + local normalize = function(f) + return function(a, b) return tobit(f(tobit(a), tobit(b))) end + end + + bit_or, bit_and, bit_xor = normalize(bit.bor), normalize(bit.band), normalize(bit.bxor) + bit_rshift, bit_lshift = normalize(bit.rshift), normalize(bit.lshift) + + else + + local function tbl2number(tbl) + local result = 0 + local power = 1 + for i = 1, #tbl do + result = result + tbl[i] * power + power = power * 2 + end + return result + end + + local function expand(t1, t2) + local big, small = t1, t2 + if (#big < #small) then + big, small = small, big + end + -- expand small + for i = #small + 1, #big do + small[i] = 0 + end + end + + local to_bits -- needs to be declared before bit_not + + bit_not = function(n) + local tbl = to_bits(n) + local size = math.max(#tbl, 32) + for i = 1, size do + if (tbl[i] == 1) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + return tbl2number(tbl) + end + + -- defined as local above + to_bits = function(n) + if (n < 0) then + -- negative + return to_bits(bit_not(math.abs(n)) + 1) + end + -- to bits table + local tbl = {} + local cnt = 1 + local last + while n > 0 do + last = n % 2 + tbl[cnt] = last + n = (n - last) / 2 + cnt = cnt + 1 + end + + return tbl + end + + bit_or = function(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + for i = 1, #tbl_m do + if (tbl_m[i] == 0 and tbl_n[i] == 0) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + + return tbl2number(tbl) + end + + bit_and = function(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + for i = 1, #tbl_m do + if (tbl_m[i] == 0 or tbl_n[i] == 0) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + + return tbl2number(tbl) + end + + bit_xor = function(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + for i = 1, #tbl_m do + if (tbl_m[i] ~= tbl_n[i]) then + tbl[i] = 1 + else + tbl[i] = 0 + end + end + + return tbl2number(tbl) + end + + bit_rshift = function(n, bits) + local high_bit = 0 + if (n < 0) then + -- negative + n = bit_not(math.abs(n)) + 1 + high_bit = 0x80000000 + end + + local floor = math.floor + + for i = 1, bits do + n = n / 2 + n = bit_or(floor(n), high_bit) + end + return floor(n) + end + + bit_lshift = function(n, bits) + if (n < 0) then + -- negative + n = bit_not(math.abs(n)) + 1 + end + + for i = 1, bits do + n = n * 2 + end + return bit_and(n, 0xFFFFFFFF) + end + end +end + +-- convert little-endian 32-bit int to a 4-char string +local lei2str +-- function is defined this way to allow full jit compilation (removing UCLO instruction in LuaJIT) +if ok_ffi then + local ct_IntType = ffi.typeof("int[1]") + lei2str = function(i) return ffi.string(ct_IntType(i), 4) end +else + lei2str = function(i) + local f = function(s) return char(bit_and(bit_rshift(i, s), 255)) end + return f(0) .. f(8) .. f(16) .. f(24) + end +end + + + +-- convert raw string to big-endian int +local function str2bei(s) + local v = 0 + for i = 1, #s do + v = v * 256 + byte(s, i) + end + return v +end + +-- convert raw string to little-endian int +local str2lei + +if ok_ffi then + local ct_constcharptr = ffi.typeof("const char*") + local ct_constintptr = ffi.typeof("const int*") + str2lei = function(s) + local int = ct_constcharptr(s) + return ffi.cast(ct_constintptr, int)[0] + end +else + str2lei = function(s) + local v = 0 + for i = #s, 1, -1 do + v = v * 256 + byte(s, i) + end + return v + end +end + + +-- cut up a string in little-endian ints of given size +local function cut_le_str(s) + return { + str2lei(sub(s, 1, 4)), + str2lei(sub(s, 5, 8)), + str2lei(sub(s, 9, 12)), + str2lei(sub(s, 13, 16)), + str2lei(sub(s, 17, 20)), + str2lei(sub(s, 21, 24)), + str2lei(sub(s, 25, 28)), + str2lei(sub(s, 29, 32)), + str2lei(sub(s, 33, 36)), + str2lei(sub(s, 37, 40)), + str2lei(sub(s, 41, 44)), + str2lei(sub(s, 45, 48)), + str2lei(sub(s, 49, 52)), + str2lei(sub(s, 53, 56)), + str2lei(sub(s, 57, 60)), + str2lei(sub(s, 61, 64)), + } +end + +-- An MD5 mplementation in Lua, requires bitlib (hacked to use LuaBit from above, ugh) +-- 10/02/2001 jcw@equi4.com + +local CONSTS = { + 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, + 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, + 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, + 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, + 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, + 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, + 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, + 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, + 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391, + 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 +} + +local f = function(x, y, z) return bit_or(bit_and(x, y), bit_and(-x - 1, z)) end +local g = function(x, y, z) return bit_or(bit_and(x, z), bit_and(y, -z - 1)) end +local h = function(x, y, z) return bit_xor(x, bit_xor(y, z)) end +local i = function(x, y, z) return bit_xor(y, bit_or(x, -z - 1)) end +local z = function(ff, a, b, c, d, x, s, ac) + a = bit_and(a + ff(b, c, d) + x + ac, 0xFFFFFFFF) + -- be *very* careful that left shift does not cause rounding! + return bit_or(bit_lshift(bit_and(a, bit_rshift(0xFFFFFFFF, s)), s), bit_rshift(a, 32 - s)) + b +end + +local function transform(A, B, C, D, X) + local a, b, c, d = A, B, C, D + local t = CONSTS + + a = z(f, a, b, c, d, X[0], 7, t[1]) + d = z(f, d, a, b, c, X[1], 12, t[2]) + c = z(f, c, d, a, b, X[2], 17, t[3]) + b = z(f, b, c, d, a, X[3], 22, t[4]) + a = z(f, a, b, c, d, X[4], 7, t[5]) + d = z(f, d, a, b, c, X[5], 12, t[6]) + c = z(f, c, d, a, b, X[6], 17, t[7]) + b = z(f, b, c, d, a, X[7], 22, t[8]) + a = z(f, a, b, c, d, X[8], 7, t[9]) + d = z(f, d, a, b, c, X[9], 12, t[10]) + c = z(f, c, d, a, b, X[10], 17, t[11]) + b = z(f, b, c, d, a, X[11], 22, t[12]) + a = z(f, a, b, c, d, X[12], 7, t[13]) + d = z(f, d, a, b, c, X[13], 12, t[14]) + c = z(f, c, d, a, b, X[14], 17, t[15]) + b = z(f, b, c, d, a, X[15], 22, t[16]) + + a = z(g, a, b, c, d, X[1], 5, t[17]) + d = z(g, d, a, b, c, X[6], 9, t[18]) + c = z(g, c, d, a, b, X[11], 14, t[19]) + b = z(g, b, c, d, a, X[0], 20, t[20]) + a = z(g, a, b, c, d, X[5], 5, t[21]) + d = z(g, d, a, b, c, X[10], 9, t[22]) + c = z(g, c, d, a, b, X[15], 14, t[23]) + b = z(g, b, c, d, a, X[4], 20, t[24]) + a = z(g, a, b, c, d, X[9], 5, t[25]) + d = z(g, d, a, b, c, X[14], 9, t[26]) + c = z(g, c, d, a, b, X[3], 14, t[27]) + b = z(g, b, c, d, a, X[8], 20, t[28]) + a = z(g, a, b, c, d, X[13], 5, t[29]) + d = z(g, d, a, b, c, X[2], 9, t[30]) + c = z(g, c, d, a, b, X[7], 14, t[31]) + b = z(g, b, c, d, a, X[12], 20, t[32]) + + a = z(h, a, b, c, d, X[5], 4, t[33]) + d = z(h, d, a, b, c, X[8], 11, t[34]) + c = z(h, c, d, a, b, X[11], 16, t[35]) + b = z(h, b, c, d, a, X[14], 23, t[36]) + a = z(h, a, b, c, d, X[1], 4, t[37]) + d = z(h, d, a, b, c, X[4], 11, t[38]) + c = z(h, c, d, a, b, X[7], 16, t[39]) + b = z(h, b, c, d, a, X[10], 23, t[40]) + a = z(h, a, b, c, d, X[13], 4, t[41]) + d = z(h, d, a, b, c, X[0], 11, t[42]) + c = z(h, c, d, a, b, X[3], 16, t[43]) + b = z(h, b, c, d, a, X[6], 23, t[44]) + a = z(h, a, b, c, d, X[9], 4, t[45]) + d = z(h, d, a, b, c, X[12], 11, t[46]) + c = z(h, c, d, a, b, X[15], 16, t[47]) + b = z(h, b, c, d, a, X[2], 23, t[48]) + + a = z(i, a, b, c, d, X[0], 6, t[49]) + d = z(i, d, a, b, c, X[7], 10, t[50]) + c = z(i, c, d, a, b, X[14], 15, t[51]) + b = z(i, b, c, d, a, X[5], 21, t[52]) + a = z(i, a, b, c, d, X[12], 6, t[53]) + d = z(i, d, a, b, c, X[3], 10, t[54]) + c = z(i, c, d, a, b, X[10], 15, t[55]) + b = z(i, b, c, d, a, X[1], 21, t[56]) + a = z(i, a, b, c, d, X[8], 6, t[57]) + d = z(i, d, a, b, c, X[15], 10, t[58]) + c = z(i, c, d, a, b, X[6], 15, t[59]) + b = z(i, b, c, d, a, X[13], 21, t[60]) + a = z(i, a, b, c, d, X[4], 6, t[61]) + d = z(i, d, a, b, c, X[11], 10, t[62]) + c = z(i, c, d, a, b, X[2], 15, t[63]) + b = z(i, b, c, d, a, X[9], 21, t[64]) + + return bit_and(A + a, 0xFFFFFFFF), bit_and(B + b, 0xFFFFFFFF), + bit_and(C + c, 0xFFFFFFFF), bit_and(D + d, 0xFFFFFFFF) +end + +---------------------------------------------------------------- + +local function md5_update(self, s) + self.pos = self.pos + #s + s = self.buf .. s + for ii = 1, #s - 63, 64 do + local X = cut_le_str(sub(s, ii, ii + 63)) + assert(#X == 16) + X[0] = table.remove(X, 1) -- zero based! + self.a, self.b, self.c, self.d = transform(self.a, self.b, self.c, self.d, X) + end + self.buf = sub(s, math.floor(#s / 64) * 64 + 1, #s) + return self +end + +local function md5_finish(self) + local msgLen = self.pos + local padLen = 56 - msgLen % 64 + + if msgLen % 64 > 56 then padLen = padLen + 64 end + + if padLen == 0 then padLen = 64 end + + local s = char(128) .. + rep(char(0), padLen - 1) .. lei2str(bit_and(8 * msgLen, 0xFFFFFFFF)) .. lei2str(math.floor(msgLen / 0x20000000)) + md5_update(self, s) + + assert(self.pos % 64 == 0) + return lei2str(self.a) .. lei2str(self.b) .. lei2str(self.c) .. lei2str(self.d) +end + +---------------------------------------------------------------- + +function md5.new() + return { a = CONSTS[65], b = CONSTS[66], c = CONSTS[67], d = CONSTS[68], + pos = 0, + buf = '', + update = md5_update, + finish = md5_finish } +end + +function md5.tohex(s) + return format("%08x%08x%08x%08x", str2bei(sub(s, 1, 4)), str2bei(sub(s, 5, 8)), str2bei(sub(s, 9, 12)), + str2bei(sub(s, 13, 16))) +end + +function md5.sum(s) + return md5.new():update(s):finish() +end + +function md5.sumhexa(s) + return md5.tohex(md5.sum(s)) +end + +return md5 diff --git a/lua/Trans/util/test/format.lua b/lua/Trans/util/test/format.lua deleted file mode 100644 index 12d799b..0000000 --- a/lua/Trans/util/test/format.lua +++ /dev/null @@ -1,188 +0,0 @@ - - --- local M = {} --- -- local type_check = require("Trans.util.debug").type_check --- --- --- -- NOTE :中文字符及占两个字节宽,但是在lua里是3个字节长度 --- -- 为了解决中文字符在lua的长度和neovim显示不一致的问题 --- function string:width() --- return vim.fn.strdisplaywidth(self) --- end --- --- -- 各种风格的基础宽度 --- local style_width = { --- -- float = require("Trans.conf.window").float.width, -- NOTE : need window parsed conf --- cursor = 60, --- } --- local s_to_b = true -- 从小到大排列 --- --- local m_fields -- 待格式化的字段 --- local m_indent -- 每行的行首缩进 --- local m_tot_width -- 所有字段加起来的长度(不包括缩进和间隔) --- local m_interval -- 每个字段的间隔 --- local m_win_width -- 需要被格式化窗口的高度 --- local m_item_width -- 每个字段的宽度 --- local m_size --- --- local function caculate_format() --- local width = m_win_width - m_item_width[1] --- local cols = 0 --- for i = 2, #m_fields do --- width = width - m_item_width[i] - m_interval --- if width < 0 then --- cols = i - 1 --- break --- else --- cols = i --- end --- end --- --- return math.ceil(#m_fields / cols), cols --- end --- --- local function format_to_line() --- local line = m_fields[1] --- if m_size == 1 then --- --- Center Align --- local space = math.floor((m_win_width - m_item_width[1]) / 2) --- line = (' '):rep(space) .. line --- else --- local space = math.floor((m_win_width - m_tot_width) / m_size - 1) --- for i = 2, m_size do --- line = line .. (' '):rep(space) .. m_fields[i] --- end --- end --- return line --- end --- --- --- local function sort_tables() --- table.sort(m_item_width, function (a, b) --- return a > b --- end) --- --- table.sort(m_fields, function (a, b) --- return a:width() > b:width() --- end) --- end --- --- --- local function format_to_multilines() --- local lines = {} --- sort_tables() --- --- --- NOTE : 计算应该格式化成多少行和列 --- local rows, cols = caculate_format() --- local rest = #m_fields % cols --- if rest == 0 then --- rest = cols --- end --- --- local s_width = m_item_width[1] -- 列中最宽的字符串宽度 --- -- NOTE : 第一列不需要加空格 --- for i = 1, rows do --- local idx = s_to_b and rows - i + 1 or i --- local space = (' '):rep(s_width - m_item_width[i]) --- lines[idx] = m_fields[i] .. space -- NOTE 由大到小 --- end --- --- local index = rows + 1 -- 最宽字符的下标 --- local interval = (' '):rep(m_interval) -- 每个字符串间的间隙 --- --- for j = 2, cols do -- 以列为单位遍历 --- s_width = m_item_width[index] --- local stop = (j > rest and rows - 1 or rows) --- for i = 1, stop do --- local idx = s_to_b and stop - i + 1 or i -- 当前操作的行数 --- local item = index + i - 1 -- 当前操作的字段数 --- local space = (' '):rep(s_width - m_item_width[item]) -- 对齐空格 --- --- lines[idx] = lines[idx] .. interval .. m_fields[item] .. space -- NOTE 从大到小 --- end --- index = index + stop -- 更新最宽字符的下标 --- end --- --- return lines --- end --- --- --- local function get_formatted_lines() --- local lines = {} --- -- NOTE : 判断能否格式化成一行 --- local line_size = m_tot_width + (#m_fields * m_interval) --- if line_size > m_win_width then --- lines = format_to_multilines() --- else --- lines[1] = format_to_line() --- end --- --- -- NOTE :进行缩进 --- if m_indent > 0 then --- for i, v in ipairs(lines) do --- lines[i] = (' '):rep(m_indent) .. v --- end --- end --- return lines --- end --- --- ---将组件格式化成相应的vim支持的lines格式 --- ---@param style string 窗口的风格 --- ---@param fields string[] 需要格式化的字段 --- ---@param indent? number 缩进的长度 --- ---@return string[] lines 便于vim.api.nvim_buf_set_lines --- M.to_lines = function(style, fields, indent) --- --- local length = 0 --- local width = 0 --- local item_size = {} --- for i, v in ipairs(fields) do --- width = v:width() --- item_size[i] = width --- length = length + width --- end --- --- m_indent = indent or 0 --- m_win_width = style_width[style] - m_indent --- m_fields = fields --- m_tot_width = length --- m_item_width = item_size --- m_interval = m_win_width > 50 and 6 or 4 --- m_size = #fields --- --- return get_formatted_lines() --- end --- --- local test = { --- 'isjlk测试dj', --- '测试一下..', --- } --- --- local lines = M.to_lines('cursor', test) --- --- -- print('===========================================') --- -- for _, v in ipairs(test) do --- -- print(v .. ' width:', v:width()) --- -- end --- -- print('===========================================') --- -- print('===========================================') --- -- print('===========================================') --- --- -- print('type is :' .. type(lines) .. ' size is :' .. #lines[1]) --- --- for _, v in ipairs(test) do --- print(v:width()) --- end --- --- -- lines = M.to_lines('cursor', { --- -- 'ajlkasj', --- -- 'jklasjldajjnn测试', --- -- '测试将安得拉蓝色', --- -- 'cool this', --- -- }, 4) --- --- -- for _, v in ipairs(lines) do --- -- print(v) --- -- end --- return M --- diff --git a/lua/Trans/util/test/is_Chinese.lua b/lua/Trans/util/test/is_Chinese.lua deleted file mode 100644 index cf15825..0000000 --- a/lua/Trans/util/test/is_Chinese.lua +++ /dev/null @@ -1,15 +0,0 @@ -local M = {} --- local type_check = require("Trans.util.debug").type_check - - ----@param str string -local function is_Chinese(str) - for i = 1, #str do - if not str:byte(i) >= [[\u4e00]] then - return false - end - end - return true -end - -return M diff --git a/lua/Trans/util/test/query_youdao.lua b/lua/Trans/util/test/query_youdao.lua deleted file mode 100644 index 8653e25..0000000 --- a/lua/Trans/util/test/query_youdao.lua +++ /dev/null @@ -1,71 +0,0 @@ -local M = {} --- local type_check = require("Trans.util.debug").type_check -local salt = '96836db9-1e28-4789-b5a6-fb7bb67e1259' -local appKey = '1858465a8708c121' -local appPasswd = 'fG0sitfk16nJOlIlycnLPYZn1optxUxL' - -local curtime -local word - -local function caculate_input() - local input - local len = #word - if len > 20 then - input = word:sub(1, 10) .. len .. word:sub(-10) - else - input = word - end - return input -end - -local function caculate_sign() - -- sign=sha256(应用ID+input+salt+curtime+应用密钥); - local hash = appKey .. caculate_input() .. salt .. curtime .. appPasswd - - return vim.fn.sha256(hash) -end - -local function test() - local query = { - q = word, - from = 'auto', - to = 'zh-CHS', - -- dicts = 'ec', - signType = 'v3', - appKey = appKey, - salt = salt, - curtime = curtime, - sign = caculate_sign(), - } - return query -end - --- curl --data {{'{"name":"bob"}'}} --header {{'Content-Type: application/json'}} {{http://example.com/users/1234}} - -local function query_word(q) - local ok, curl = pcall(require, 'plenary.curl') - if ok then - -- TODO - else - local field = ( - [[curl -s --header 'Content-Type: application/x-www-form-urlencoded' https://openapi.youdao.com/api]]) - - for k, v in pairs(q) do - field = field .. ([[ -d '%s=%s']]):format(k, v) - end - - local output = vim.fn.system(field) - local tb = vim.fn.json_decode(output) - return tb - end -end - -M.test = function(query) - curtime = tostring(os.time()) -- 更新一下time - word = query or 'as' - -- local json = vim.fn.json_encode(test()) - query_word(test()) - -- vim.pretty_print(vim.fn.json_encode(json)) -end - -return M diff --git a/lua/Trans/util/test/test.lua b/lua/Trans/util/test/test.lua deleted file mode 100644 index 8d5e8ff..0000000 --- a/lua/Trans/util/test/test.lua +++ /dev/null @@ -1,14 +0,0 @@ --- 记录开始时间 -local starttime = os.clock(); --> os.clock()用法 - -local str = 'test' -local len = #str - -for i = 1, 100000000 do - local size = len -end - --- 记录结束时间 -local endtime = os.clock(); --> os.clock()用法 -print(string.format("end time : %.4f", endtime)); -print(string.format("cost time : %.4f", endtime - starttime));