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

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

[elpa] externals/pyim e6ff7bc: * pyim-cstring.el (pyim-cstring-to-codes)


From: ELPA Syncer
Subject: [elpa] externals/pyim e6ff7bc: * pyim-cstring.el (pyim-cstring-to-codes): Use pyim-string-dsitance.
Date: Wed, 12 May 2021 02:57:11 -0400 (EDT)

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

    * pyim-cstring.el (pyim-cstring-to-codes): Use pyim-string-dsitance.
---
 pyim-common.el  | 23 +++++++++++++++++++++++
 pyim-cstring.el |  9 ++++-----
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/pyim-common.el b/pyim-common.el
index 7df17fb..0ef39ba 100644
--- a/pyim-common.el
+++ b/pyim-common.el
@@ -140,6 +140,29 @@ better way is let exwm provide a test function.
 for example: https://github.com/ch11ng/exwm/pull/831";
   (string-match-p " \\*temp\\*" (buffer-name)))
 
+(if (fboundp 'string-distance)
+    (defalias 'pyim-string-distance 'string-distance)
+  (defun pyim-string-distance (s1 s2)
+    "Return the edit (levenshtein) distance between strings S1 S2."
+    (let* ((l1 (length s1))
+              (l2 (length s2))
+              (dist (vconcat (mapcar (lambda (_) (make-vector (1+ l2) nil))
+                                                 (number-sequence 1 (1+ l1)))))
+              (in (lambda (i j) (aref (aref dist i) j))))
+      (setf (aref (aref dist 0) 0) 0)
+      (dolist (j (number-sequence 1 l2))
+        (setf (aref (aref dist 0) j) j))
+      (dolist (i (number-sequence 1 l1))
+        (setf (aref (aref dist i) 0) i)
+        (dolist (j (number-sequence 1 l2))
+             (setf (aref (aref dist i) j)
+                   (min
+                    (1+ (funcall in (1- i) j))
+                    (1+ (funcall in i (1- j)))
+                    (+ (if (equal (aref s1 (1- i)) (aref s2 (1- j))) 0 1)
+                           (funcall in (1- i) (1- j)))))))
+      (funcall in l1 l2))))
+
 ;; * Footer
 (provide 'pyim-common)
 
diff --git a/pyim-cstring.el b/pyim-cstring.el
index 218cd6d..2d5a82c 100644
--- a/pyim-cstring.el
+++ b/pyim-cstring.el
@@ -28,6 +28,7 @@
 ;;; Code:
 ;; * 代码                                                           :code:
 (require 'cl-lib)
+(require 'pyim-common)
 
 (defgroup pyim-cstring nil
   "Chinese string tools for pyim."
@@ -374,17 +375,15 @@ CRITERIA 字符串一般是通过 imobjs 构建的,它保留了用户原始的
           ;;拼音使用了多音字校正
           (t (let ((codes (pyim-cstring-to-pinyin string nil "-" t nil t))
                    codes-sorted)
-               (if (or (< (length criteria) 1)
-                       ;; Emacs 27.1 include `string-distance'.
-                       (not (functionp 'string-distance)))
+               (if (< (length criteria) 1)
                    codes
                  ;; 将 所有 codes 与 criteria 字符串比对,选取相似度最高的一个
                  ;; code. 这种处理方式适合拼音输入法。
                  (setq codes-sorted
                        (sort codes
                              (lambda (a b)
-                               (< (string-distance a criteria)
-                                  (string-distance b criteria)))))
+                               (< (pyim-string-distance a criteria)
+                                  (pyim-string-distance b criteria)))))
                  (list (car codes-sorted))))))))
 
 ;; ** 获取光标处中文字符串或者中文词条的功能



reply via email to

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