[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/pyim 02c5004: 简化 pyim-dhashcache-update-ishortcode2wor
From: |
ELPA Syncer |
Subject: |
[elpa] externals/pyim 02c5004: 简化 pyim-dhashcache-update-ishortcode2word-1 和 pyim-dhashcache-update-shortcode2word-1. |
Date: |
Mon, 13 Dec 2021 07:57:38 -0500 (EST) |
branch: externals/pyim
commit 02c50045cb14ab253d8d8435e83e7f10b0bbc130
Author: Feng Shu <tumashu@163.com>
Commit: Feng Shu <tumashu@163.com>
简化 pyim-dhashcache-update-ishortcode2word-1 和
pyim-dhashcache-update-shortcode2word-1.
* tests/pyim-tests.el (pyim-tests-pyim-dhashcache-update-shortcode2word)
(pyim-tests-pyim-dhashcache-update-ishortcode2word): Simplify.
* pyim-dhashcache.el (pyim-dhashcache-sort-words): Add iword2count
argument.
(pyim-dhashcache-update-ishortcode2word): simplify.
(pyim-dhashcache-update-ishortcode2word-1): Add icode2word and
iword2count argument.
(pyim-dhashcache-update-shortcode2word): simplify.
(pyim-dhashcache-update-shortcode2word-1): Add code2word and
iword2count argument.
(pyim-dhashcache-update-icode2word): update for
pyim-dhashcache-sord-words.
* pyim-dcache.el (pyim-dcache-save-variable): Add value argument.
---
pyim-dcache.el | 4 +-
pyim-dhashcache.el | 125 +++++++++++++++++++++++++++-------------------------
tests/pyim-tests.el | 56 ++++++++++-------------
3 files changed, 92 insertions(+), 93 deletions(-)
diff --git a/pyim-dcache.el b/pyim-dcache.el
index 06c4a34..ef7990f 100644
--- a/pyim-dcache.el
+++ b/pyim-dcache.el
@@ -103,11 +103,11 @@ VARIABLE 变量,FORCE-RESTORE 设置为 t 时,强制恢复,变量原来的
fallback-value
(make-hash-table :test #'equal))))))
-(defun pyim-dcache-save-variable (variable)
+(defun pyim-dcache-save-variable (variable &optional value)
"将 VARIABLE 变量的取值保存到 `pyim-dcache-directory' 中对应文件中."
(let ((file (expand-file-name (symbol-name variable)
pyim-dcache-directory))
- (value (symbol-value variable)))
+ (value (or value (symbol-value variable))))
(pyim-dcache-save-value-to-file value file)))
(defun pyim-dcache-save-value-to-file (value file)
diff --git a/pyim-dhashcache.el b/pyim-dhashcache.el
index c81155e..b5aa590 100644
--- a/pyim-dhashcache.el
+++ b/pyim-dhashcache.el
@@ -53,14 +53,16 @@
(defvar pyim-dhashcache-update-icode2word-p nil)
(defvar pyim-dhashcache-update-code2word-running-p nil)
-(defun pyim-dhashcache-sort-words (words-list)
+(defun pyim-dhashcache-sort-words (words-list &optional iword2count)
"对 WORDS-LIST 排序,词频大的排在前面.
-排序使用 `pyim-dhashcache-iword2count' 中记录的词频信息"
- (sort words-list
- (lambda (a b)
- (> (or (gethash a pyim-dhashcache-iword2count) 0)
- (or (gethash b pyim-dhashcache-iword2count) 0)))))
+如果 IWORD2COUNT 为 nil, 排序将使用 `pyim-dhashcache-iword2count'
+中记录的词频信息"
+ (let ((iword2count (or iword2count pyim-dhashcache-iword2count)))
+ (sort words-list
+ (lambda (a b)
+ (> (or (gethash a iword2count) 0)
+ (or (gethash b iword2count) 0))))))
(defun pyim-dhashcache-get-shortcode (code)
"获取一个 CODE 的所有简写.
@@ -99,34 +101,37 @@
(require 'pyim-dhashcache)
(pyim-dcache-set-variable 'pyim-dhashcache-icode2word)
(pyim-dcache-set-variable 'pyim-dhashcache-iword2count)
- (pyim-dhashcache-update-ishortcode2word-1))
+ (pyim-dcache-save-variable
+ 'pyim-dhashcache-ishortcode2word
+ (pyim-dhashcache-update-ishortcode2word-1
+ pyim-dhashcache-icode2word
+ pyim-dhashcache-iword2count)))
(lambda (_)
(pyim-dcache-set-variable 'pyim-dhashcache-ishortcode2word t)))))
-(defun pyim-dhashcache-update-ishortcode2word-1 ()
+(defun pyim-dhashcache-update-ishortcode2word-1 (icode2word iword2count)
"`pyim-dhashcache-update-ishortcode2word' 内部函数."
- (setq pyim-dhashcache-ishortcode2word
- (make-hash-table :test #'equal))
- (maphash
- (lambda (key value)
- (when (and (> (length key) 0)
- (not (string-match-p "[^a-z-]" key)))
- (let* ((newkey (mapconcat
- (lambda (x)
- (substring x 0 1))
- (split-string key "-") "-")))
- (puthash newkey
- (delete-dups
- `(,@(gethash newkey pyim-dhashcache-ishortcode2word)
- ,@value))
- pyim-dhashcache-ishortcode2word))))
- pyim-dhashcache-icode2word)
- (maphash
- (lambda (key value)
- (puthash key (pyim-dhashcache-sort-words value)
- pyim-dhashcache-ishortcode2word))
- pyim-dhashcache-ishortcode2word)
- (pyim-dcache-save-variable 'pyim-dhashcache-ishortcode2word))
+ (let ((ishortcode2word (make-hash-table :test #'equal)))
+ (maphash
+ (lambda (key value)
+ (when (and (> (length key) 0)
+ (not (string-match-p "[^a-z-]" key)))
+ (let* ((newkey (mapconcat
+ (lambda (x)
+ (substring x 0 1))
+ (split-string key "-") "-")))
+ (puthash newkey
+ (delete-dups
+ `(,@(gethash newkey ishortcode2word)
+ ,@value))
+ ishortcode2word))))
+ icode2word)
+ (maphash
+ (lambda (key value)
+ (puthash key (pyim-dhashcache-sort-words value iword2count)
+ ishortcode2word))
+ ishortcode2word)
+ ishortcode2word))
(defun pyim-dhashcache-update-shortcode2word (&optional force)
"使用 `pyim-dhashcache-code2word' 中的词条,创建简写 code 词库缓存并加载.
@@ -143,38 +148,40 @@
(require 'pyim-dhashcache)
(pyim-dcache-set-variable 'pyim-dhashcache-code2word)
(pyim-dcache-set-variable 'pyim-dhashcache-iword2count)
- (pyim-dhashcache-update-shortcode2word-1))
+ (pyim-dcache-save-variable
+ 'pyim-dhashcache-shortcode2word
+ (pyim-dhashcache-update-shortcode2word-1
+ pyim-dhashcache-code2word
+ pyim-dhashcache-iword2count)))
(lambda (_)
(pyim-dcache-set-variable 'pyim-dhashcache-shortcode2word t)))))
-(defun pyim-dhashcache-update-shortcode2word-1 ()
+(defun pyim-dhashcache-update-shortcode2word-1 (code2word iword2count)
"`pyim-dhashcache-update-shortcode2word' 的内部函数"
- (setq pyim-dhashcache-shortcode2word
- (make-hash-table :test #'equal))
- (maphash
- (lambda (key value)
- (dolist (x (pyim-dhashcache-get-shortcode key))
- (puthash x
- (mapcar
- (lambda (word)
- ;; 这个地方的代码用于实现五笔 code 自动提示功能,
- ;; 比如输入 'aa' 后得到选词框:
- ;; ----------------------
- ;; | 1. 莁aa 2.匶wv ... |
- ;; ----------------------
- (if (get-text-property 0 :comment word)
- word
- (propertize word :comment (substring key (length x)))))
- (delete-dups `(,@(gethash x pyim-dhashcache-shortcode2word)
,@value)))
- pyim-dhashcache-shortcode2word)))
- pyim-dhashcache-code2word)
- (maphash
- (lambda (key value)
- (puthash key (pyim-dhashcache-sort-words value)
- pyim-dhashcache-shortcode2word))
- pyim-dhashcache-shortcode2word)
- (pyim-dcache-save-variable 'pyim-dhashcache-shortcode2word)
- nil)
+ (let ((shortcode2word (make-hash-table :test #'equal)))
+ (maphash
+ (lambda (key value)
+ (dolist (x (pyim-dhashcache-get-shortcode key))
+ (puthash x
+ (mapcar
+ (lambda (word)
+ ;; 这个地方的代码用于实现五笔 code 自动提示功能,
+ ;; 比如输入 'aa' 后得到选词框:
+ ;; ----------------------
+ ;; | 1. 莁aa 2.匶wv ... |
+ ;; ----------------------
+ (if (get-text-property 0 :comment word)
+ word
+ (propertize word :comment (substring key (length x)))))
+ (delete-dups `(,@(gethash x shortcode2word) ,@value)))
+ shortcode2word)))
+ code2word)
+ (maphash
+ (lambda (key value)
+ (puthash key (pyim-dhashcache-sort-words value iword2count)
+ shortcode2word))
+ shortcode2word)
+ shortcode2word))
(defun pyim-dhashcache-get-path (variable)
"获取保存 VARIABLE 取值的文件的路径."
@@ -323,7 +330,7 @@ code 对应的中文词条了。
(pyim-dcache-set-variable 'pyim-dhashcache-iword2count)
(maphash
(lambda (key value)
- (puthash key (pyim-dhashcache-sort-words value)
+ (puthash key (pyim-dhashcache-sort-words value
pyim-dhashcache-iword2count)
pyim-dhashcache-icode2word))
pyim-dhashcache-icode2word)
(pyim-dcache-save-variable 'pyim-dhashcache-icode2word)
diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el
index 4b4a416..93fe7cc 100644
--- a/tests/pyim-tests.el
+++ b/tests/pyim-tests.el
@@ -641,56 +641,48 @@ zuo-zuo-you-mang 作作有芒")
(should (equal (gethash "啊啊" output2) nil))))
(ert-deftest pyim-tests-pyim-dhashcache-update-shortcode2word ()
- (let ((pyim-dcache-directory (file-name-as-directory (make-temp-name
"pyim-dcache-")))
- (pyim-dhashcache-code2word (make-hash-table :test #'equal))
- (pyim-dhashcache-iword2count (make-hash-table :test #'equal))
- (pyim-dhashcache-shortcode2word (make-hash-table :test #'equal))
+ (let ((code2word (make-hash-table :test #'equal))
+ (iword2count (make-hash-table :test #'equal))
+ (shortcode2word (make-hash-table :test #'equal))
output)
- (puthash "wubi/a" '("戈") pyim-dhashcache-code2word)
- (puthash "wubi/aa" '("式" "藏") pyim-dhashcache-code2word)
- (puthash "wubi/aaa" '("工") pyim-dhashcache-code2word)
- (puthash "wubi/aaaa" '("工" "㠭") pyim-dhashcache-code2word)
- (puthash "wubi/aaab" '("㐂") pyim-dhashcache-code2word)
- (puthash "wubi/aaae" '("𧝣") pyim-dhashcache-code2word)
+ (puthash "wubi/a" '("戈") code2word)
+ (puthash "wubi/aa" '("式" "藏") code2word)
+ (puthash "wubi/aaa" '("工") code2word)
+ (puthash "wubi/aaaa" '("工" "㠭") code2word)
+ (puthash "wubi/aaab" '("㐂") code2word)
+ (puthash "wubi/aaae" '("𧝣") code2word)
- (pyim-dhashcache-update-shortcode2word-1)
+ (setq shortcode2word
+ (pyim-dhashcache-update-shortcode2word-1 code2word iword2count))
- (with-temp-buffer
- (insert-file-contents (concat pyim-dcache-directory
"pyim-dhashcache-shortcode2word"))
- (setq output (read (current-buffer))))
-
- (should (equal (gethash "wubi/aa" output)
+ (should (equal (gethash "wubi/aa" shortcode2word)
'(#("工" 0 1 (:comment "a"))
#("㠭" 0 1 (:comment "aa"))
#("㐂" 0 1 (:comment "ab"))
#("𧝣" 0 1 (:comment "ae")))))
- (should (equal (gethash "wubi/aaa" output)
+ (should (equal (gethash "wubi/aaa" shortcode2word)
'(#("工" 0 1 (:comment "a"))
#("㠭" 0 1 (:comment "a"))
#("㐂" 0 1 (:comment "b"))
#("𧝣" 0 1 (:comment "e")))))))
(ert-deftest pyim-tests-pyim-dhashcache-update-ishortcode2word ()
- (let ((pyim-dcache-directory (file-name-as-directory (make-temp-name
"pyim-dcache-")))
- (pyim-dhashcache-icode2word (make-hash-table :test #'equal))
- (pyim-dhashcache-iword2count (make-hash-table :test #'equal))
- (pyim-dhashcache-ishortcode2word (make-hash-table :test #'equal))
- output)
+ (let ((icode2word (make-hash-table :test #'equal))
+ (iword2count (make-hash-table :test #'equal))
+ ishortcode2word)
- (puthash "ni" '("你" "呢") pyim-dhashcache-icode2word)
- (puthash "ni-hao" '("你好" "呢耗") pyim-dhashcache-icode2word)
- (puthash "ni-huai" '("你坏") pyim-dhashcache-icode2word)
-
- (pyim-dhashcache-update-ishortcode2word-1)
+ (puthash "ni" '("你" "呢") icode2word)
+ (puthash "ni-hao" '("你好" "呢耗") icode2word)
+ (puthash "ni-huai" '("你坏") icode2word)
- (with-temp-buffer
- (insert-file-contents (concat pyim-dcache-directory
"pyim-dhashcache-ishortcode2word"))
- (setq output (read (current-buffer))))
+ (setq ishortcode2word
+ (pyim-dhashcache-update-ishortcode2word-1
+ icode2word iword2count))
- (should (equal (gethash "n-h" output)
+ (should (equal (gethash "n-h" ishortcode2word)
'("你好" "呢耗" "你坏")))
- (should (equal (gethash "n" output)
+ (should (equal (gethash "n" ishortcode2word)
'("你" "呢")))))
(ert-deftest pyim-tests-pyim-dhashcache-put/delete ()
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [elpa] externals/pyim 02c5004: 简化 pyim-dhashcache-update-ishortcode2word-1 和 pyim-dhashcache-update-shortcode2word-1.,
ELPA Syncer <=