From 6b40eb033d9c0bda4006f23426c34ef8fda4a0d5 Mon Sep 17 00:00:00 2001 From: bd Date: Fri, 24 May 2024 18:21:35 -0600 Subject: Add partial "mutate" implementation, failing tests --- spellcheck/mutate.scm | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 spellcheck/mutate.scm (limited to 'spellcheck/mutate.scm') diff --git a/spellcheck/mutate.scm b/spellcheck/mutate.scm new file mode 100644 index 0000000..117d0b3 --- /dev/null +++ b/spellcheck/mutate.scm @@ -0,0 +1,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))))) -- cgit v1.2.3