emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/pyim 05d95422b9 07/12: cl-defgeneric rest dcache interf


From: ELPA Syncer
Subject: [elpa] externals/pyim 05d95422b9 07/12: cl-defgeneric rest dcache interfaces.
Date: Thu, 9 Jun 2022 10:57:53 -0400 (EDT)

branch: externals/pyim
commit 05d95422b9f4570c83d3aebd5ba480f5026223d2
Author: Feng Shu <tumashu@163.com>
Commit: Feng Shu <tumashu@163.com>

    cl-defgeneric rest dcache interfaces.
---
 pyim-candidates.el  |  2 +-
 pyim-cstring.el     |  2 +-
 pyim-dcache.el      | 40 ++++++++++++++++++++++------------------
 pyim-dhashcache.el  | 21 ++++++++++++---------
 pyim-dregcache.el   | 10 ++++++----
 pyim-process.el     |  5 +++--
 pyim.el             |  2 +-
 tests/pyim-tests.el |  7 ++++---
 8 files changed, 50 insertions(+), 39 deletions(-)

diff --git a/pyim-candidates.el b/pyim-candidates.el
index 3f035fddbe..38675707b6 100644
--- a/pyim-candidates.el
+++ b/pyim-candidates.el
@@ -46,7 +46,7 @@
 ;; ** 获取备选词列表
 (defun pyim-candidates-sort (candidates)
   "对 CANDIDATES 进行排序。"
-  (pyim-dcache-call-api 'sort-words candidates))
+  (pyim-dcache-sort-words candidates))
 
 (cl-defgeneric pyim-candidates-get-chief (scheme &optional personal-words 
common-words)
   "PYIM 输入法第一位候选词的获取策略。")
diff --git a/pyim-cstring.el b/pyim-cstring.el
index c90b78afec..7e9dbff6ba 100644
--- a/pyim-cstring.el
+++ b/pyim-cstring.el
@@ -182,7 +182,7 @@ BUG: 当 STRING 中包含其它标点符号,并且设置 SEPERATER 时,结
             (mapcar (lambda (x)
                       (when (string-prefix-p prefix x)
                         (string-remove-prefix prefix x)))
-                    (sort (cl-copy-list (pyim-dcache-call-api 
'search-word-code string))
+                    (sort (cl-copy-list (pyim-dcache-search-word-code string))
                           (lambda (a b)
                             (> (length a) (length b))))))
            (codes (remove nil dcache-codes)))
diff --git a/pyim-dcache.el b/pyim-dcache.el
index d6d072c2fe..e812d3972c 100644
--- a/pyim-dcache.el
+++ b/pyim-dcache.el
@@ -65,25 +65,14 @@ pyim 对资源的消耗。
 2. 自动更新功能无法正常工作,用户通过手工从其他机器上拷贝
 dcache 文件的方法让 pyim 正常工作。")
 
-;; ** Dcache API 调用功能
-(defun pyim-dcache-call-api (api-name &rest api-args)
-  "Get backend API named API-NAME then call it with arguments API-ARGS."
-  ;; make sure the backend is load
-  (unless (featurep pyim-dcache-backend)
-    (require pyim-dcache-backend))
-  (let ((func (intern (concat (symbol-name pyim-dcache-backend)
-                              "-" (symbol-name api-name)))))
-    (if (functionp func)
-        (apply func api-args)
-      (when pyim-debug
-        (message "%S 不是一个有效的 dcache api 函数." (symbol-name func))
-        ;; Need to return nil
-        nil))))
-
 ;; ** Dcache 变量处理相关功能
-(defun pyim-dcache-init-variables ()
+(cl-defgeneric pyim-dcache-init-variables ()
   "初始化 dcache 缓存相关变量."
-  (pyim-dcache-call-api 'init-variables))
+  nil)
+
+(cl-defmethod pyim-dcache-init-variables :before ()
+  (unless (featurep pyim-dcache-backend)
+    (require pyim-dcache-backend)))
 
 (defmacro pyim-dcache-init-variable (variable &optional fallback-value)
   "初始化 VARIABLE.
@@ -243,7 +232,22 @@ non-nil,文件存在时将会提示用户是否覆盖,默认为覆盖模式"
   "从 FROM 对应的 dcache 中搜索 CODE, 得到对应的词条.
 
 当词库文件加载完成后,pyim 就可以用这个函数从词库缓存中搜索某个
-code 对应的中文词条了.")
+code 对应的中文词条了."
+  ;; Fix compile warn
+  (ignore code from)
+  nil)
+
+(cl-defmethod pyim-dcache-get :before (_code &optional _from)
+  (unless (featurep pyim-dcache-backend)
+    (require pyim-dcache-backend)))
+
+(cl-defgeneric pyim-dcache-search-word-code (word)
+  "从 dcache 中搜索 WROD 对应的 code.")
+
+;; ** Dcache 排序功能
+(cl-defgeneric pyim-dcache-sort-words (words)
+  "对 WORDS 进行排序。"
+  words)
 
 ;; * Footer
 (provide 'pyim-dcache)
diff --git a/pyim-dhashcache.el b/pyim-dhashcache.el
index 7cd08f4181..fc8b43cf06 100644
--- a/pyim-dhashcache.el
+++ b/pyim-dhashcache.el
@@ -84,7 +84,7 @@
   "读取并加载所有相关词库 dcache.
 
 如果 FORCE 为真,强制加载。"
-  (pyim-dhashcache-init-variables)
+  (pyim-dcache-init-variables)
   (when pyim-dcache-auto-update
     (pyim-dhashcache-update-iword2priority force)
     (pyim-dhashcache-update-personal-words force)
@@ -92,7 +92,8 @@
            (dicts-md5 (pyim-dcache-create-files-md5 dict-files)))
       (pyim-dhashcache-update-code2word dict-files dicts-md5 force))))
 
-(defun pyim-dhashcache-sort-words (words-list)
+(cl-defmethod pyim-dcache-sort-words
+  (words-list &context (pyim-dcache-backend (eql pyim-dhashcache)))
   "对 WORDS-LIST 排序"
   (let ((iword2count pyim-dhashcache-iword2count)
         (iword2priority pyim-dhashcache-iword2priority))
@@ -227,7 +228,7 @@
      icode2word)
     (maphash
      (lambda (key value)
-       (puthash key (pyim-dhashcache-sort-words value)
+       (puthash key (pyim-dcache-sort-words value)
                 ishortcode2word))
      ishortcode2word)
     ishortcode2word))
@@ -276,7 +277,7 @@
      code2word)
     (maphash
      (lambda (key value)
-       (puthash key (pyim-dhashcache-sort-words value)
+       (puthash key (pyim-dcache-sort-words value)
                 shortcode2word))
      shortcode2word)
     shortcode2word))
@@ -444,7 +445,7 @@ code 对应的中文词条了。
         (pyim-dhashcache-init-count-and-priority-variables)
         (maphash
          (lambda (key value)
-           (puthash key (pyim-dhashcache-sort-words value)
+           (puthash key (pyim-dcache-sort-words value)
                     pyim-dhashcache-icode2word))
          pyim-dhashcache-icode2word)
         (pyim-dcache-save-variable
@@ -495,7 +496,8 @@ code 对应的中文词条了。
 (defun pyim-dhashcache-update-personal-words (&optional force)
   (pyim-dhashcache-update-icode2word force))
 
-(defun pyim-dhashcache-init-variables ()
+(cl-defmethod pyim-dcache-init-variables
+  (&context (pyim-dcache-backend (eql pyim-dhashcache)))
   "初始化 dcache 缓存相关变量."
   (when (and (not pyim-dhashcache-icode2word)
              pyim-dcache-directory
@@ -712,20 +714,21 @@ code 对应的中文词条了。
           `(,word ,@(remove word orig-value))
         `(,@(remove word orig-value) ,word)))))
 
-(defun pyim-dhashcache-search-word-code (string)
+(cl-defmethod pyim-dcache-search-word-code
+  (string &context (pyim-dcache-backend (eql pyim-dhashcache)))
   (gethash string pyim-dhashcache-word2code))
 
 (cl-defmethod pyim-dcache-export-personal-words
   (file &context (pyim-dcache-backend (eql pyim-dhashcache))
         &optional confirm)
   "导出个人词库到 FILE."
-  (pyim-dhashcache-init-variables)
+  (pyim-dcache-init-variables)
   (pyim-dhashcache-export pyim-dhashcache-icode2word file confirm))
 
 (cl-defmethod pyim-dcache-export-words-and-counts
   (file &context (pyim-dcache-backend (eql pyim-dhashcache))
         &optional confirm ignore-counts)
-  (pyim-dhashcache-init-variables)
+  (pyim-dcache-init-variables)
   (with-temp-buffer
     (insert ";;; -*- coding: utf-8-unix -*-\n")
     (maphash
diff --git a/pyim-dregcache.el b/pyim-dregcache.el
index 9197be914b..ef06687b8a 100644
--- a/pyim-dregcache.el
+++ b/pyim-dregcache.el
@@ -49,7 +49,7 @@
   "读取并加载所有相关词库 dcache.
 
 如果 FORCE 为真,强制加载。"
-  (pyim-dregcache-init-variables)
+  (pyim-dcache-init-variables)
   (when pyim-dcache-auto-update
     (pyim-dregcache-update-personal-words force)
     (let* ((dict-files (pyim-dict-get-enabled-dict-files))
@@ -331,7 +331,8 @@ DICT-FILES 是词库文件列表. DICTS-MD5 是词库的MD5校验码.
   (when (and force pyim-dregcache-icode2word)
     (pyim-dregcache-sort-icode2word)))
 
-(defun pyim-dregcache-init-variables ()
+(cl-defmethod pyim-dcache-init-variables
+  (&context (pyim-dcache-backend (eql pyim-dregcache)))
   "初始化 cache 缓存相关变量."
   (pyim-dcache-init-variable
    pyim-dregcache-iword2count
@@ -461,7 +462,8 @@ update-icode2word 目前只要是用于更新型码输入法的 code-prefix, 所
     (when (string-match regexp content)
       (match-string-no-properties 1 content))))
 
-(defun pyim-dregcache-search-word-code (word)
+(cl-defmethod pyim-dcache-search-word-code
+  (word &context (pyim-dcache-backend (eql pyim-dregcache)))
   "从 `pyim-dregcache-cache' 和 `pyim-dregcache-icode2word' 搜索 word, 得到对应的code."
   (when pyim-debug (message "pyim-dregcache-search-word-code word=%s" word))
   (when pyim-dregcache-cache
@@ -482,7 +484,7 @@ update-icode2word 目前只要是用于更新型码输入法的 code-prefix, 所
   (file &context (pyim-dcache-backend (eql pyim-dregcache))
         &optional confirm)
   "将个人词库存入 FILE."
-  (pyim-dregcache-init-variables)
+  (pyim-dcache-init-variables)
   (when pyim-dregcache-icode2word
     ;; 按词频排序,把词频信息保存到用户词典
     (pyim-dregcache-sort-icode2word)
diff --git a/pyim-process.el b/pyim-process.el
index a01efefa04..23345dba4d 100644
--- a/pyim-process.el
+++ b/pyim-process.el
@@ -205,6 +205,7 @@ imobj 组合构成在一起,构成了 imobjs 这个概念。比如:
   "PYIM 流程,词库相关的初始化工作。"
   (pyim-recreate-local-variables)
   (pyim-pymap-cache-create)
+  (pyim-dcache-init-variables)
   (pyim-dcache-update force))
 
 (defun pyim-process-save-dcaches (&optional force)
@@ -213,8 +214,8 @@ imobj 组合构成在一起,构成了 imobjs 这个概念。比如:
     (pyim-dcache-save-caches))
   t)
 
-(defun pyim-process-update-personal-words ()
-  (pyim-dcache-call-api 'update-personal-words t))
+(defun pyim-process-update (&optional force)
+  (pyim-dcache-update force))
 
 (defun pyim-process-start-daemon ()
   "启动 pyim 流程需要的 daemon."
diff --git a/pyim.el b/pyim.el
index 3b1bf7e2bd..bff910eaf2 100644
--- a/pyim.el
+++ b/pyim.el
@@ -430,7 +430,7 @@ MERGE-METHOD 是一个函数,这个函数需要两个数字参数,代表词
     ;; 有这一步骤,导入的词条就会被覆盖。
     (pyim-process-save-dcaches t)
     ;; 更新相关的 dcache
-    (pyim-process-update-personal-words)
+    (pyim-process-update t)
 
     (message "PYIM: 词条和词频信息导入完成!")))
 
diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el
index 336f6b2cb1..03b7b97b3e 100644
--- a/tests/pyim-tests.el
+++ b/tests/pyim-tests.el
@@ -1199,15 +1199,16 @@ yin-xing 因行
     (should (equal (gethash "n-h" pyim-dhashcache-ishortcode2word)
                    '("你慌" "你好" "你坏")))))
 
-(ert-deftest pyim-tests-pyim-dhashcache-sort-words ()
-  (let ((pyim-dhashcache-iword2count (make-hash-table :test #'equal))
+(ert-deftest pyim-tests-pyim-dcache-sort-words ()
+  (let ((pyim-dcache-backend 'pyim-dhashcache)
+        (pyim-dhashcache-iword2count (make-hash-table :test #'equal))
         words)
     (puthash "你好" 3 pyim-dhashcache-iword2count)
     (puthash "呢耗" 2 pyim-dhashcache-iword2count)
     (puthash "你豪" 1 pyim-dhashcache-iword2count)
 
     (setq words (list "呢耗" "你豪" "你好"))
-    (should (equal (pyim-dhashcache-sort-words words)
+    (should (equal (pyim-dcache-sort-words words)
                    '("你好" "呢耗" "你豪")))))
 
 (ert-deftest pyim-tests-pyim-dhashcache-get-counts-from-log ()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]