branch: externals/pyim commit 5989a6c43c323db680254c6900091361c6e9ddd9 Author: Feng Shu <tuma...@163.com> Commit: Feng Shu <tuma...@163.com>
cl-defgeneric pyim-dcache-get. --- pyim-dcache.el | 6 ++---- pyim-dhashcache.el | 39 +++++++++++++++++++++------------------ pyim-dregcache.el | 45 ++++++++++++++++++++++++--------------------- tests/pyim-tests.el | 13 +++++++------ 4 files changed, 54 insertions(+), 49 deletions(-) diff --git a/pyim-dcache.el b/pyim-dcache.el index 79e0236bab..c0a2cba356 100644 --- a/pyim-dcache.el +++ b/pyim-dcache.el @@ -266,13 +266,11 @@ non-nil,文件存在时将会提示用户是否覆盖,默认为覆盖模式" (pyim-dcache-call-api 'delete-word word)) ;; ** Dcache 检索功能 -(defun pyim-dcache-get (code &optional from) +(cl-defgeneric pyim-dcache-get (code &optional from) "从 FROM 对应的 dcache 中搜索 CODE, 得到对应的词条. 当词库文件加载完成后,pyim 就可以用这个函数从词库缓存中搜索某个 -code 对应的中文词条了." - (when code - (pyim-dcache-call-api 'get code from))) +code 对应的中文词条了.") ;; * Footer (provide 'pyim-dcache) diff --git a/pyim-dhashcache.el b/pyim-dhashcache.el index db2b0a8ad6..c4e242cc64 100644 --- a/pyim-dhashcache.el +++ b/pyim-dhashcache.el @@ -396,7 +396,9 @@ DCACHE 是一个 code -> words 的 hashtable. dcache) (pyim-dcache-write-file file confirm))) -(defun pyim-dhashcache-get (code &optional from) +(cl-defmethod pyim-dcache-get + (code &context (pyim-dcache-backend (eql pyim-dhashcache)) + &optional from) "从 FROM 对应的 dcaches 中搜索 CODE, 得到对应的词条. 当词库文件加载完成后,pyim 就可以用这个函数从词库缓存中搜索某个 @@ -404,23 +406,24 @@ code 对应的中文词条了。 如果 FROM 为 nil, 则默认搜索 `pyim-dhashcache-icode2word' 和 `pyim-dhashcache-code2word' 两个 dcache." - (let* ((caches (mapcar (lambda (x) - (intern (concat "pyim-dhashcache-" (symbol-name x)))) - (or (and from - (if (listp from) - from - (list from))) - '(icode2word code2word)))) - result) - (dolist (cache caches) - (let* ((cache (ignore-errors (symbol-value cache))) - (value (and cache (gethash code cache)))) - ;; 处理 iword2count. - (unless (listp value) - (setq value (list value))) - (when value - (setq result (append result value))))) - result)) + (when code + (let* ((caches (mapcar (lambda (x) + (intern (concat "pyim-dhashcache-" (symbol-name x)))) + (or (and from + (if (listp from) + from + (list from))) + '(icode2word code2word)))) + result) + (dolist (cache caches) + (let* ((cache (ignore-errors (symbol-value cache))) + (value (and cache (gethash code cache)))) + ;; 处理 iword2count. + (unless (listp value) + (setq value (list value))) + (when value + (setq result (append result value))))) + result))) (defun pyim-dhashcache-update-icode2word (&optional force) "对 personal 缓存中的词条进行排序,加载排序后的结果. diff --git a/pyim-dregcache.el b/pyim-dregcache.el index 4686b074ed..909ae51584 100644 --- a/pyim-dregcache.el +++ b/pyim-dregcache.el @@ -261,28 +261,31 @@ DICT-FILES 是词库文件列表. DICTS-MD5 是词库的MD5校验码. (setq start (+ start 2 (length code) (length word)))) output)) -(defun pyim-dregcache-get (code &optional from) +(cl-defmethod pyim-dcache-get + (code &context (pyim-dcache-backend (eql pyim-dregcache)) + &optional from) "从 `pyim-dregcache-cache' 搜索 CODE, 得到对应的词条." - (cond ((or (memq 'icode2word from) - (memq 'ishortcode2word from)) - (pyim-dregcache-get-icode2word-ishortcode2word code)) - ;; FIXME: pyim-dregcache 暂时不支持 iword2count-recent-10-words 和 - ;; iword2count-recent-50-words. - ((or (memq 'iword2count-recent-10-words from) - (memq 'iword2count-recent-50-words from)) - nil) - (t (let ((dict-files (pyim-dregcache-all-dict-files)) - result) - - (when pyim-debug (message "pyim-dregcache-get is called. code=%s" code)) - (when dict-files - (dolist (file dict-files) - (let* ((file-info (lax-plist-get pyim-dregcache-cache file)) - (content (pyim-dregcache-get-content code file-info))) - (setq result (append (pyim-dregcache-get-1 content code) result))))) - ;; `push' plus `nreverse' is more efficient than `add-to-list' - ;; Many examples exist in Emacs' own code - (nreverse result))))) + (when code + (cond ((or (memq 'icode2word from) + (memq 'ishortcode2word from)) + (pyim-dregcache-get-icode2word-ishortcode2word code)) + ;; FIXME: pyim-dregcache 暂时不支持 iword2count-recent-10-words 和 + ;; iword2count-recent-50-words. + ((or (memq 'iword2count-recent-10-words from) + (memq 'iword2count-recent-50-words from)) + nil) + (t (let ((dict-files (pyim-dregcache-all-dict-files)) + result) + + (when pyim-debug (message "pyim-dregcache-get is called. code=%s" code)) + (when dict-files + (dolist (file dict-files) + (let* ((file-info (lax-plist-get pyim-dregcache-cache file)) + (content (pyim-dregcache-get-content code file-info))) + (setq result (append (pyim-dregcache-get-1 content code) result))))) + ;; `push' plus `nreverse' is more efficient than `add-to-list' + ;; Many examples exist in Emacs' own code + (nreverse result)))))) (defun pyim-dregcache-get-icode2word-ishortcode2word (code) "以 CODE 搜索个人词和个人联想词. 正则表达式搜索词库,不需要为联想词开单独缓存." diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el index 3b366d296b..10dc88391f 100644 --- a/tests/pyim-tests.el +++ b/tests/pyim-tests.el @@ -1166,7 +1166,8 @@ yin-xing 因行 "))))) (ert-deftest pyim-tests-pyim-dhashcache-get () - (let ((pyim-dhashcache-code2word (make-hash-table :test #'equal)) + (let ((pyim-dcache-backend 'pyim-dhashcache) + (pyim-dhashcache-code2word (make-hash-table :test #'equal)) (pyim-dhashcache-icode2word (make-hash-table :test #'equal)) (pyim-dhashcache-iword2count (make-hash-table :test #'equal))) @@ -1174,11 +1175,11 @@ yin-xing 因行 (puthash "ni-hao" '("你好") pyim-dhashcache-code2word) (puthash "你好" 10 pyim-dhashcache-iword2count) - (should (equal (pyim-dhashcache-get "ni-hao" '(code2word)) '("你好"))) - (should (equal (pyim-dhashcache-get "ni-hao" '(icode2word)) '("呢耗"))) - (should (equal (pyim-dhashcache-get "你好" '(iword2count)) '(10))) - (should (equal (pyim-dhashcache-get "ni-hao" '(code2word icode2word)) '("你好" "呢耗"))) - (should (equal (pyim-dhashcache-get "ni-hao") '("呢耗" "你好"))))) + (should (equal (pyim-dcache-get "ni-hao" '(code2word)) '("你好"))) + (should (equal (pyim-dcache-get "ni-hao" '(icode2word)) '("呢耗"))) + (should (equal (pyim-dcache-get "你好" '(iword2count)) '(10))) + (should (equal (pyim-dcache-get "ni-hao" '(code2word icode2word)) '("你好" "呢耗"))) + (should (equal (pyim-dcache-get "ni-hao") '("呢耗" "你好"))))) (ert-deftest pyim-tests-pyim-dhashcache-insert-word-into-icode2word () (let ((pyim-dhashcache-icode2word (make-hash-table :test #'equal)))