summaryrefslogtreecommitdiff
path: root/spellcheck/mutate.scm
blob: 117d0b3cf0d9112f9ddea4e1af13792f7b4268a7 (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
(define-module (mutate)
  #:use-module (srfi srfi-1)
  #:export (deletes
            transposes
            replaces
            inserts))


;; see https://norvig.com/spell-correct.html
;; this is a translation of that from python
;; to guile
(define (deletes word)
  (define (deletes-helper word splits result)
    (if (null? splits)
	result
	(let* ((split (car splits))
	       (tail (cdr split)))
	  (if (zero? (string-length tail))
	      (deletes-helper word (cdr splits) result)
	      (deletes-helper word (cdr splits)
			      (append result
				      (list (string-append (car split) (substring tail 1)))))))))
  (deletes-helper word (splits word) '()))

(define (transposes word)
  #t)

(define (replaces word)
  #t)

(define (inserts word)
  #t)


(define (splits word)
  "Given WORD, returns a list of pairs of
all possible splits:

'six' ->
(('' . 'six') ('s' . 'ix') ('si' . 'x') ('six' . ''))"
  (map (lambda (s) (cons (substring word 0 s)
			 (substring word s)))
       (iota (1+ (string-length word)))))