-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocomplete.rkt
270 lines (241 loc) · 9.78 KB
/
autocomplete.rkt
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#lang racket
(require racket/class racket/match racket/list)
(provide prefix-tree%
gnode)
;; add edit distance function style
;; try making (hof tree prefix node-pred-list node-perform-list)
(struct gnode (alphabet frequency childlist) #:transparent #:mutable)
(define prefix-tree%
(class object%
(super-new)
;; a field to get the initial words to set the tree
(init-field initial-word-list)
;; private variable to define the trie
(define main-trie (gnode #\space +inf.0 '()))
;; temp for using map without displaying
(define te 1)
;; add all the initial words to the trie
(set! te (insert-words (map (lambda (x) (cons x 1)) initial-word-list)))
;; public function to display the trie
(define/public (show)
(map (lambda (z) (cons (list->string (cdr (car z))) (cdr z))) (all-suffixes main-trie)))
(define/public (show-trie)
(displayln main-trie))
;; public function to add a word
(define/public (insert-word-freq word num)
(define (add-word-helper word num trie)
(match word
[(cons x '())
(match trie
[(gnode alp fr chl)
#:when (equal? alp x)
(begin (set-gnode-frequency! trie (+ fr num))
trie)]
[_ trie]
)]
[(cons x rest)
(match trie
[(gnode alp fr chl)
#:when (equal? alp x)
(let ([insert-point (ormap (lambda (x) (equal? (car rest) (gnode-alphabet x)))
chl)])
(if (equal? #f insert-point)
(begin (set-gnode-childlist! trie (cons (add-word-helper rest num (gnode (car rest) 0 '()))
chl))
trie)
(begin (set-gnode-childlist! trie (map (lambda (x) (add-word-helper rest num x))
chl))
trie)))]
[_ trie]
)]))
(add-word-helper (cons #\space (string->list word)) num main-trie))
;; public function to delete the frequency of a word from the trie
(define/public (delete-word-freq word freq)
(define (dwf-helper word freq trie)
(match word
[(cons x '())
(match trie
[(gnode alp fr chl)
#:when (equal? alp x)
(begin (set-gnode-frequency! trie (max 0 (- fr freq)))
trie)]
[_ trie]
)]
[(cons x rest)
(match trie
[(gnode alp fr chl)
#:when (equal? alp x)
(begin (set-gnode-childlist! trie (map (lambda (x) (dwf-helper rest freq x)) chl))
trie)]
[_ trie]
)]))
(dwf-helper (cons #\space (string->list word)) freq main-trie))
(define/public (prune trie)
(match trie
[(gnode alp fr '())
#:when (equal? fr 0)
'()]
[(gnode alp fr chl)
#:when (equal? fr 0)
(let* ([res (map (lambda (x) (prune x)) chl)])
(cond [(equal? '(()) (remove-duplicates res))
'()]
[else (gnode alp fr (remove* (list '()) res))]))]
[_ trie]))
(define/public (delete-word word freq)
(begin (when word (delete-word-freq word freq))
(set! main-trie (prune main-trie))))
(define/public (delete-words lst)
(begin (map (lambda (z) (delete-word (car z) (cdr z))) lst)
(set! main-trie (prune main-trie))))
(define/public (insert-word word freq)
(begin (when word (insert-word-freq word freq))
(set! main-trie (prune main-trie))))
(define/public (insert-words lst)
(begin (map (lambda (z) (insert-word (car z) (cdr z))) lst)
(set! main-trie (prune main-trie))))
;; private function that gets all suffixes from this node
(define/private (all-suffixes trie)
(match trie
[(gnode alp freq chl)
#:when (> freq 0)
(cons (cons (list alp) freq)
(map (lambda (y) (cons (cons alp (car y)) (cdr y)))
(append-map (lambda (z) (all-suffixes z)) chl)))]
[(gnode alp freq chl)
(map (lambda (y) (cons (cons alp (car y)) (cdr y)))
(append-map (lambda (z) (all-suffixes z)) chl))]
[_ (error "Suffix not a gnode")]))
;; public function that finds all completions for a given prefix
(define/public (get-all-completions prefix)
(define (get-all-completions-helper prefix build-prefix trie)
(match prefix
['()
(match trie
[(gnode _ _ _)
(map (lambda (z) (cons (list->string (append (reverse build-prefix) (car z))) (cdr z)))
(all-suffixes trie))]
[_ (begin (displayln trie)
trie)])]
[(cons x rest)
(match trie
[(gnode alp fre chl)
#:when (equal? x alp)
(cond [(equal? x #\space)
(append-map (lambda (z) (get-all-completions-helper rest build-prefix z))
chl)]
[(and (equal? rest '()) (equal? chl '()) (> fre 0))
(list (cons (list->string (reverse (cons x build-prefix))) fre))]
[else
(append-map (lambda (z) (get-all-completions-helper rest (cons x build-prefix) z))
chl)])]
[(gnode alp _ chl)
(append-map (lambda (z) (get-all-completions-helper prefix (cons alp build-prefix) z))
chl)]
[_ '()])]
[_ '()]))
(if (not prefix) '()
(map car (sort (get-all-completions-helper (cons #\space (string->list prefix))
'()
main-trie) #:key cdr >))));;
))
;
;(define (read-hist-word-list file-path #:pick? [choice 'word])
; (call-with-input-file file-path
; (lambda (fin)
; (for/list ([word-count (in-lines fin)])
; (let ([wc-split (string-split word-count #:trim? #t)])
; (match choice
; ['word (car wc-split)]
; ['counts (string->number (cadr wc-split))]
; ['both (cons (car wc-split)
; (string->number (cadr wc-split)))]))))))
; (define dictionary
; (read-hist-word-list "../google-books-common-words.txt"))
;
; (define dictionary
; (list "apple" "ant" "aloha" "always" "almight"))
(define my-trie (new prefix-tree%
[initial-word-list '()]))
;(define editor-trie (new prefix-tree% [initial-word-list '()
; ]))
;(define autocompletion-cursor<%>
; (interface ()
; get-completions ; -> (listof string)
; get-length ; -> int
; empty? ; -> boolean
; narrow ; char -> autocompletion-cursor<%>
; widen)) ; char -> autocompletion-cursor<%>
;
;
;;; string -> (values (string -> real) natural)
;;; produce a ranking function and a max normal score
;;; the ranking function is as follows:
;;; w |-> +inf.0 if `prefix' is a prefix of w
;;; w |-> 1000 if `prefix' appears in w
;;; w |-> n if n parts of `prefix' appear in w as first segments
;;; the max normal score is the largest n that the last clause can produce
;;(define (rank prefix)
;; (define splitters "[-/:_!]")
;; (define parts (regexp-split splitters prefix))
;; (define re (regexp (string-append "^" (regexp-quote prefix))))
;; (values (λ (w) (cond
;; [(regexp-match re w) +inf.0]
;; ;; it's a very good match prefix appears in the word
;; [(regexp-match (regexp-quote prefix) w) 1000]
;; ;; otherwise, we iterate and check each component of
;; [else
;; (for/fold ([c 0]) ([r parts])
;; (define rq (regexp-quote r))
;; (cond [(regexp-match (string-append "^" rq) w)
;; (+ 1 c)]
;; [(regexp-match (string-append splitters rq) w)
;; (+ 1 c)]
;; [else c]))]))
;; (length parts)))
;
;;; ============================================================
;;; autocompletion-cursor<%> implementation
;
;(define autocompletion-cursor%
; (class* object% (autocompletion-cursor<%>)
;
; (init-field word)
;
; ;(define-values (rnk max-count) (rank word))
; ;; this determines the fuzziness
; ;; if we set mx to +inf.0, we get just the prefix matches
; ;; if we set mx to 1000, we get just the matches somewhere in the word
; ;; this definition is fuzzier the more parts there are in the word
;; (define mx (cond
;; [(<= max-count 2) max-count]
;; [(<= max-count 4) (- max-count 1)]
;; [else (- max-count 2)]))
;;
; ;; all the possible completions for `word', in ranked order
; (define all-completions
; (send editor-trie get-all-completions word))
;
; (define all-completions-length (length all-completions))
;
; (define/public (narrow prefix)
; (set! word prefix)
; this)
;
; (define/public (widen prefix)
; (let ([strlen (string-length word)])
; (cond
; [(< strlen 2) #f]
; [else
; (set! word prefix)
; this])))
;; (new autocompletion-cursor%
;; [word (substring word 0 (- (string-length word) 1))]
;; [all-words all-words])])))
;;
; (define/public (get-completions) all-completions)
; (define/public (get-length) all-completions-length)
; (define/public (empty?) (eq? (get-length) 0))
;
; (super-new)))
;