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

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

[elpa] externals/pyim 33a0f5a9b7 09/41: Add pyim-candidates-create-like-


From: ELPA Syncer
Subject: [elpa] externals/pyim 33a0f5a9b7 09/41: Add pyim-candidates-create-like-jianpin
Date: Sat, 4 Jun 2022 09:57:45 -0400 (EDT)

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

    Add pyim-candidates-create-like-jianpin
---
 pyim-candidates.el  | 59 ++++++++++++++++++++++++++++++-----------------------
 tests/pyim-tests.el | 11 ++++++++++
 2 files changed, 44 insertions(+), 26 deletions(-)

diff --git a/pyim-candidates.el b/pyim-candidates.el
index 84c40428da..89279f0afa 100644
--- a/pyim-candidates.el
+++ b/pyim-candidates.el
@@ -166,33 +166,9 @@
 (defun pyim-candidates-create-quanpin (imobjs scheme &optional fast-search)
   "`pyim-candidates-create' 内部使用的函数。"
   (let ((znabc-words (pyim-candidates-create-like-znabc imobjs scheme 
fast-search))
-        jianpin-words personal-words common-words
+        (jianpin-words (pyim-candidates-create-like-jianpin imobjs scheme))
+        personal-words common-words
         pinyin-chars-1 pinyin-chars-2 chief-word)
-    ;; 假如输入 "nih" ,那么搜索 code 为 "n-h" 的词条,然后筛选出所有拼音匹配
-    ;; "ni-h" 或者 "ni[^-]*-h" 的词条。
-    (when (and pyim-enable-shortcode
-               (> (length (car imobjs)) 1))
-      (dolist (imobj imobjs)
-        (let* ((w (pyim-dcache-get
-                   (string-join (pyim-codes-create imobj scheme 1) "-")
-                   '(ishortcode2word)))
-               (regexp1 (string-join
-                         (pyim-codes-create imobj scheme)
-                         "-"))
-               (regexp2 (string-join
-                         (pyim-codes-create imobj scheme)
-                         "[^-]*-"))
-               (w1 (cl-remove-if-not
-                    (lambda (cstr)
-                      (let ((py (pyim-cstring-to-pinyin cstr nil "-")))
-                        (or (string-match-p regexp1 py)
-                            (string-match-p regexp2 py))))
-                    w))
-               (w2 (cl-remove-if-not
-                    (lambda (cstr)
-                      (string-match-p regexp1 (pyim-cstring-to-pinyin cstr nil 
"-")))
-                    w1)))
-          (push (append w2 w1) jianpin-words))))
 
     ;; 获取个人词条,词库词条和第一汉字列表。
     (dolist (imobj imobjs)
@@ -277,6 +253,37 @@
                       (pyim-zip codes))
               fast-search)))
 
+(defun pyim-candidates-create-like-jianpin (imobjs scheme)
+  "简拼模式。
+
+ 假如输入 \"nih\" ,那么搜索 code 为 \"n-h\" 的词条,然后筛选出所
+ 有拼音匹配\"ni-h\" 或者 \"ni[^-]*-h\" 的词条。"
+  (when (and pyim-enable-shortcode
+             (> (length (car imobjs)) 1))
+    (let (jianpin-words)
+      (dolist (imobj imobjs)
+        (let* ((w (pyim-dcache-get
+                   (string-join (pyim-codes-create imobj scheme 1) "-")
+                   '(ishortcode2word)))
+               (regexp1 (string-join
+                         (pyim-codes-create imobj scheme)
+                         "-"))
+               (regexp2 (string-join
+                         (pyim-codes-create imobj scheme)
+                         "[^-]*-"))
+               (w1 (cl-remove-if-not
+                    (lambda (cstr)
+                      (let ((py (pyim-cstring-to-pinyin cstr nil "-")))
+                        (or (string-match-p regexp1 py)
+                            (string-match-p regexp2 py))))
+                    w))
+               (w2 (cl-remove-if-not
+                    (lambda (cstr)
+                      (string-match-p regexp1 (pyim-cstring-to-pinyin cstr nil 
"-")))
+                    w1)))
+          (push (delete-dups (append w2 w1)) jianpin-words)))
+      jianpin-words)))
+
 (cl-defmethod pyim-candidates-create (_imobjs (_scheme pyim-scheme-shuangpin))
   "按照 SCHEME, 从 IMOBJS 获得候选词条,用于双拼输入法。"
   (cl-call-next-method))
diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el
index 87d8101e21..cbc4a4bfda 100644
--- a/tests/pyim-tests.el
+++ b/tests/pyim-tests.el
@@ -526,6 +526,17 @@
     (should (equal (pyim-candidates-create-like-znabc imobjs quanpin t)
                    '("你好吗朋友" "你好吗" "你好")))))
 
+(ert-deftest pyim-tests-pyim-candidates-create-like-jianpin ()
+  (let* ((pyim-dhashcache-code2word (make-hash-table :test #'equal))
+         (pyim-dhashcache-icode2word (make-hash-table :test #'equal))
+         (quanpin (pyim-scheme-get 'quanpin))
+         (imobjs1 (pyim-imobjs-create "nih" quanpin))
+         (imobjs2 (pyim-imobjs-create "ni" quanpin)))
+    (puthash "n-h" (list "你好" "你坏" "尼耗" "南好" "内核" "内河") 
pyim-dhashcache-ishortcode2word)
+    (should (equal (pyim-candidates-create-like-jianpin imobjs1 quanpin)
+                   '(("你好" "你坏" "尼耗"))))
+    (should-not (pyim-candidates-create-like-jianpin imobjs2 quanpin))))
+
 (ert-deftest pyim-tests-pyim-candidates-search-buffer ()
   (with-temp-buffer
     (insert "你好你好你坏你坏你话牛蛤牛和牛蛤牛蛤牛蛤牛蛤牛蛤")



reply via email to

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