diff --git a/words/char-index.rkt b/words/char-index.rkt index 8b0dd81..17b0af8 100644 --- a/words/char-index.rkt +++ b/words/char-index.rkt @@ -1,6 +1,7 @@ #lang debug racket/base (require racket/file racket/fasl + racket/vector "words.rkt") (provide (all-defined-out)) @@ -28,15 +29,15 @@ #:when (bitwise-bit-set? int i)) (bitindex->char i))) -(define charidx-file "data/charidx.rktd") +(define charidx-file "compiled/charidx.rktd") (define (regenerate-char-index!) - (s-exp->fasl (map word->charidx usable-words) (open-output-file charidx-file #:exists 'replace))) + (s-exp->fasl (vector-map word->charidx usable-words) (open-output-file charidx-file #:exists 'replace))) (define charidx (let () (unless (file-exists? charidx-file) (regenerate-char-index!)) - (list->vector (fasl->s-exp (open-input-file charidx-file))))) + (fasl->s-exp (open-input-file charidx-file)))) (define (contains-char? charidx-entry c) (bitwise-bit-set? charidx-entry (char->bitindex c))) diff --git a/words/data/charidx.rktd b/words/data/charidx.rktd deleted file mode 100644 index 260d8b6..0000000 Binary files a/words/data/charidx.rktd and /dev/null differ diff --git a/words/data/lengthidx.rktd b/words/data/lengthidx.rktd deleted file mode 100644 index dbf3926..0000000 Binary files a/words/data/lengthidx.rktd and /dev/null differ diff --git a/words/data/wordidx.rktd b/words/data/wordidx.rktd deleted file mode 100644 index 31d7c9d..0000000 Binary files a/words/data/wordidx.rktd and /dev/null differ diff --git a/words/length-index.rkt b/words/length-index.rkt index 3af1a02..c862588 100644 --- a/words/length-index.rkt +++ b/words/length-index.rkt @@ -1,16 +1,17 @@ #lang debug racket/base (require racket/file racket/fasl + racket/vector "words.rkt") (provide (all-defined-out)) -(define lengthidx-file "data/lengthidx.rktd") +(define lengthidx-file "compiled/lengthidx.rktd") (define (regenerate-length-index!) - (s-exp->fasl (map string-length usable-words) (open-output-file lengthidx-file #:exists 'replace))) + (s-exp->fasl (vector-map string-length usable-words) (open-output-file lengthidx-file #:exists 'replace))) (define lengthidx (let () (unless (file-exists? lengthidx-file) (regenerate-length-index!)) - (list->vector (fasl->s-exp (open-input-file lengthidx-file))))) + (fasl->s-exp (open-input-file lengthidx-file)))) diff --git a/words/main.rkt b/words/main.rkt index 1cbfd86..bf1151e 100644 --- a/words/main.rkt +++ b/words/main.rkt @@ -15,8 +15,10 @@ #:max-words [max-words 10] #:all-caps [all-caps? #f] #:initial-caps [initial-caps? #f]) - (define mandatory-cs (if mandatory (string->list mandatory) null)) - (define letter-cs (append (if letters (string->list letters) null) mandatory-cs)) + (define mandatory-cs + (if mandatory (remove-duplicates (map char-downcase (string->list mandatory)) char=?) null)) + (define letter-cs (remove-duplicates (append (if letters (map char-downcase (string->list letters)) null) mandatory-cs) char=?)) + (define letter-cs-charidx (word->charidx (list->string letter-cs))) (for*/fold ([ws null] [count 0] #:result (map (cond @@ -31,13 +33,13 @@ #:when (and ;; between min and max length ((if (<= min-length max-length) <= >=) min-length w-lengthidx max-length) - ;; contains each mandatory, case-insensitive + ;; word contains each mandatory char, case-insensitive (or (not mandatory) - (for/or ([c (in-list (map char-downcase (charidx->chars w-charidx)))]) - (memv c mandatory-cs))) - ;; contains only letters + mandatory, case-insensitive - (for/and ([c (in-list (map char-downcase (charidx->chars w-charidx)))]) - (memv c letter-cs)) + (for/and ([mc (in-list mandatory-cs)]) + (w-charidx . contains-char? . mc))) + ;; word contains only letters + mandatory, case-insensitive + (for/and ([wc (in-list (map char-downcase (charidx->chars w-charidx)))]) + (letter-cs-charidx . contains-char? . wc)) ;; maybe only proper names (if proper-names? (capitalized? w-charidx) (not (capitalized? w-charidx))) ;; maybe hide plurals @@ -45,4 +47,7 @@ (values (cons w ws) (add1 count)))) (module+ test - (time (wordlist))) \ No newline at end of file + (require rackunit) + (time (wordlist)) + (check-equal? (sort (wordlist #:mandatory "xyz") stringfasl (make-wordlist) (open-output-file wordidx-file #:exists 'replace)))