model.scm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. (use-modules
  2. (json)
  3. (srfi srfi-1) ;; union and other set operations
  4. (srfi srfi-9) ;; structs
  5. (srfi srfi-13) ;; hash table procedures
  6. (srfi srfi-9 gnu) ;; for functional structs (not part of srfi-9 directly)
  7. (ice-9 hash-table)) ;; hash tables
  8. ;; ========================
  9. ;; VOCABULARY ITEM METADATA
  10. ;; ========================
  11. (define-immutable-record-type <item-metadata>
  12. (make-item-metadata learned
  13. description
  14. relevance
  15. difficulty
  16. similar-words
  17. mnemonics
  18. explanations
  19. tags
  20. usage-examples
  21. #;id)
  22. ;; predicate
  23. item-metadata?
  24. ;; getters & setters
  25. (learned get-item-metadata-learned
  26. set-item-metadata-learned)
  27. (description get-item-metadata-description
  28. set-item-metadata-description)
  29. (relevance get-item-metadata-relevance
  30. set-item-metadata-relevance)
  31. (difficulty get-item-metadata-difficulty
  32. set-item-metadata-difficulty)
  33. (similar-words get-item-metadata-similar-words
  34. set-item-metadata-similar-words)
  35. (mnemonics get-item-metadata-mnemonics
  36. set-item-metadata-mnemonics)
  37. (explanations get-item-metadata-explanations
  38. set-item-metadata-explanations)
  39. (tags get-item-metadata-tags
  40. set-item-metadata-tags)
  41. (usage-examples get-item-metadata-usage-examples
  42. set-item-metadata-usage-examples)
  43. #;(id get-item-metadata-id
  44. set-item-metadata-id))
  45. (set-record-type-printer!
  46. <item-metadata>
  47. (λ (record port)
  48. (display
  49. (simple-format
  50. port "~a\n"
  51. (string-join
  52. '("item-metadata:"
  53. (string-append
  54. "<"
  55. (string-join
  56. (list
  57. #;(simple-format port "id: ~a" (item-metadata-id record))
  58. (simple-format port "learned: ~a" (item-metadata-learned record))
  59. (simple-format port "description: ~a" (item-metadata-description record))
  60. (simple-format port "relevance: ~a" (item-metadata-relevance record))
  61. (simple-format port "difficulty: ~a" (item-metadata-difficulty record))
  62. (simple-format port "similar-words: ~a" (item-metadata-similar-words record))
  63. (simple-format port "mnemonics: ~a" (item-metadata-mnemonics record))
  64. (simple-format port "explanations: ~a" (item-metadata-explanations record))
  65. (simple-format port "tags: ~a" (item-metadata-tags record))
  66. (simple-format port "usage-examples: ~a" (item-metadata-usage-examples record)))
  67. " ")
  68. ">")
  69. )
  70. " ")))))
  71. (define (make-default-item-metadata vocabulary)
  72. (make-item-metadata #f ;; learned
  73. "" ;; description
  74. 0 ;; relevance
  75. 0 ;; difficulty
  76. #() ;; similar_words
  77. #() ;; mnemonics
  78. #() ;; explanations
  79. #() ;; tags
  80. #())) ;; usage_examples
  81. ;; ============================
  82. ;; TRANSLATION DATA ABSTRACTION
  83. ;; ============================
  84. (define (make-translation-data alist)
  85. (alist->hash-table alist))
  86. (define (translation-data->alist translation-data)
  87. (hash-map->list cons m))
  88. (define (get-translations translation-data lang)
  89. (hash-ref translation-data lang))
  90. (define (set-translation! translation-data lang translation)
  91. (hash-set! translation-data lang translation))
  92. (define (add-translation! translation-data lang translation)
  93. (define (merge-translations translation-lst translation)
  94. (list->vector
  95. (lset-union eq?
  96. translation-lst
  97. translation)))
  98. (let ([prev-translations (hash-ref translation-data lang #f)])
  99. (cond [prev-translations
  100. (hash-set! translation-data
  101. lang
  102. (merge-translations (vector->list prev-translations) (list translation)))]
  103. [else
  104. (hash-set! translation-data
  105. lang
  106. (vector translation))])))
  107. (define-immutable-record-type <vocabulary-item>
  108. ;; define constructor
  109. (make-vocabulary-item metadata translation-data)
  110. ;; define predicate
  111. vocabulary-item?
  112. ;; define accessors and functional setters
  113. (metadata get-vocabulary-item-metadata set-vocabulary-item-metadata)
  114. (translation-data get-translation-data set-translation-data))
  115. (set-record-type-printer!
  116. <vocabulary-item>
  117. (λ (record port)
  118. (display
  119. (simple-format port
  120. "vocabulary-item: <~a ~a>\n"
  121. (get-vocabulary-item-metadata record)
  122. (get-translation-data record)))))
  123. (define-immutable-record-type <vocabulary-metadata>
  124. ;; define constructor
  125. (make-vocabulary-metadata ...)
  126. ;; define predicate
  127. vocabulary-metadata?
  128. ;; define accessors and functional setters
  129. (identifier get-vocabulary-metadata-identifier set-vocabulary-metadata-identifier)
  130. (source-note get-vocabulary-metadata-source-note set-vocabulary-metadata-source-note))
  131. (set-record-type-printer!
  132. <vocabulary-metadata>
  133. (λ (record port)
  134. (display
  135. (simple-format port
  136. "vocabulary-item: <~a ~a>\n"
  137. (get-vocabulary-metadata-identifier record)
  138. (get-vocabulary-metadata-source-note record)))))
  139. (define-immutable-record-type <vocabulary>
  140. ;; define constructor
  141. (make-vocabulary metadata items)
  142. ;; define predicate
  143. vocabulary?
  144. ;; define accessors and functional setters
  145. (metadata get-vocabulary-metadata set-vocabulary-metadata)
  146. (items get-vocabulary-items set-vocabulary-items))
  147. (set-record-type-printer!
  148. <vocabulary>
  149. (λ (record port)
  150. (display
  151. (simple-format port
  152. "vocabulary-item: <~a ~a>\n"
  153. (get-vocabulary-metadata record)
  154. (get-vocabulary-items record)))))