branch: externals/pyim commit 89f9c3518a80e80aa61b926accc387cc0dd915e1 Author: Feng Shu <tuma...@163.com> Commit: Feng Shu <tuma...@163.com>
Fix baidu cloudim. --- pyim-cloudim.el | 25 ++++++++++++++++++------- tests/pyim-tests.el | 8 ++++++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/pyim-cloudim.el b/pyim-cloudim.el index 098b3275aa..d39738798f 100644 --- a/pyim-cloudim.el +++ b/pyim-cloudim.el @@ -61,13 +61,24 @@ ;; NOTE: 以前这个函数使用 `json-parse-buffer' 来处理返回的结果,但因为旧版本 ;; Emacs 没有 `json-parse-buffer' 函数,所以现在改用这种简单粗暴的方式,虽然没 ;; 有使用 json 得到的结果精确,但应该适用于大多数情况,同时也减少了一个包依赖。 - (let ((word (replace-regexp-in-string - "\\CC" "" - (decode-coding-string - (buffer-string) - 'utf-8)))) - (when (> (length word) 0) - (list (propertize word :comment "(云)"))))) + (let ((words (pyim-cloudim--parse-baidu-buffer-string (buffer-string)))) + (when (> (length words) 0) + (mapcar (lambda (word) + (propertize word :comment "(云)")) + words)))) + +(defun pyim-cloudim--parse-baidu-buffer-string (string) + "从 baidu buffer STRING 中抓取中文词条,返回对应的词条列表." + (let ((string (decode-coding-string string 'utf-8)) + (sep "丨")) + (cl-remove-if-not + (lambda (x) + (> (length x) 0)) + (split-string + (replace-regexp-in-string + "\\CC" "" + (replace-regexp-in-string "," sep string)) + (format "[%s]+" sep))))) (cl-defmethod pyim-candidates-create-async (imobjs (scheme pyim-scheme-quanpin) callback diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el index 9fb3e98eb8..b0e0083123 100644 --- a/tests/pyim-tests.el +++ b/tests/pyim-tests.el @@ -1546,10 +1546,14 @@ Content-Length: 88 Content-Type: text/plain; charset=utf-8 Date: Sun, 08 May 2022 00:56:13 GMT -{\"0\":[[[\"你好\",5,{\"pinyin\":\"ni'hao\",\"type\":\"IMEDICT\"}]]],\"1\":\"ni'hao\",\"result\":[null]}") - (should (equal (pyim-cloudim--parse-baidu-buffer) '("你好"))) +{\"0\":[[[\"嘻嘻\",4,{\"pinyin\":\"xi'xi\",\"type\":\"IMEDICT\"}],[\"茜茜\",8,{\"pinyin\":\"qian'qian\",\"type\":\"IMEDICT\"}],[\"洗洗\",4,{\"pinyin\":\"xi'xi\",\"type\":\"IMEDICT\"}]]],\"1\":\"xi'xi\",\"result\":[null]}") + (should (equal (pyim-cloudim--parse-baidu-buffer) '("嘻嘻" "茜茜" "洗洗"))) (should (equal (get-text-property 0 :comment (car (pyim-cloudim--parse-baidu-buffer))) "(云)"))) + (should (equal (pyim-cloudim--parse-baidu-buffer-string + "{\"0\":[[[\"嘻嘻\",4,{\"pinyin\":\"xi'xi\",\"type\":\"IMEDICT\"}],[\"茜茜\",8,{\"pinyin\":\"qian'qian\",\"type\":\"IMEDICT\"}],[\"洗洗\",4,{\"pinyin\":\"xi'xi\",\"type\":\"IMEDICT\"}]]],\"1\":\"xi'xi\",\"result\":[null]}") + '("嘻嘻" "茜茜" "洗洗"))) + (with-temp-buffer (insert "HTTP/1.1 200 OK Date: Sun, 08 May 2022 03:33:56 GMT