Skip to content

Instantly share code, notes, and snippets.

@kencoba
Created December 10, 2010 15:55
Show Gist options
  • Save kencoba/736375 to your computer and use it in GitHub Desktop.
Save kencoba/736375 to your computer and use it in GitHub Desktop.
(trans-meisuu "千二百億十三")
(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]
(re-partition #"[零一二三四五六七八九十百千]+" s))
(def meisuu {"万" 4
"億" 8
"兆" 12
"京" 16
"垓" 20
"叙" 24
"穣" 28
"溝" 32
"澗" 36
"正" 40
"載" 44
"極" 48
"恒河沙" 52
"阿僧祇" 56
"那由多" 60
"不可思議" 64
"無量大数" 68})
(defn- trans-mei [s]
(loop [m (meisuu s) res 1]
(if (zero? m) res
(recur (dec m) (* res 10)))))
(defn- trans-meisuu-sub [lst]
(if (nil? lst) 0
(if (= (count lst) 1) (trans-keta (first lst))
(+ (* (trans-keta (first lst)) (trans-mei (second lst)))
(trans-meisuu-sub (next (next lst)))))))
(defn trans-meisuu [s]
(trans-meisuu-sub (next (split-keta s))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment