branch: externals/pyim commit 6d1d93f22a07d51e5a78b3fe91d2107391fcbfed Author: Feng Shu <tuma...@163.com> Commit: Feng Shu <tuma...@163.com>
* pyim-candidates.el (pyim-candidates-search-buffer): Sort words by count. --- pyim-candidates.el | 15 +++++++++++---- tests/pyim-tests.el | 28 +++------------------------- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/pyim-candidates.el b/pyim-candidates.el index c2ee97a..b3aba11 100644 --- a/pyim-candidates.el +++ b/pyim-candidates.el @@ -123,15 +123,22 @@ IMOBJS 获得候选词条。" (defun pyim-candidates-search-buffer (regexp) "在当前 buffer 中使用 REGEXP 搜索词条。" (save-excursion - (let (words) + (let ((counts (make-hash-table :test #'equal)) + (time-limit 0.1) + words) (goto-char (point-min)) (pyim-time-limit-while (and (not (input-pending-p)) - (re-search-forward regexp nil t)) 0.1 + (re-search-forward regexp nil t)) time-limit (let ((match (match-string-no-properties 0))) ;; NOTE: 单个汉字我觉得不值得收集。 (when (>= (length match) 2) - (cl-pushnew match words :test #'equal)))) - words))) + (if (member match words) + (cl-incf (gethash match counts)) + (push match words) + (puthash match 1 counts))))) + (sort words (lambda (a b) + (> (or (gethash a counts) 0) + (or (gethash b counts) 0))))))) (defun pyim-candidates-create-quanpin (imobjs scheme-name &optional fast-search) "`pyim-candidates-create:quanpin' 内部使用的函数。" diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el index 03acd9f..aba806a 100644 --- a/tests/pyim-tests.el +++ b/tests/pyim-tests.el @@ -349,31 +349,9 @@ ;; ** pyim-candidates 相关单元测试 (ert-deftest pyim-tests-pyim-candidates-search-buffer () (with-temp-buffer - (insert " -一日,正当嗟悼之际,俄见一僧一道远远而来,生得骨格不凡,丰神迥别,说说 -笑笑,来至峰下,坐于石边,高谈快论:先是说些云山雾海、神仙玄幻之事,后 -便说到红尘中荣华富贵。此石听了,不觉打动凡心,也想要到人间去享一享这荣 -华富贵,但自恨粗蠢,不得已,便口吐人言,向那僧道说道:“大师,弟子蠢物, -不能见礼了!适闻二位谈那人世间荣耀繁华,心切慕之。弟子质虽粗蠢,性却稍 -通,况见二师仙形道体,定非凡品,必有补天济世之材,利物济人之德。如蒙发 -一点慈心,携带弟子得入红尘,在那富贵场中,温柔乡里受享几年,自当永佩洪 -恩,万劫不忘也!”二仙师听毕,齐憨笑道:“善哉,善哉!那红尘中有却有些乐 -事,但不能永远依恃;况又有‘美中不足,好事多磨’八个字紧相连属,瞬息间则 -又乐极悲生,人非物换,究竟是到头一梦,万境归空,倒不如不去的好。”这石 -凡心已炽,那里听得进这话去,乃复苦求再四。二仙知不可强制,乃叹道:“此 -亦静极思动,无中生有之数也!既如此,我们便携你去受享受享,只是到不得意 -时,切莫后悔!”石道:“自然,自然。”那僧又道:“若说你性灵,却又如此质蠢, -并更无奇贵之处。如此也只好踮脚而已。也罢!我如今大施佛法,助你助,待劫 -终之日,复还本质,以了此案。你道好否?”石头听了,感谢不尽。那僧便念咒 -书符,大展幻术,将一块大石登时变成一块鲜明莹洁的美玉,且又缩成扇坠大小 -的可佩可拿。那僧托于掌上,笑道:“形体倒也是个宝物了!还只没有实在的好 -处,须得再镌上数字,使人一见便知是奇物方妙。然后好携你到那昌明隆盛之邦、 -诗礼簪缨之族、花柳繁华地、温柔富贵乡去安身乐业。”石头听了,喜不能禁, -乃问:“不知赐了弟子那哪几件奇处?又不知携了弟子到何地方?望乞明示,使 -弟子不惑。”那僧笑道:“你且莫问,日后自然明白的。”说着,便袖了这石,同 -那道人飘然而去,竟不知投奔何方何舍。") - (should (equal (pyim-candidates-search-buffer (pyim-cregexp-build "hs" 3 t)) - '("何舍" "幻术" "好事"))))) + (insert "你好你好你坏你坏你话牛蛤牛和牛蛤牛蛤牛蛤牛蛤牛蛤") + (should (equal (pyim-candidates-search-buffer (pyim-cregexp-build "nh" 3 t)) + '("牛蛤" "你坏" "你好" "牛和" "你话"))))) ;; ** pyim-cstring 相关单元测试 (ert-deftest pyim-tests-pyim-cstring-partition ()