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

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

[elpa] externals/pyim 29a112182a 28/41: Refactor pyim-candidates.el


From: ELPA Syncer
Subject: [elpa] externals/pyim 29a112182a 28/41: Refactor pyim-candidates.el
Date: Sat, 4 Jun 2022 09:57:47 -0400 (EDT)

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

    Refactor pyim-candidates.el
---
 pyim-candidates.el  | 66 ++++++++++++++++++++++++++++++-----------------------
 tests/pyim-tests.el | 56 +++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 88 insertions(+), 34 deletions(-)

diff --git a/pyim-candidates.el b/pyim-candidates.el
index ecf2eb3356..53440ed15e 100644
--- a/pyim-candidates.el
+++ b/pyim-candidates.el
@@ -167,7 +167,7 @@
   "用于全拼输入法的 `pyim-candidates-create' 方法内部使用的函数。"
   (let* ((znabc-words (pyim-candidates-znabc-words imobjs scheme fast-search))
          (jianpin-words (pyim-candidates-jianpin-words imobjs scheme 
fast-search))
-         (dcache-words (pyim-candidates-dcache-words imobjs scheme 
fast-search))
+         (dcache-words (pyim-candidates-dcache-quanpin-words imobjs scheme 
fast-search))
          (personal-words (pyim-candidates-sort (nth 0 dcache-words)))
          (chief-word (pyim-candidates-get-chief scheme personal-words))
          (common-words (nth 1 dcache-words))
@@ -225,33 +225,20 @@
           (push (delete-dups (append w2 w1)) jianpin-words)))
       (pyim-zip (nreverse jianpin-words) fast-search))))
 
-(defun pyim-candidates-dcache-words (imobjs scheme &optional fast-search 
pymap-chars-num)
+(defun pyim-candidates-dcache-quanpin-words (imobjs scheme &optional 
fast-search pymap-chars-num)
   "从 dcache 获取个人词条,词库词条和第一汉字列表。"
   (let (personal-words common-words pinyin-chars-1 pinyin-chars-2)
     (dolist (imobj imobjs)
-      (let* (;; 个人词条
-             (w1 (pyim-dcache-get
-                  (string-join (pyim-codes-create imobj scheme) "-")
-                  (if pyim-enable-shortcode
-                      '(icode2word ishortcode2word)
-                    '(icode2word))))
-             ;; 词库词条
-             (w2 (pyim-dcache-get
-                  (string-join (pyim-codes-create imobj scheme) "-")
-                  (if pyim-enable-shortcode
-                      '(code2word shortcode2word)
-                    '(code2word))))
+      (let* ((w1 (pyim-candidates-quanpin-personal-words imobj scheme))
+             (w2 (pyim-candidates-quanpin-common-words imobj scheme))
              ;; 第一个汉字
-             (w3 (pyim-dcache-get
-                  (car (pyim-codes-create imobj scheme))))
+             (w3 (pyim-candidates-quanpin-chars imobj scheme))
              ;; 如果 w3 找不到第一个拼音对应的汉字,那就进一步使用
              ;; `pyim-pymap-py2cchar-get' 来查找,这个函数支持声母搜索。可以得到
              ;; 更多的词条。
              (w4 (unless w3
-                   (cl-subseq
-                    (pyim-candidates-pymap-chars
-                     (car (pyim-codes-create imobj scheme)))
-                    0 pymap-chars-num))))
+                   (pyim-candidates-pymap-chars
+                    (car (pyim-codes-create imobj scheme)) pymap-chars-num))))
         (push w1 personal-words)
         (push w2 common-words)
         (push w3 pinyin-chars-1)
@@ -262,15 +249,36 @@
     (setq pinyin-chars-2 (pyim-zip (nreverse pinyin-chars-2) fast-search))
     (list personal-words common-words pinyin-chars-1 pinyin-chars-2)))
 
-(defun pyim-candidates-pymap-chars (pinyin)
+(defun pyim-candidates-quanpin-personal-words (imobj scheme)
+  (pyim-dcache-get
+   (string-join (pyim-codes-create imobj scheme) "-")
+   (if pyim-enable-shortcode
+       '(icode2word ishortcode2word)
+     '(icode2word))))
+
+(defun pyim-candidates-quanpin-common-words (imobj scheme)
+  (pyim-dcache-get
+   (string-join (pyim-codes-create imobj scheme) "-")
+   (if pyim-enable-shortcode
+       '(code2word shortcode2word)
+     '(code2word))))
+
+(defun pyim-candidates-quanpin-chars (imobj scheme)
+  (pyim-dcache-get
+   (car (pyim-codes-create imobj scheme))
+   '(icode2word code2word)))
+
+(defun pyim-candidates-pymap-chars (pinyin &optional num)
   "从 pymap 表获取汉字。"
-  (mapcar #'char-to-string
-          (pyim-zip
-           (mapcar (lambda (x)
-                     ;; NOTE: 这里只取最常用的汉字,太多的汉字会带来后续处理压力,可能拖慢输入法。不过
-                     ;; 这个结论只是猜测。
-                     (car (split-string x "|")))
-                   (pyim-pymap-py2cchar-get pinyin)))))
+  (let ((chars (mapcar #'char-to-string
+                       (pyim-zip
+                        (mapcar (lambda (x)
+                                  ;; NOTE: 这里只取最常用的汉字,太多的汉字会带
+                                  ;; 来后续处理压力,可能拖慢输入法。不过这个结
+                                  ;; 论只是猜测。
+                                  (car (split-string x "|")))
+                                (pyim-pymap-py2cchar-get pinyin))))))
+    (cl-subseq chars 0 num)))
 
 (cl-defgeneric pyim-candidates-create-async (imobjs scheme)
   "按照 SCHEME, 使用异步的方式从 IMOBJS 获得候选词条。")
@@ -311,7 +319,7 @@
         (pyim-time-limit-while (and (not (input-pending-p)) ;如果用户继续输入,就停止 
buffer 搜索。
                                     (re-search-forward regexp nil t)) 
time-limit
           (let* ((match (match-string-no-properties 0))
-                 (word (propertize match :comment "(Buf)")))
+                 (word (propertize match :comment "(buf)")))
             ;; NOTE: 单个汉字我觉得不值得收集。
             (when (>= (length word) 2)
               (if (member word words)
diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el
index 879b4277ca..1a033f5f65 100644
--- a/tests/pyim-tests.el
+++ b/tests/pyim-tests.el
@@ -553,15 +553,61 @@
     (puthash "ni-hao" (list "你好" "尼耗" "呢耗") pyim-dhashcache-icode2word)
     (puthash "ni-hao" (list "你好" "尼耗") pyim-dhashcache-code2word)
     (puthash "n-h" (list "你好" "你坏" "尼耗" "南好" "内核" "内河") 
pyim-dhashcache-ishortcode2word)
-    (should (equal (pyim-candidates-dcache-words imobjs1 quanpin nil 10)
+    (should (equal (pyim-candidates-dcache-quanpin-words imobjs1 quanpin nil 
10)
                    '(("你" "您" "妮") nil nil ("南" "乃" "囊" "脑" "呢" "内" "嫩" "能" 
"你" "年"))))
-    (should (equal (pyim-candidates-dcache-words imobjs2 quanpin nil 10)
+    (should (equal (pyim-candidates-dcache-quanpin-words imobjs2 quanpin nil 
10)
                    '(("你" "尼") ("尼" "你") ("你" "尼" "尼" "你") nil)))
-    (should (equal (pyim-candidates-dcache-words imobjs3 quanpin nil 10)
+    (should (equal (pyim-candidates-dcache-quanpin-words imobjs3 quanpin nil 
10)
                    '(("你好" "尼耗" "呢耗") ("你好" "尼耗") ("你好" "尼耗" "呢耗" "你好" "尼耗") 
nil)))))
 
+(ert-deftest pyim-tests-pyim-candidates-quanpin-personal-words ()
+  (let* ((pyim-dhashcache-icode2word (make-hash-table :test #'equal))
+         (pyim-dhashcache-ishortcode2word (make-hash-table :test #'equal))
+         (quanpin (pyim-scheme-get 'quanpin))
+         (imobjs1 (pyim-imobjs-create "n" quanpin))
+         (imobjs2 (pyim-imobjs-create "ni" quanpin))
+         (imobjs3 (pyim-imobjs-create "nh" quanpin)))
+    (puthash "n" (list "你" "您" "妮") pyim-dhashcache-ishortcode2word)
+    (puthash "ni" (list "你" "尼") pyim-dhashcache-icode2word)
+    (puthash "n-h" (list "呢耗") pyim-dhashcache-icode2word)
+    (puthash "n-h" (list "你好" "你坏" "尼耗") pyim-dhashcache-ishortcode2word)
+    (should (equal (pyim-candidates-quanpin-personal-words (car imobjs1) 
quanpin)
+                   '("你" "您" "妮")))
+    (should (equal (pyim-candidates-quanpin-personal-words (car imobjs2) 
quanpin)
+                   '("你" "尼")))
+    (should (equal (pyim-candidates-quanpin-personal-words (car imobjs3) 
quanpin)
+                   '("呢耗" "你好" "你坏" "尼耗")))))
+
+(ert-deftest pyim-tests-pyim-candidates-quanpin-common-words ()
+  (let* ((pyim-dhashcache-code2word (make-hash-table :test #'equal))
+         (pyim-dhashcache-shortcode2word (make-hash-table :test #'equal))
+         (quanpin (pyim-scheme-get 'quanpin))
+         (imobjs1 (pyim-imobjs-create "n" quanpin))
+         (imobjs2 (pyim-imobjs-create "ni" quanpin))
+         (imobjs3 (pyim-imobjs-create "nh" quanpin)))
+    (puthash "n" (list "你" "您" "妮") pyim-dhashcache-shortcode2word)
+    (puthash "ni" (list "你" "尼") pyim-dhashcache-code2word)
+    (puthash "n-h" (list "呢耗") pyim-dhashcache-code2word)
+    (puthash "n-h" (list "你好" "你坏" "尼耗") pyim-dhashcache-shortcode2word)
+    (should (equal (pyim-candidates-quanpin-common-words (car imobjs1) quanpin)
+                   '("你" "您" "妮")))
+    (should (equal (pyim-candidates-quanpin-common-words (car imobjs2) quanpin)
+                   '("你" "尼")))
+    (should (equal (pyim-candidates-quanpin-common-words (car imobjs3) quanpin)
+                   '("呢耗" "你好" "你坏" "尼耗")))))
+
+(ert-deftest pyim-tests-pyim-candidates-quanpin-chars ()
+  (let* ((pyim-dhashcache-icode2word (make-hash-table :test #'equal))
+         (pyim-dhashcache-code2word (make-hash-table :test #'equal))
+         (quanpin (pyim-scheme-get 'quanpin))
+         (imobjs (pyim-imobjs-create "nihao" quanpin)))
+    (puthash "ni" (list "你" "呢") pyim-dhashcache-icode2word)
+    (puthash "ni" (list "你" "尼") pyim-dhashcache-code2word)
+    (should (equal (pyim-candidates-quanpin-chars (car imobjs) quanpin)
+                   '("你" "呢" "你" "尼")))))
+
 (ert-deftest pyim-tests-pyim-candidates-pymap-chars ()
-  (should (equal (cl-subseq (pyim-candidates-pymap-chars "ni") 0 10)
+  (should (equal (pyim-candidates-pymap-chars "ni" 10)
                  '("你" "年" "娘" "鸟" "摄" "您" "宁" "牛" "尼" "念"))))
 
 (ert-deftest pyim-tests-pyim-candidates-search-buffer ()
@@ -570,7 +616,7 @@
     (should (equal (pyim-candidates-search-buffer (pyim-cregexp-build "nh" 3 
t))
                    '("牛蛤" "你坏" "你好" "牛和" "你话")))
     (let ((words (pyim-candidates-search-buffer (pyim-cregexp-build "nh" 3 
t))))
-      (should (equal (get-text-property 0 :comment (car words)) "(Buf)")))))
+      (should (equal (get-text-property 0 :comment (car words)) "(buf)")))))
 
 (ert-deftest pyim-tests-pyim-candidates-cloud-search ()
   (should-not (pyim-candidates-cloud-search "a" t)))



reply via email to

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