aboutsummaryrefslogtreecommitdiffstats
path: root/jp-hash.tl
blob: 70f70c63f2a64a3ca6091719a4115c84e76df4db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env txrlisp

;; One-Clause BSD License ("1BSD")
;;
;; Copyright 2022  Kaz Kylheku <kaz@kylheku.com>
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following condition is
;; met:
;;
;; 1. The source code distribution retains the above copyright notice,
;;    this condition, and the following disclaimer.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
;; FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
;; COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
;; BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
;; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
;; CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
;; ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;; POSSIBILITY OF SUCH DAMAGE.

(defvarl vowels '#"a i u e o")
(defvarl diphthongs '#"ya yu yo")
(defvarl consonants '#"k g s z t d n h p b m r")
(defvarl symbols '#"! # @ $ % ^ & * ? /")

(defvarl mora-fix (let ((trie (make-trie)))
                    (mapdo (op trie-add trie)
                           '#"si  sy zi zy ti  tu  ty di du dy hu"
                           '#"shi sh ji j  chi tsu ch ji zu j  fu")
                    trie))

(defvar mora (flow (append vowels diphthongs '#"wa"
                           [maprod join consonants vowels]
                           [maprod join consonants diphthongs])
               (filter-string-tree mora-fix)
               uniq))

(defun choose (list index)
  [list (mod index (len list))])

(defun jp-hash (phrase)
  (let* ((buf (sha256 phrase))
         (word (carray-buf buf (ffi le-uint16)))
         (ms (mapcar (opip word (choose mora)) 0..6))
         (dig (list (pic "#" (mod [word 6] 10))))
         (sym (list (choose symbols [word 7]))))
    (upd [ms 0] copy)
    (upd [[ms 0] 0] chr-toupper)
    (cat-str (caseq (logand [word 8] 7)
               (0 (append [ms 0..3] sym [ms 3..6] dig))
               (1 (append sym [ms 0..3] dig [ms 3..6]))
               (2 (append [ms 0..2] sym [ms 2..4] dig [ms 4..6]))
               (3 (append [ms 0..2] dig [ms 2..4] sym [ms 4..6]))
               (4 (append [ms 0..3] '#"n" sym [ms 3..6] dig))
               (5 (append sym [ms 0..3] dig [ms 3..6] '#"n"))
               (6 (append [ms 0..2] '#"n" sym [ms 2..4] dig [ms 4..6]))
               (7 (append [ms 0..2] dig [ms 2..4] sym [ms 4..6] '#"n"))))))

(compile-only
  (match-case *args*
    ((@arg) (put-line (jp-hash arg)))
    (@else (put-line `@{self-path}: one argument required`))))