Skip to content

Instantly share code, notes, and snippets.

@hozumi
Forked from kencoba/meisuu.clj
Created December 13, 2010 06:23
Show Gist options
  • Save hozumi/738718 to your computer and use it in GitHub Desktop.
Save hozumi/738718 to your computer and use it in GitHub Desktop.
(use 'clojure.contrib.str-utils)
(def digits {"零" 0 "一" 1 "二" 2 "三" 3 "四" 4 "五" 5 "六" 6 "七" 7 "八" 8 "九" 9})
(defn- str-first [s]
(str (.charAt s 0)))
(defn- trans-ichi [s]
(let [n (re-find #"[一二三四五六七八九]$" s)]
(if (nil? n) 0 (digits n))))
(defn- trans-jyuu [s]
(try
(let [n (str-first (first (re-find #"([二三四五六七八九]十|十)" s)))]
(if (nil? n) 0
(let [d (digits n)]
(if (nil? d) 10
(* 10 d)))))
(catch NullPointerException _ 0)))
(defn- trans-hyaku [s]
(try
(let [n (str-first (first (re-find #"([二三四五六七八九]百|百)" s)))]
(if (nil? n) 0
(let [d (digits n)]
(if (nil? d) 100
(* 100 d)))))
(catch NullPointerException _ 0)))
(defn- trans-sen [s]
(try
(let [n (str-first (first (re-find #"([二三四五六七八九]千|千)" s)))]
(if (nil? n) 0
(let [d (digits n)]
(if (nil? d) 1000
(* 1000 d)))))
(catch NullPointerException _ 0)))
(defn- trans-keta [s]
(+ (trans-sen s) (trans-hyaku s) (trans-jyuu s) (trans-ichi s)))
(defn- split-keta [s]
(partition-all 2 (rest (re-partition #"[零一二三四五六七八九十百千]+" s))))
;;(split-keta "十億二千八百五")
;;=>(("十" "億") ("二千八百五"))
(defn- bekijou [a n]
(reduce * (repeat n a)))
;;(bekijou 10 0)
;;=>1
;;(bekijou 10 1)
;;=>10
;;(bekijou 10 2)
;;=>100
(def meisuu {"万" 4
"億" 8
"兆" 12
"京" 16
"垓" 20
"叙" 24
"穣" 28
"溝" 32
"澗" 36
"正" 40
"載" 44
"極" 48
"恒河沙" 52
"阿僧祇" 56
"那由多" 60
"不可思議" 64
"無量大数" 68})
(defn kanji->number [[kazu tanni]]
(* (trans-keta kazu) (bekijou 10 (meisuu tanni 0))))
;;(kanji->number ["二十三" "億"])
;;=>2300000000
;;(kanji->number ["二千八百五"])
;;=>2805
(defn trans-meisuu [s]
(reduce + (map kanji->number (split-keta s))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment