branch: externals/pyim commit f72f23c2f78dd89bf6eb7101e18442cf7f34d1eb Author: Feng Shu <tuma...@163.com> Commit: Feng Shu <tuma...@163.com>
Add pyim-dhashcache-get-ishortcodes. * pyim-dhashcache.el (pyim-dhashcache-get-ishortcodes): New function. (pyim-dhashcache-update-ishortcode2word-1) (pyim-dhashcache-insert-word-into-ishortcode2word): Use pyim-dhashcache-get-ishortcodes. --- pyim-dhashcache.el | 39 ++++++++++++++++++++++----------------- tests/pyim-tests.el | 6 ++++++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/pyim-dhashcache.el b/pyim-dhashcache.el index 2e800a8..aae3dc0 100644 --- a/pyim-dhashcache.el +++ b/pyim-dhashcache.el @@ -84,6 +84,20 @@ (push (concat prefix (substring code1 0 i)) results))) results))) +(defun pyim-dhashcache-get-ishortcodes (code) + "获取CODE 所有的简写 ishortcodes. + +比如: ni-hao -> (n-h) + +注意事项:这个函数用于全拼输入法。" + (when (and (> (length code) 0) + (not (pyim-string-match-p "/" code)) + (not (pyim-string-match-p "[^a-z-]" code))) + (list (mapconcat + (lambda (x) + (substring x 0 1)) + (split-string code "-") "-")))) + (defun pyim-dhashcache-async-inject-variables () "pyim's async-inject-variables." (list (async-inject-variables "^load-path$") @@ -118,17 +132,12 @@ (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)))) + (dolist (newkey (pyim-dhashcache-get-ishortcodes key)) + (puthash newkey + (delete-dups + `(,@(gethash newkey ishortcode2word) + ,@value)) + ishortcode2word))) icode2word) (maphash (lambda (key value) @@ -446,14 +455,10 @@ code 对应的中文词条了。 默认 WORD 放到已有词条的最后,如果 PREPEND 为 non-nil, WORD 将放 到已有词条的最前面。" - (when (string-match-p "-" code) + (dolist (newcode (pyim-dhashcache-get-ishortcodes code)) (pyim-dhashcache-put pyim-dhashcache-ishortcode2word - ;; ni-hao -> n-h - (mapconcat (lambda (x) - (substring x 0 1)) - (split-string code "-") - "-") + newcode (if prepend `(,word ,@(remove word orig-value)) `(,@(remove word orig-value) ,word))))) diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el index fd92455..2fcfcf0 100644 --- a/tests/pyim-tests.el +++ b/tests/pyim-tests.el @@ -608,6 +608,12 @@ (should (equal (pyim-dhashcache-get-shortcodes "ni-hao") nil)) (should (equal (pyim-dhashcache-get-shortcodes "") nil))) +(ert-deftest pyim-tests-pyim-dhashcache-get-ishortcodes () + (should (equal (pyim-dhashcache-get-ishortcodes "ni-hao") '("n-h"))) + (should (equal (pyim-dhashcache-get-ishortcodes "wubi/aaaa") nil)) + (should (equal (pyim-dhashcache-get-ishortcodes "ni") '("n"))) + (should (equal (pyim-dhashcache-get-ishortcodes "") nil))) + (ert-deftest pyim-tests-pyim-dhashcache-get-path () (let ((pyim-dcache-directory "/tmp/dcache")) (should (equal (pyim-dhashcache-get-path 'hello) "/tmp/dcache/hello"))