branch: externals/greader
commit 3f34b5c1cc204ffb1e90a36fffae0f0ae8bdc0ab
Author: Michelangelo Rodriguez <michelangelo.rodrig...@gmail.com>
Commit: Michelangelo Rodriguez <michelangelo.rodrig...@gmail.com>

    greader: version 0.9.4
    
    General improvements to         `greader-dict--get-key-from-word':
    Now it handles efficently the process of searching.
    First, when there was not keys corresponding to a given word, the
    algoritm scanned all the keys in the hash-table.
    Now first the function checks if the word as is is present in the
    hash-table, and in that case returns it.
    If the word is not present as is, then we reduce the dictionary to
    contain only the words classified as matches, and then searches for
    the appropriate key.
---
 greader-dict.el | 52 ++++++++++++++++++++++++++++------------------------
 greader.el      |  2 +-
 2 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/greader-dict.el b/greader-dict.el
index eee2fc47b0..b83042f8d6 100644
--- a/greader-dict.el
+++ b/greader-dict.el
@@ -234,26 +234,31 @@ Return nil if KEY is not present in `greader-dictionary'."
 (defun greader-dict--get-key-from-word (word)
   "Return key related to WORD, nil otherwise."
   (setq word (string-trim word))
-  (let ((key nil))
-    (maphash
-     (lambda (k v)
-       (let* ((result (string-remove-suffix
-                      greader-dict-match-indicator k))
-             (candidate-matches (string-split result "\\W" t)))
-        (setq candidate-matches (sort candidate-matches
-                                      (lambda (s1 s2) (> (length s1)
-                                                         (length
-                                                          s2)))))
-        (catch 'matched
-          (dolist (candidate candidate-matches)
-            ;; (message "%s" (concat "matching " candidate " against " word 
"..."))
-            (when (string-match candidate word)
-              ;; (message "Matched!")
-              (unless key
-                (setq key k)
-                (throw 'matched key)))))))
-     greader-dictionary)
-    key))
+  (cond
+   ((gethash word greader-dictionary)
+    word)
+   (t
+    (let ((reduced-dictionary (make-hash-table :test 'ignore-case)))
+      (dolist (item (greader-dict--get-matches 'match))
+       (puthash item (gethash item greader-dictionary) reduced-dictionary))
+      (let ((key nil))
+       (catch 'matched
+         (maphash
+          (lambda (k v)
+            (let* ((result (string-remove-suffix
+                            greader-dict-match-indicator k))
+                   (candidate-matches (string-split result "\\W" t)))
+              (setq candidate-matches (sort candidate-matches
+                                            (lambda (s1 s2) (> (length s1)
+                                                               (length
+                                                                s2)))))
+              (dolist (candidate candidate-matches)
+                ;; (message "%s" (concat "matching " candidate " against " 
word "..."))
+                (when (string-match candidate word)
+                  ;; (message "Matched!")
+                  (setq key (concat k greader-dict-match-indicator))
+                  (throw 'matched key)))))
+          reduced-dictionary)))))))
 
 ;; This function checks that, in the string you pass to it, there are
 ;; effectively words to be replaced. If so, use apis
@@ -273,11 +278,10 @@ Return nil if KEY is not present in `greader-dictionary'."
       (re-search-forward "\\w" nil t)
       (while (not (eobp))
        (let*
-           ((key (greader-dict--get-key-from-word (downcase
-                                                   (thing-at-point
-                                                    'word))))
+           ((key (greader-dict--get-key-from-word (thing-at-point
+                                                   'word)))
             (modified-word
-             (concat (downcase (thing-at-point 'word))
+             (concat (thing-at-point 'word)
                      greader-dict-match-indicator)))
          (cond
           ((equal (greader-dict-item-type key) 'word)
diff --git a/greader.el b/greader.el
index 9243eab76b..8870efc603 100644
--- a/greader.el
+++ b/greader.el
@@ -6,7 +6,7 @@
 ;; Author: Michelangelo Rodriguez <michelangelo.rodrig...@gmail.com>
 ;; Keywords: tools, accessibility
 
-;; Version: 0.9.3
+;; Version: 0.9.4
 
 ;; This program is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by

Reply via email to