summaryrefslogtreecommitdiff
path: root/spellcheck/hasher/dictionary.scm
blob: 10ec3b825a57fa3e73f71f507a0cca15eee45bed (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
(define-module (hasher dictionary)
  #:export (create-dictionary
            dict-occur-ref
            dict-keys-ref
            dict-values-ref))

(define (create-dictionary lst)
  (dictionary-helper (make-hash-table) lst))

(define (dictionary-helper dict rest)
  "Recursively adds words to DICT."
  (if (null? rest)
      dict
      (begin
        (dict-inc-word dict (car rest))
        (dictionary-helper dict (cdr rest)))))

(define (dict-inc-word dict e)
  "Inserts word E into the DICT, using E
as a key and the running total of occurances
as the value."
  (hashq-set!
   dict e (1+ (dict-occur-ref dict e))))

(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)))

(define (dict-keys-ref dict)
  "Returns a list of all words in the dictionary."
  (map car (hash-map->list cons dict)))

(define (dict-values-ref dict)
  "Sums up the total number of occurances for all words."
  (apply + (map cdr (hash-map->list cons dict))))