自 百度汉语 - Helm 搜索建议 继续讨论:
百度翻译 也有自动补全功能:
而且也有一个可用的 API:
~ $ curl -s https://fanyi.baidu.com/sug -d kw=impl | jq
{
"errno": 0,
"data": [
{
"k": "impl",
"v": "网络 实现类; 接口实现;"
},
{
"k": "implement",
"v": "v. 使生效; 贯彻; 执行; 实施; n. 工具; 器具; (常指)简单的户外用具;"
},
{
"k": "implication",
"v": "n. 可能的影响(或作用、结果); 含意; 暗指; (被)牵连,牵涉;"
},
{
"k": "imply",
"v": "v. 含有…的意思; 暗示; 暗指; 说明; 表明; 必然包含; 使有必要;"
},
{
"k": "implementation",
"v": "n. 执行,履行;实施,贯彻;生效;完成; 工具;仪器;供给器具;"
}
]
}
~ $
所以我也写一个 Helm 界面的,单词没记好或者只知道中文时,能派上些用场:
(require 'helm)
(defun helm-baidu-fanyi-suggest-fetch (keyword)
(let ((url-user-agent (format "%s <%s>" user-full-name user-mail-address))
(url-request-method "POST")
(url-request-extra-headers '(("Content-Type" . "application/x-www-form-urlencoded")))
(url-request-data (encode-coding-string
(mapconcat
(pcase-lambda (`(,key . ,val))
(concat (url-hexify-string key)
"="
(url-hexify-string val)))
(list (cons "kw" keyword))
"&")
'utf-8)))
(with-current-buffer (url-retrieve-synchronously "https://fanyi.baidu.com/sug")
;; 百度使用 \uxxxx,而不是 UTF-8
;; http://softwaremaniacs.org/blog/2015/03/22/json-encoding-problem/en/
;; (set-buffer-multibyte t)
(goto-char url-http-end-of-headers)
(let ((json (let ((json-array-type 'list))
(json-read))))
(mapcar
(lambda (x)
(let-alist x
(cons .k .v)))
(alist-get 'data json))))))
(defun helm-baidu-fanyi-suggest-candidates (&optional keyword)
(mapcar
(pcase-lambda (`(,word . ,meaning))
(format "%-20s %s" word
;; 有时开头会有空格
(string-trim meaning)))
(helm-baidu-fanyi-suggest-fetch (or keyword helm-pattern))))
(defvar helm-baidu-fanyi-suggest-action
(helm-make-actions
"Insert Query"
(lambda (candidate)
;; NOTE 单词和解释之间至少间隔 2 个空格
(insert (car (split-string candidate " " t))))
"Browse URL"
(lambda (candidate)
(let* ((query (car (split-string candidate " " t)))
;; NOTE 只考虑中文 ⇔ 英文
;; https://fanyi.baidu.com/#en/zh/aggressive
;; https://fanyi.baidu.com/#zh/en/%E4%B8%AD%E5%9B%BD
(from (if (string-match-p "\\cC" query) 'zh 'en))
(to (pcase from
('zh 'en)
('en 'zh))))
(browse-url
(format "https://fanyi.baidu.com/#%s/%s/%s"
from to (url-hexify-string query)))))))
(defun helm-baidu-fanyi-suggest ()
"百度翻译(搜索补全)."
(interactive)
(helm
:sources
(helm-build-sync-source "百度翻译"
:header-name
(lambda (name)
(format "%s <%s>" name "https://fanyi.baidu.com/"))
:candidates #'helm-baidu-fanyi-suggest-candidates
:action helm-baidu-fanyi-suggest-action
:volatile t
:requires-pattern 1)
:buffer "*helm 百度翻译*"))
相关推荐
@manateelazycat 用 Company + StarDict 的词典写过了一个类似的,优势在于本地词典速度快、可靠,缺点是字典质量不太高。