Spaces:
Sleeping
Sleeping
Delete num2kana.py
Browse files- num2kana.py +0 -317
num2kana.py
DELETED
|
@@ -1,317 +0,0 @@
|
|
| 1 |
-
# https://github.com/Greatdane/Convert-Numbers-to-Japanese/blob/master/Convert-Numbers-to-Japanese.py
|
| 2 |
-
# Japanese Number Converter
|
| 3 |
-
# - Currently using length functions - possible to use Recursive Functions? Difficult with the many exceptions
|
| 4 |
-
# - Works up to 9 figures (999999999)
|
| 5 |
-
|
| 6 |
-
romaji_dict = {".": "ten", "0": "zero", "1": "ichi", "2": "ni", "3": "san", "4": "yon", "5": "go", "6": "roku", "7": "nana",
|
| 7 |
-
"8": "hachi", "9": "kyuu", "10": "juu", "100": "hyaku", "1000": "sen", "10000": "man", "100000000": "oku",
|
| 8 |
-
"300": "sanbyaku", "600": "roppyaku", "800": "happyaku", "3000": "sanzen", "8000":"hassen", "01000": "issen"}
|
| 9 |
-
|
| 10 |
-
kanji_dict = {".": "点", "0": "零", "1": "一", "2": "二", "3": "三", "4": "四", "5": "五", "6": "六", "7": "七",
|
| 11 |
-
"8": "八", "9": "九", "10": "十", "100": "百", "1000": "千", "10000": "万", "100000000": "億",
|
| 12 |
-
"300": "三百", "600": "六百", "800": "八百", "3000": "三千", "8000":"八千", "01000": "一千"}
|
| 13 |
-
|
| 14 |
-
hiragana_dict = {".": "てん", "0": "ゼロ", "1": "いち", "2": "に", "3": "さん", "4": "よん", "5": "ご", "6": "ろく", "7": "なな",
|
| 15 |
-
"8": "はち", "9": "きゅう", "10": "じゅう", "100": "ひゃく", "1000": "せん", "10000": "まん", "100000000": "おく",
|
| 16 |
-
"300": "さんびゃく", "600": "ろっぴゃく", "800": "はっぴゃく", "3000": "さんぜん", "8000":"はっせん", "01000": "いっせん" }
|
| 17 |
-
|
| 18 |
-
key_dict = {"kanji" : kanji_dict, "hiragana" : hiragana_dict, "romaji": romaji_dict}
|
| 19 |
-
|
| 20 |
-
def len_one(convert_num,requested_dict):
|
| 21 |
-
# Returns single digit conversion, 0-9
|
| 22 |
-
return requested_dict[convert_num]
|
| 23 |
-
|
| 24 |
-
def len_two(convert_num,requested_dict):
|
| 25 |
-
# Returns the conversion, when number is of length two (10-99)
|
| 26 |
-
if convert_num[0] == "0": #if 0 is first, return len_one
|
| 27 |
-
return len_one(convert_num[1],requested_dict)
|
| 28 |
-
if convert_num == "10":
|
| 29 |
-
return requested_dict["10"] # Exception, if number is 10, simple return 10
|
| 30 |
-
if convert_num[0] == "1": # When first number is 1, use ten plus second number
|
| 31 |
-
return requested_dict["10"] + " " + len_one(convert_num[1],requested_dict)
|
| 32 |
-
elif convert_num[1] == "0": # If ending number is zero, give first number plus 10
|
| 33 |
-
return len_one(convert_num[0],requested_dict) + " " + requested_dict["10"]
|
| 34 |
-
else:
|
| 35 |
-
num_list = []
|
| 36 |
-
for x in convert_num:
|
| 37 |
-
num_list.append(requested_dict[x])
|
| 38 |
-
num_list.insert(1, requested_dict["10"])
|
| 39 |
-
# Convert to a string (from a list)
|
| 40 |
-
output = ""
|
| 41 |
-
for y in num_list:
|
| 42 |
-
output += y + " "
|
| 43 |
-
output = output[:len(output) - 1] # take off the space
|
| 44 |
-
return output
|
| 45 |
-
|
| 46 |
-
def len_three(convert_num,requested_dict):
|
| 47 |
-
# Returns the conversion, when number is of length three (100-999)
|
| 48 |
-
num_list = []
|
| 49 |
-
if convert_num[0] == "1":
|
| 50 |
-
num_list.append(requested_dict["100"])
|
| 51 |
-
elif convert_num[0] == "3":
|
| 52 |
-
num_list.append(requested_dict["300"])
|
| 53 |
-
elif convert_num[0] == "6":
|
| 54 |
-
num_list.append(requested_dict["600"])
|
| 55 |
-
elif convert_num[0] == "8":
|
| 56 |
-
num_list.append(requested_dict["800"])
|
| 57 |
-
else:
|
| 58 |
-
num_list.append(requested_dict[convert_num[0]])
|
| 59 |
-
num_list.append(requested_dict["100"])
|
| 60 |
-
if convert_num[1:] == "00" and len(convert_num) == 3:
|
| 61 |
-
pass
|
| 62 |
-
else:
|
| 63 |
-
if convert_num[1] == "0":
|
| 64 |
-
num_list.append(requested_dict[convert_num[2]])
|
| 65 |
-
else:
|
| 66 |
-
num_list.append(len_two(convert_num[1:], requested_dict))
|
| 67 |
-
output = ""
|
| 68 |
-
for y in num_list:
|
| 69 |
-
output += y + " "
|
| 70 |
-
output = output[:len(output) - 1]
|
| 71 |
-
return output
|
| 72 |
-
|
| 73 |
-
def len_four(convert_num,requested_dict, stand_alone):
|
| 74 |
-
# Returns the conversion, when number is of length four (1000-9999)
|
| 75 |
-
num_list = []
|
| 76 |
-
# First, check for zeros (and get deal with them)
|
| 77 |
-
if convert_num == "0000":
|
| 78 |
-
return ""
|
| 79 |
-
while convert_num[0] == "0":
|
| 80 |
-
convert_num = convert_num[1:]
|
| 81 |
-
if len(convert_num) == 1:
|
| 82 |
-
return len_one(convert_num,requested_dict)
|
| 83 |
-
elif len(convert_num) == 2:
|
| 84 |
-
return len_two(convert_num,requested_dict)
|
| 85 |
-
elif len(convert_num) == 3:
|
| 86 |
-
return len_three(convert_num,requested_dict)
|
| 87 |
-
# If no zeros, do the calculation
|
| 88 |
-
else:
|
| 89 |
-
# Have to handle 1000, depending on if its a standalone 1000-9999 or included in a larger number
|
| 90 |
-
if convert_num[0] == "1" and stand_alone:
|
| 91 |
-
num_list.append(requested_dict["1000"])
|
| 92 |
-
elif convert_num[0] == "1":
|
| 93 |
-
num_list.append(requested_dict["01000"])
|
| 94 |
-
elif convert_num[0] == "3":
|
| 95 |
-
num_list.append(requested_dict["3000"])
|
| 96 |
-
elif convert_num[0] == "8":
|
| 97 |
-
num_list.append(requested_dict["8000"])
|
| 98 |
-
else:
|
| 99 |
-
num_list.append(requested_dict[convert_num[0]])
|
| 100 |
-
num_list.append(requested_dict["1000"])
|
| 101 |
-
if convert_num[1:] == "000" and len(convert_num) == 4:
|
| 102 |
-
pass
|
| 103 |
-
else:
|
| 104 |
-
if convert_num[1] == "0":
|
| 105 |
-
num_list.append(len_two(convert_num[2:],requested_dict))
|
| 106 |
-
else:
|
| 107 |
-
num_list.append(len_three(convert_num[1:],requested_dict))
|
| 108 |
-
output = ""
|
| 109 |
-
for y in num_list:
|
| 110 |
-
output += y + " "
|
| 111 |
-
output = output[:len(output) - 1]
|
| 112 |
-
return output
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
def len_x(convert_num,requested_dict):
|
| 116 |
-
#Returns everything else.. (up to 9 digits)
|
| 117 |
-
num_list = []
|
| 118 |
-
if len(convert_num[0:-4]) == 1:
|
| 119 |
-
num_list.append(requested_dict[convert_num[0:-4]])
|
| 120 |
-
num_list.append(requested_dict["10000"])
|
| 121 |
-
elif len(convert_num[0:-4]) == 2:
|
| 122 |
-
num_list.append(len_two(convert_num[0:2],requested_dict))
|
| 123 |
-
num_list.append(requested_dict["10000"])
|
| 124 |
-
elif len(convert_num[0:-4]) == 3:
|
| 125 |
-
num_list.append(len_three(convert_num[0:3],requested_dict))
|
| 126 |
-
num_list.append(requested_dict["10000"])
|
| 127 |
-
elif len(convert_num[0:-4]) == 4:
|
| 128 |
-
num_list.append(len_four(convert_num[0:4],requested_dict, False))
|
| 129 |
-
num_list.append(requested_dict["10000"])
|
| 130 |
-
elif len(convert_num[0:-4]) == 5:
|
| 131 |
-
num_list.append(requested_dict[convert_num[0]])
|
| 132 |
-
num_list.append(requested_dict["100000000"])
|
| 133 |
-
num_list.append(len_four(convert_num[1:5],requested_dict, False))
|
| 134 |
-
if convert_num[1:5] == "0000":
|
| 135 |
-
pass
|
| 136 |
-
else:
|
| 137 |
-
num_list.append(requested_dict["10000"])
|
| 138 |
-
else:
|
| 139 |
-
assert False, "Not yet implemented, please choose a lower number."
|
| 140 |
-
num_list.append(len_four(convert_num[-4:],requested_dict, False))
|
| 141 |
-
output = ""
|
| 142 |
-
for y in num_list:
|
| 143 |
-
output += y + " "
|
| 144 |
-
output = output[:len(output) - 1]
|
| 145 |
-
return output
|
| 146 |
-
|
| 147 |
-
def remove_spaces(convert_result):
|
| 148 |
-
# Remove spaces in Hirigana and Kanji results
|
| 149 |
-
correction = ""
|
| 150 |
-
for x in convert_result:
|
| 151 |
-
if x == " ":
|
| 152 |
-
pass
|
| 153 |
-
else:
|
| 154 |
-
correction += x
|
| 155 |
-
return correction
|
| 156 |
-
|
| 157 |
-
def do_convert(convert_num,requested_dict):
|
| 158 |
-
#Check lengths and convert accordingly
|
| 159 |
-
if len(convert_num) == 1:
|
| 160 |
-
return(len_one(convert_num,requested_dict))
|
| 161 |
-
elif len(convert_num) == 2:
|
| 162 |
-
return(len_two(convert_num,requested_dict))
|
| 163 |
-
elif len(convert_num) == 3:
|
| 164 |
-
return(len_three(convert_num,requested_dict))
|
| 165 |
-
elif len(convert_num) == 4:
|
| 166 |
-
return(len_four(convert_num,requested_dict, True))
|
| 167 |
-
else:
|
| 168 |
-
return(len_x(convert_num,requested_dict))
|
| 169 |
-
|
| 170 |
-
def split_Point(split_num,dict_choice):
|
| 171 |
-
# Used if a decmial point is in the string.
|
| 172 |
-
split_num = split_num.split(".")
|
| 173 |
-
split_num_a = split_num[0]
|
| 174 |
-
split_num_b = split_num[1]
|
| 175 |
-
split_num_b_end = " "
|
| 176 |
-
for x in split_num_b:
|
| 177 |
-
split_num_b_end += len_one(x,key_dict[dict_choice]) + " "
|
| 178 |
-
# To account for small exception of small tsu when ending in jyuu in hiragana/romaji
|
| 179 |
-
if split_num_a[-1] == "0" and split_num_a[-2] != "0" and dict_choice == "hiragana":
|
| 180 |
-
small_Tsu = Convert(split_num_a,dict_choice)
|
| 181 |
-
small_Tsu = small_Tsu[0:-1] + "っ"
|
| 182 |
-
return small_Tsu + key_dict[dict_choice]["."] + split_num_b_end
|
| 183 |
-
if split_num_a[-1] == "0" and split_num_a[-2] != "0" and dict_choice == "romaji":
|
| 184 |
-
small_Tsu = Convert(split_num_a,dict_choice)
|
| 185 |
-
small_Tsu = small_Tsu[0:-1] + "t"
|
| 186 |
-
return small_Tsu + key_dict[dict_choice]["."] + split_num_b_end
|
| 187 |
-
|
| 188 |
-
return Convert(split_num_a,dict_choice) + " " + key_dict[dict_choice]["."] + split_num_b_end
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
def do_kanji_convert(convert_num):
|
| 192 |
-
# Converts kanji to arabic number
|
| 193 |
-
|
| 194 |
-
if convert_num == "零":
|
| 195 |
-
return 0
|
| 196 |
-
|
| 197 |
-
# First, needs to check for MAN 万 and OKU 億 kanji, as need to handle differently, splitting up the numbers at these intervals.
|
| 198 |
-
# key tells us whether we need to add or multiply the numbers, then we create a list of numbers in an order we need to add/multiply
|
| 199 |
-
key = []
|
| 200 |
-
numberList = []
|
| 201 |
-
y = ""
|
| 202 |
-
for x in convert_num:
|
| 203 |
-
if x == "万" or x == "億":
|
| 204 |
-
numberList.append(y)
|
| 205 |
-
key.append("times")
|
| 206 |
-
numberList.append(x)
|
| 207 |
-
key.append("plus")
|
| 208 |
-
y = ""
|
| 209 |
-
else:
|
| 210 |
-
y += x
|
| 211 |
-
if y != "":
|
| 212 |
-
numberList.append(y)
|
| 213 |
-
|
| 214 |
-
numberListConverted = []
|
| 215 |
-
baseNumber = ["一", "二", "三", "四", "五", "六", "七", "八", "九"]
|
| 216 |
-
linkNumber = ["十", "百", "千", "万", "億"]
|
| 217 |
-
|
| 218 |
-
# Converts the kanji number list to arabic numbers, using the 'base number' and 'link number' list above. For a link number, we would need to
|
| 219 |
-
# link with a base number
|
| 220 |
-
for noX in numberList:
|
| 221 |
-
count = len(noX)
|
| 222 |
-
result = 0
|
| 223 |
-
skip = 1
|
| 224 |
-
for x in reversed(noX):
|
| 225 |
-
addTo = 0
|
| 226 |
-
skip -= 1
|
| 227 |
-
count = count - 1
|
| 228 |
-
if skip == 1:
|
| 229 |
-
continue
|
| 230 |
-
if x in baseNumber:
|
| 231 |
-
for y, z in kanji_dict.items():
|
| 232 |
-
if z == x:
|
| 233 |
-
result += int(y)
|
| 234 |
-
elif x in linkNumber:
|
| 235 |
-
if noX[count - 1] in baseNumber and count > 0:
|
| 236 |
-
for y, z in kanji_dict.items():
|
| 237 |
-
if z == noX[count - 1]:
|
| 238 |
-
tempNo = int(y)
|
| 239 |
-
for y, z in kanji_dict.items():
|
| 240 |
-
if z == x:
|
| 241 |
-
addTo += tempNo * int(y)
|
| 242 |
-
result += addTo
|
| 243 |
-
skip = 2
|
| 244 |
-
else:
|
| 245 |
-
for y, z in kanji_dict.items():
|
| 246 |
-
if z == x:
|
| 247 |
-
result += int(y)
|
| 248 |
-
numberListConverted.append(int(result))
|
| 249 |
-
|
| 250 |
-
result = numberListConverted[0]
|
| 251 |
-
y = 0
|
| 252 |
-
|
| 253 |
-
# Iterate over the converted list, and either multiply/add as instructed in key list
|
| 254 |
-
for x in range(1,len(numberListConverted)):
|
| 255 |
-
if key[y] == "plus":
|
| 256 |
-
try:
|
| 257 |
-
if key[y+1] == "times":
|
| 258 |
-
result = result + numberListConverted[x] * numberListConverted[x+1]
|
| 259 |
-
y += 1
|
| 260 |
-
else:
|
| 261 |
-
result += numberListConverted[x]
|
| 262 |
-
except IndexError:
|
| 263 |
-
result += numberListConverted[-1]
|
| 264 |
-
break
|
| 265 |
-
else:
|
| 266 |
-
result = result * numberListConverted[x]
|
| 267 |
-
y += 1
|
| 268 |
-
|
| 269 |
-
return result
|
| 270 |
-
|
| 271 |
-
def Convert(convert_num, dict_choice='hiragana'):
|
| 272 |
-
# Input formatting
|
| 273 |
-
convert_num = str(convert_num)
|
| 274 |
-
convert_num = convert_num.replace(',','')
|
| 275 |
-
dict_choice = dict_choice.lower()
|
| 276 |
-
|
| 277 |
-
# If all is selected as dict_choice, return as a list
|
| 278 |
-
if dict_choice == "all":
|
| 279 |
-
result_list = []
|
| 280 |
-
for x in "kanji", "hiragana", "romaji":
|
| 281 |
-
result_list.append(Convert(convert_num,x))
|
| 282 |
-
return result_list
|
| 283 |
-
|
| 284 |
-
dictionary = key_dict[dict_choice]
|
| 285 |
-
|
| 286 |
-
# Exit if length is greater than current limit
|
| 287 |
-
if len(convert_num) > 9:
|
| 288 |
-
return("Number length too long, choose less than 10 digits")
|
| 289 |
-
|
| 290 |
-
# Remove any leading zeroes
|
| 291 |
-
while convert_num[0] == "0" and len(convert_num) > 1:
|
| 292 |
-
convert_num = convert_num[1:]
|
| 293 |
-
|
| 294 |
-
# Check for decimal places
|
| 295 |
-
if "." in convert_num:
|
| 296 |
-
result = split_Point(convert_num,dict_choice)
|
| 297 |
-
else:
|
| 298 |
-
result = do_convert(convert_num, dictionary)
|
| 299 |
-
|
| 300 |
-
# Remove spaces and return result
|
| 301 |
-
if key_dict[dict_choice] == romaji_dict:
|
| 302 |
-
pass
|
| 303 |
-
else:
|
| 304 |
-
result = remove_spaces(result)
|
| 305 |
-
return result
|
| 306 |
-
|
| 307 |
-
def ConvertKanji(convert_num):
|
| 308 |
-
if convert_num[0] in kanji_dict.values():
|
| 309 |
-
# Check to see if 点 (point) is in the input, and handle by splitting at 点, before and after is handled separately
|
| 310 |
-
if "点" in convert_num:
|
| 311 |
-
point = convert_num.find("点")
|
| 312 |
-
endNumber = ""
|
| 313 |
-
for x in convert_num[point+1:]:
|
| 314 |
-
endNumber += list(kanji_dict.keys())[list(kanji_dict.values()).index(x)]
|
| 315 |
-
return(str(do_kanji_convert(convert_num[0:point])) + "." + endNumber)
|
| 316 |
-
else:
|
| 317 |
-
return(str(do_kanji_convert(convert_num)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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