model.scm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. (library (model)
  2. (export scm->vocabulary
  3. make-vocabulary
  4. vocabulary?
  5. vocabulary-metadata
  6. vocabulary-words
  7. vocabulary-shuffle
  8. vocabulary-filter)
  9. (import (except (rnrs base) vector-for-each)
  10. (only (guile)
  11. lambda* λ
  12. display
  13. simple-format
  14. write-char)
  15. (only (srfi srfi-9 gnu)
  16. set-record-type-printer!)
  17. (srfi srfi-43)
  18. (ice-9 ports)
  19. (ice-9 pretty-print)
  20. (json)
  21. (vector-utils)
  22. (only (words) filter-vocabulary-words))
  23. (define-json-type vocabulary
  24. (metadata)
  25. (words))
  26. (set-record-type-printer!
  27. vocabulary
  28. (lambda (record port)
  29. (display "<vocabulary:\n" port)
  30. (display
  31. (with-output-to-string
  32. (λ ()
  33. (pretty-print (vocabulary-metadata record)
  34. #:width 80
  35. #:display? #t)))
  36. port)
  37. (vector-for-each (λ (ind word)
  38. (display
  39. (with-output-to-string
  40. (λ ()
  41. (pretty-print word #:width 80 #:display? #t)))
  42. port))
  43. (vocabulary-words record))
  44. (display ">\n" port)))
  45. (define vocabulary-shuffle
  46. (λ (vocabulary)
  47. (make-vocabulary
  48. (vocabulary-metadata vocabulary)
  49. (vector-shuffle (vocabulary-words vocabulary)))))
  50. (define vocabulary-filter
  51. (λ (vocabulary . word-filters)
  52. "Take a VOCABULARY and any number of WORD-FILTERS. A
  53. word-filter is a function, which filters a vector of words."
  54. (make-vocabulary
  55. (vocabulary-metadata vocabulary)
  56. (apply filter-vocabulary-words
  57. (vocabulary-words vocabulary)
  58. word-filters)))))