百度翻译 - Helm 搜索补全

百度汉语 - Helm 搜索建议 继续讨论:

百度翻译 也有自动补全功能:

46

而且也有一个可用的 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 的词典写过了一个类似的,优势在于本地词典速度快、可靠,缺点是字典质量不太高。

6 个赞

不是有谷歌翻译吗,挺好用的啊,那个插件。而且百度翻译的那么烂,实在比不上谷歌翻译,尤其是文档,没有对比,就没有伤害

我们是在讨论的同一个功能吗?我提到的「搜索补全」指的是你一边输入一边自动给你建议,Google 翻译的插件有这个功能吗?(我印象中没有吧?)

看上去挺有意思 :)

ivy + request/curl版本的。

(require 'request)
(require 'ivy)


(defun ivy-baidu-fanyi-suggest-fetch (keyword)
  (let* ((json-array-type 'list)
         (fanyi (request
                 "https://fanyi.baidu.com/sug"
                 :params `(("kw" . ,keyword))
                 :parser 'json-read
                 :sync t))
         (data (request-response-data fanyi))
         (err (request-response-error-thrown fanyi))
         (status (request-response-status-code fanyi)))
    (unless err
      (mapcar
       (lambda (x)
         (let-alist x
           (cons .k .v)))
       (alist-get 'data data))
      )))

(defun ivy-baidu-fanyi-suggest-candidates (&optional keyword)
  (mapcar
   (pcase-lambda (`(,word . ,meaning))
     (format "%-20s -- %s" word
             ;; 有时开头会有空格
             (string-trim meaning)))
   (ivy-baidu-fanyi-suggest-fetch
    (or keyword
        (string-trim
         (if (use-region-p)
             (buffer-substring-no-properties (region-beginning) (region-end))
           (substring-no-properties (or (thing-at-point 'word) ""))
           ))))))

(defun ivy-baidu-fanyi-suggest ()
  "百度翻译(搜索补全)."
  (interactive)
  (let* ((str (if (use-region-p)
                  (buffer-substring-no-properties (region-beginning) (region-end))
                (substring-no-properties (or (thing-at-point 'word) ""))))
         beg end candidates initial-input)
    (when str
      (setq beg (if (use-region-p) (region-beginning) (- (point) (length str)))
            end (if (use-region-p) (region-end) (point))
            candidates (ivy-baidu-fanyi-suggest-candidates))
      (setq ivy-completion-beg beg
            ivy-completion-end end)
      (setq initial-input (string-trim (car (split-string (car candidates) " -- "))))
      (ivy-read "Baidu Fanyi to complete:"
                (ivy-baidu-fanyi-suggest-candidates)
                :initial-input initial-input
                :action (lambda (cand)
                          (ivy-completion-in-region-action (string-trim (car (split-string cand " -- ")))))
                :caller 'ivy-baidu-fanyi-suggest
                ))))

(ivy-set-actions
 'ivy-baidu-fanyi-suggest
 `(("l" ,(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)))))
    "Browse URL")))
4 个赞