-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rkt~
62 lines (51 loc) · 2.63 KB
/
test.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
#lang racket
(struct bknode (pdis value nodes) #:transparent)
(define bki (open-input-file "dic.txt"))
(define dictree (bknode 0 "correct" '()))
(define (make-tree)
(define word (read-line bki))
(cond [(not (eof-object? word))
(set! dictree (add dictree word))
(make-tree)]))
(define (add bktree word)
(define x (edit-distance word (bknode-value bktree)))
(cond [(nodes-present? x (bknode-nodes bktree))
(bknode (bknode-pdis bktree) (bknode-value bktree) (map (lambda (subtree)
(cond [(= (bknode-pdis subtree) x)
(add subtree word)]
[else subtree]))
(bknode-nodes bktree)))]
[else
(bknode (bknode-pdis bktree) (bknode-value bktree)
(cons (bknode x word '()) (bknode-nodes bktree)))]))
(define (nodes-present? val bnodes)
(cond [(null? bnodes) #f]
[(= val (bknode-pdis (car bnodes))) #t]
[else (nodes-present? val (cdr bnodes))]))
(define tol 4)
(define (find-matching-words word tree)
(define dis (edit-distance word (bknode-value tree)))
(cond [(<= dis tol)
(cons (bknode-value tree) (append* (map (lambda (bkn)
(cond [(<= (edit-distance (bknode-value bkn) word)
(+ dis tol))
(find-matching-words word bkn)]
[else '()]))
(bknode-nodes tree))))]
[else
(append* (map (lambda (bkn)
(define temp (edit-distance (bknode-value bkn) word))
(cond [(and (<= temp (+ dis tol))
(>= temp (- dis tol)))
(find-matching-words word bkn)]
[else '()]))
(bknode-nodes tree)))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (smart-correction word n)
(define x (find-matching-words word dictree))
(define one-list (lc z : z <- x @(equal? (edit-distance z word) 1)))
(define l (length one-list))
(if (>= l n) (first n one-list) (append one-list (first (- n l) x))))
(define (first n l)
(cond [(or (null? l) (= n 0)) '()]
[else (cons (car l) (first (- n 1) (cdr l)))]))