summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--spellcheck/dictionary.scm (renamed from spellcheck/hasher/dictionary.scm)5
-rw-r--r--spellcheck/mutate.scm43
-rw-r--r--spellcheck/spellcheck-test.scm29
3 files changed, 71 insertions, 6 deletions
diff --git a/spellcheck/hasher/dictionary.scm b/spellcheck/dictionary.scm
index 10ec3b8..856fec5 100644
--- a/spellcheck/hasher/dictionary.scm
+++ b/spellcheck/dictionary.scm
@@ -1,4 +1,4 @@
-(define-module (hasher dictionary)
+(define-module (dictionary)
#:export (create-dictionary
dict-occur-ref
dict-keys-ref
@@ -25,8 +25,7 @@ as the value."
(define (dict-occur-ref dict e)
"A wrapper for hashq-ref. Returns '0' if an element
is not present, rather than '#f'."
- (let ((occur (hashq-ref dict e)))
- (if occur occur 0)))
+ (or (hashq-ref dict e) 0))
(define (dict-keys-ref dict)
"Returns a list of all words in the dictionary."
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)))))
diff --git a/spellcheck/spellcheck-test.scm b/spellcheck/spellcheck-test.scm
index 8030c41..438384d 100644
--- a/spellcheck/spellcheck-test.scm
+++ b/spellcheck/spellcheck-test.scm
@@ -1,5 +1,6 @@
(use-modules (srfi srfi-64)
- (hasher dictionary))
+ (dictionary)
+ (mutate))
(define simple-dict (create-dictionary '("word")))
(define complex-dict (create-dictionary '("Again" "the" "thirst"
@@ -11,7 +12,7 @@
"heart" "of" "the" "snake"
"across" "the" "fire")))
-(test-begin "harness")
+(test-begin "dictionary")
(test-assert "create-dictionary returns hashtable"
@@ -54,4 +55,26 @@
(dict-values-ref complex-dict))
-(test-end "harness")
+(test-end "dictionary")
+
+(test-begin "mutate")
+
+
+(test-equal "test deletes six"
+ '("ix" "sx" "si")
+ (deletes "six"))
+
+(test-equal "test transposes"
+ '("isx" "sxi")
+ (transposes "six"))
+
+(test-equal "test replaces"
+ '("aix" "bix" "cix" "dix" "eix" "fix" "gix" "hix" "iix" "jix" "kix" "lix" "mix" "nix" "oix" "pix" "qix" "rix" "six" "tix" "uix" "vix" "wix" "xix" "yix" "zix" "sax" "sbx" "scx" "sdx" "sex" "sfx" "sgx" "shx" "six" "sjx" "skx" "slx" "smx" "snx" "sox" "spx" "sqx" "srx" "ssx" "stx" "sux" "svx" "swx" "sxx" "syx" "szx" "sia" "sib" "sic" "sid" "sie" "sif" "sig" "sih" "sii" "sij" "sik" "sil" "sim" "sin" "sio" "sip" "siq" "sir" "sis" "sit" "siu" "siv" "siw" "six" "siy" "siz")
+ (replaces "six"))
+
+(test-equal "test inserts"
+ '("asix" "bsix" "csix" "dsix" "esix" "fsix" "gsix" "hsix" "isix" "jsix" "ksix" "lsix" "msix" "nsix" "osix" "psix" "qsix" "rsix" "ssix" "tsix" "usix" "vsix" "wsix" "xsix" "ysix" "zsix" "saix" "sbix" "scix" "sdix" "seix" "sfix" "sgix" "shix" "siix" "sjix" "skix" "slix" "smix" "snix" "soix" "spix" "sqix" "srix" "ssix" "stix" "suix" "svix" "swix" "sxix" "syix" "szix" "siax" "sibx" "sicx" "sidx" "siex" "sifx" "sigx" "sihx" "siix" "sijx" "sikx" "silx" "simx" "sinx" "siox" "sipx" "siqx" "sirx" "sisx" "sitx" "siux" "sivx" "siwx" "sixx" "siyx" "sizx" "sixa" "sixb" "sixc" "sixd" "sixe" "sixf" "sixg" "sixh" "sixi" "sixj" "sixk" "sixl" "sixm" "sixn" "sixo" "sixp" "sixq" "sixr" "sixs" "sixt" "sixu" "sixv" "sixw" "sixx" "sixy" "sixz")
+ (inserts "six"))
+
+
+(test-end "mutate")