summaryrefslogtreecommitdiffstats
path: root/.emacs.d/fuzzy.el
blob: c69150ad04d71a0900c0a57f02b481cc9f005302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
;;; fuzzy.el --- Fuzzy matching utilities

;; Copyright (C) 2010  Tomohiro Matsuyama

;; Author: Tomohiro Matsuyama <m2ym.pub@gmail.com>
;; Keywords: convenience

;; 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
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; 

;;; Code:

(eval-when-compile
  (require 'cl))
(require 'regexp-opt)

(defgroup fuzzy nil
  "Fuzzy matching utilities."
  :group 'convenience
  :prefix "fuzzy-")

(defcustom fuzzy-accept-error-rate 0.10
  "Error threshold."
  :group 'fuzzy)

(defvar fuzzy-accept-length-difference 2)

(defvar fuzzy-regexp-some-char (format "\\w\\{0,%s\\}" fuzzy-accept-length-difference))



;;; Functions

(defun fuzzy-reverse-string (string)
  (apply 'string (nreverse (append string nil))))

(defun fuzzy-regexp-compile (string)
  (labels ((oddp (n) (eq (logand n 1) 1))
           (evenp (n) (eq (logand n 1) 0))
           (opt (n) (regexp-opt-charset (append (substring string
                                                           (max 0 (- n 1))
                                                           (min (length string) (+ n 2))) nil))))
    (concat
     "\\("
     (loop for i below (length string)
           for c = (if (evenp i) (opt i) fuzzy-regexp-some-char)
           concat c)
     "\\|"
     (loop for i below (length string)
           for c = (if (oddp i) (opt i) fuzzy-regexp-some-char)
           concat c)
     "\\)")))

(defalias 'fuzzy-edit-distance 'fuzzy-jaro-winkler-distance)

(defun fuzzy-jaro-winkler-distance (s1 s2)
  "http://en.wikipedia.org/wiki/Jaro-Winkler_distance"
  (let* ((l1 (length s1))
         (l2 (length s2))
         (r (max 1 (1- (/ (max l1 l2) 2))))
         (m 0)
         (tr 0)
         (p 0)
         cs1 cs2)
    (loop with seen = (make-vector l2 nil)
          for i below l1
          for c1 = (aref s1 i) do
          (loop for j from (max 0 (- i r)) below (min l2 (+ i r))
                for c2 = (aref s2 j)
                if (and (char-equal c1 c2)
                        (null (aref seen j))) do
                  (push c1 cs1)
                  (aset seen j c2)
                  (incf m)
                and return nil)
          finally
            (setq cs1 (nreverse cs1)
                  cs2 (loop for i below l2
                            for c = (aref seen i)
                            if c collect c)))
    (loop for c1 in cs1
          for c2 in cs2
          if (not (char-equal c1 c2)) do
            (incf tr))
    (loop for i below (min m 5)
          for c1 across s1
          for c2 across s2
          while (char-equal c1 c2) do
            (incf p))
    (if (eq m 0)
        0.0
      (setq m (float m))
      (let* ((dj (/ (+ (/ m l1) (/ m l2) (/ (- m (/ tr 2)) m)) 3))
             (dw (+ dj (* p 0.1 (- 1 dj)))))
        dw))))

;; this function should be compiled
(byte-compile 'fuzzy-jaro-winkler-distance)

(defun fuzzy-match (s1 s2 &optional function)
  (or function (setq function 'fuzzy-edit-distance))
  (and (<= (abs (- (length s1) (length s2)))
           fuzzy-accept-length-difference)
       (>= (funcall function s1 s2)
           (- 1 fuzzy-accept-error-rate))))

(defun fuzzy-all-completions (string collection)
  "all-completions family with fuzzy matching."
  (loop with length = (length string)
        for str in collection
        for s = (substring str 0 (min (length str)
                                      (+ length fuzzy-accept-length-difference)))
        if (fuzzy-match string s)
        collect str))



;;; Search and Incremental Search

(defvar fuzzy-search-cache nil)
(defvar fuzzy-search-cache-string nil)

(defun fuzzy-search-cache-activate ()
  (setq fuzzy-search-cache (make-hash-table))
  (setq fuzzy-search-cache-string nil))

(defun fuzzy-search-cache-deactive ()
  (setq fuzzy-search-cache nil)
  (setq fuzzy-search-cache-string nil))

(defun fuzzy-search-edit-distance (s1 s2)
  (or (and fuzzy-search-cache
           (cond
            ((null fuzzy-search-cache-string)
             (setq fuzzy-search-cache-string s1)
             nil)
            ((not (equal fuzzy-search-cache-string s1))
             (setq fuzzy-search-cache-string s1)
             (clrhash fuzzy-search-cache)
             nil)
            (t))
           (gethash s2 fuzzy-search-cache))
      (let ((d (fuzzy-edit-distance s1 s2)))
        (if fuzzy-search-cache
            (puthash s2 d fuzzy-search-cache))
        d)))

(defun fuzzy-search-match (s1 s2)
  (fuzzy-match s1 s2 'fuzzy-search-edit-distance))

(defun fuzzy-search-forward (string &optional bound noerror count)
  (let* ((regexp (fuzzy-regexp-compile string))
         match-data)
    (save-excursion
      (while (and (null match-data)
                  (re-search-forward regexp bound t))
        (if (fuzzy-search-match string (match-string 1))
            (setq match-data (match-data))
          (goto-char (1+ (match-beginning 1))))))
    (when match-data
      (store-match-data match-data)
      (goto-char (match-end 1)))))

(defun fuzzy-search-backward (string &optional bound noerror count)
  (let* ((regexp (fuzzy-regexp-compile string))
         match-data begin end)
    (save-excursion
      (while (and (null match-data)
                  (re-search-backward regexp bound t))
        (setq begin (match-beginning 1)
              end (match-end 1))
        (store-match-data nil)
        (goto-char (max (point-min) (- begin (* (length string) 2))))
        (while (re-search-forward regexp end t)
          (if (fuzzy-search-match string (match-string 1))
              (setq match-data (match-data))
            (goto-char (1+ (match-beginning 1)))))
        (unless match-data
          (goto-char begin)))
    (if match-data
        (progn
          (store-match-data match-data)
          (goto-char (match-beginning 1)))
      (store-match-data nil)))))

(defvar fuzzy-isearch nil)
(defvar fuzzy-isearch-failed-count 0)
(defvar fuzzy-isearch-enabled 'on-failed)
(defvar fuzzy-isearch-original-search-fun nil)
(defvar fuzzy-isearch-prefix "[FUZZY] ")

(defun fuzzy-isearch-activate ()
  (setq fuzzy-isearch t)
  (setq fuzzy-isearch-failed-count 0)
  (fuzzy-search-cache-activate))

(defun fuzzy-isearch-deactivate ()
  (setq fuzzy-isearch nil)
  (setq fuzzy-isearch-failed-count 0)
  (fuzzy-search-cache-deactive))

(defun fuzzy-isearch ()
  (cond (isearch-word
         (if isearch-forward 'word-search-forward 'word-search-backward))
        (isearch-regexp
         (if isearch-forward 're-search-forward 're-search-backward))
        ((or fuzzy-isearch
             (eq fuzzy-isearch-enabled 'always)
             (and (eq fuzzy-isearch-enabled 'on-failed)
                  (null isearch-success)
                  isearch-wrapped
                  (> (setq fuzzy-isearch-failed-count (1+ fuzzy-isearch-failed-count))
                     1)))
         (unless fuzzy-isearch
           ;(goto-char isearch-opoint)
           (fuzzy-isearch-activate))
         (if isearch-forward 'fuzzy-search-forward 'fuzzy-search-backward))
        (t
         (if isearch-forward 'search-forward 'search-backward))))

(defun fuzzy-isearch-end-hook ()
  (fuzzy-isearch-deactivate))

(defun turn-on-fuzzy-isearch ()
  (interactive)
  (setq fuzzy-isearch-original-search-fun isearch-search-fun-function)
  (setq isearch-search-fun-function 'fuzzy-isearch)
  (add-hook 'isearch-mode-end-hook 'fuzzy-isearch-end-hook))

(defun turn-off-fuzzy-isearch ()
  (interactive)
  (setq isearch-search-fun-function fuzzy-isearch-original-search-fun)
  (remove-hook 'isearch-mode-end-hook 'fuzzy-isearch-end-hook))

(defadvice isearch-message-prefix (after fuzzy-isearch-message-prefix activate)
  (if fuzzy-isearch
      (setq ad-return-value (concat fuzzy-isearch-prefix ad-return-value))
    ad-return-value))

(provide 'fuzzy)
;;; fuzzy.el ends here