nani/website/tools/jmdict.scm

jmdict.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 jmdict 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
(define (convert input output)
29
  (let ((sxml (load-dic input)))
30
    (call-with-output-file output
31
      (lambda (port)
32
        (write sxml port)))))
33
34
(define (compile input sense-filter output)
35
  (let* ((frq (load-frequency "dictionaries/frequency.tsv"))
36
         (sxml (if (equal? (substring input (- (string-length input) 4)) ".xml")
37
                 (load-dic input)
38
                 (call-with-input-file input read)))
39
         (results (sxml->results sxml frq))
40
         (results (map (lambda (result)
41
                         (update-result
42
                           result
43
                           #:senses (filter sense-filter
44
                                            (result-senses result))))
45
                       results))
46
         (results (filter (lambda (result) (not (null? (result-senses result))))
47
                          results))
48
         (kanji-trie (compress-trie (make-kanji-trie results)))
49
         (reading-trie (compress-trie (make-reading-trie results)))
50
         (meaning-trie (compress-trie (make-meaning-trie results))))
51
    (format #t "Number of entries in ~a: ~a~%" output (length results))
52
    (call-with-output-file output
53
      (lambda (port)
54
        (put-bytevector port
55
          (serialize-jmdict results kanji-trie reading-trie meaning-trie))))))
56
57
(match (command-line)
58
  ((_ cmd input lang output)
59
   (cond
60
    ((equal? cmd "build")
61
     (if (equal? lang "e")
62
       (compile input (const #t) output)
63
       (compile input (lambda (sense) (equal? (sense-language sense) lang)) output)))
64
    ((equal? cmd "convert")
65
     (convert input output))
66
    (else (format #t "Unknown cmd ~a.~%" cmd)))))
67