nani/website/tools/wadoku.scm

wadoku.scm

1
;;; Nani Project website
2
;;; Copyright © 2019 Julien Lepiller <julien@lepiller.eu>
3
;;;
4
;;; This file is part of the Nani Project website.
5
;;;
6
;;; The Nani Project website is free software; you can redistribute it and/or modify it
7
;;; under the terms of the GNU Affero General Public License as published by
8
;;; the Free Software Foundation; either version 3 of the License, or (at
9
;;; your option) any later version.
10
;;;
11
;;; The Nani Project website is distributed in the hope that it will be useful, but
12
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
;;; GNU Affero General Public License for more details.
15
;;;
16
;;; You should have received a copy of the GNU Affero General Public License
17
;;; along with the Nani Project website.  If not, see <http://www.gnu.org/licenses/>.
18
19
(use-modules (nani jmdict trie))
20
(use-modules (nani jmdict serialize))
21
(use-modules (nani wadoku xml))
22
(use-modules (nani frequency))
23
(use-modules (nani trie))
24
(use-modules (nani result))
25
(use-modules (ice-9 match))
26
(use-modules (ice-9 binary-ports))
27
28
;; Break these steps to try and let the GC reclaim these big objects
29
(define (get-results1 input frq)
30
  (call-with-input-file input
31
    (lambda (port)
32
      (xml->results port frq))))
33
34
(define (get-results input sense-filter frq)
35
  (let* ((results (get-results1 input frq))
36
         (results (map (lambda (result)
37
                         (update-result
38
                           result
39
                           #:senses (filter sense-filter
40
                                            (result-senses result))))
41
                       results))
42
         (results (filter (lambda (result) (not (null? (result-senses result))))
43
                          results)))
44
    (pk (car results))
45
    (let ((readings (apply append (map result-readings results))))
46
      (pk (apply append (map reading-readings readings))))
47
    results))
48
49
(define (compile input sense-filter output)
50
  (let* ((results (get-results input sense-filter
51
                               (load-frequency "dictionaries/frequency.tsv")))
52
         (kanji-trie (compress-trie (make-kanji-trie results)))
53
         (reading-trie (compress-trie (make-reading-trie results)))
54
         (meaning-trie (compress-trie (make-meaning-trie results))))
55
    (format #t "Number of entries in ~a: ~a~%" output (length results))
56
    (call-with-output-file output
57
      (lambda (port)
58
        (put-bytevector port
59
          (serialize-jmdict results kanji-trie reading-trie meaning-trie))))))
60
61
(define (print word dict)
62
  #t)
63
64
(match (command-line)
65
  ((_ cmd input lang output)
66
   (cond
67
    ((equal? cmd "build")
68
     (if (equal? lang "e")
69
       (compile input (const #t) output)
70
       (compile input (lambda (sense) (equal? (sense-language sense) lang)) output)))
71
    ((equal? cmd "convert")
72
     (convert input output))
73
    (else (format #t "Unknown cmd ~a.~%" cmd))))
74
  ((_ "print" word input)
75
   (print word input)))
76