statistics.scm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. (library (statistics)
  2. (export display-statistics
  3. display-learned-statistic)
  4. (import (except (rnrs base) vector-for-each)
  5. (only (guile)
  6. lambda* λ
  7. simple-format
  8. current-output-port
  9. current-input-port
  10. exact->inexact
  11. sort)
  12. ;; GNU Guile batteries
  13. (ice-9 exceptions)
  14. (ice-9 match)
  15. (ice-9 format)
  16. ;; custom libraries
  17. (vocabulary-data)
  18. (alist-procs)
  19. ;; custom helper libraries
  20. ;; SRFIs
  21. (srfi srfi-1)
  22. ;; SRFI 43 - vector procs
  23. (srfi srfi-43)
  24. ;; SRFI 69 - hash tables
  25. (srfi srfi-69)
  26. (vector-procs)))
  27. (define rest cdr)
  28. ;; NOTE: Calculating statistics might be a good candidate
  29. ;; for using fibers, to calculate all of the statistics in
  30. ;; separate fibers and speed up the calculation.
  31. (define display-learned-statistic
  32. (lambda* (vocabulary
  33. #:key
  34. (input-port (current-input-port))
  35. (output-port (current-output-port)))
  36. "Display statistics about the given VOCABULARY learned attribute."
  37. (let ([learned-vocabulary
  38. (vocabulary-filter (λ (entry)
  39. (get:entry/attribute entry '("metadata" "learned")))
  40. vocabulary)])
  41. (let ([num-learned
  42. (vector-length (get:vocabulary/entries learned-vocabulary))]
  43. [num-total
  44. (vector-length (get:vocabulary/entries vocabulary))])
  45. (simple-format output-port
  46. "learned vocabulary:\n ~a of ~a (~a%)\n"
  47. num-learned
  48. num-total
  49. (format #f
  50. "~,2f"
  51. (exact->inexact (* (/ 100 num-total) num-learned))))))))
  52. (define display-tags-count-statistic
  53. (lambda* (vocabulary
  54. #:key
  55. (input-port (current-input-port))
  56. (output-port (current-output-port)))
  57. "Display statistics about the count for each tag in the
  58. given VOCABULARY."
  59. (simple-format output-port "tag counts:\n")
  60. (let ([tag-count-table (make-hash-table)]
  61. [entries (get:vocabulary/entries vocabulary)])
  62. (vector-for-each
  63. (λ (ind entry)
  64. (let ([tags-attr (get:entry/attribute entry '("metadata" "tags"))])
  65. (vector-for-each
  66. (λ (ind tag)
  67. (hash-table-update!/default tag-count-table
  68. tag
  69. (λ (prev-val) (+ prev-val 1))
  70. 0))
  71. tags-attr)))
  72. entries)
  73. (let ([sorted-tag-alist
  74. (sort (hash-table->alist tag-count-table)
  75. (λ (tag-and-count-1 tag-and-count-2)
  76. (< (alist-item-value tag-and-count-1)
  77. (alist-item-value tag-and-count-2))))])
  78. ;; Using named let looping construct, to not rely on
  79. ;; unspecified order of map going through a list.
  80. (let iter ([remaining-tags-and-counts sorted-tag-alist])
  81. (cond
  82. [(null? remaining-tags-and-counts)
  83. (simple-format output-port "\n")]
  84. [else
  85. (let* ([current-tag-and-count (first remaining-tags-and-counts)]
  86. [key (alist-item-key current-tag-and-count)]
  87. [val (alist-item-value current-tag-and-count)])
  88. (simple-format output-port " ~a: ~a\n" key val))
  89. (iter (rest remaining-tags-and-counts))]))))))
  90. (define display-statistics
  91. (lambda* (vocabulary
  92. #:key
  93. (input-port (current-input-port))
  94. (output-port (current-output-port)))
  95. "Display statistics about the given VOCABULARY."
  96. (display-learned-statistic vocabulary
  97. #:input-port input-port
  98. #:output-port output-port)
  99. (display-tags-count-statistic vocabulary
  100. #:input-port input-port
  101. #:output-port output-port)))