(number_string, numbering_type=NUMBERING_TYPES[1], big=False,
traditional=False, alt_zero=False, alt_one=False, alt_two=True,
use_zeros=True, use_units=True)
| 317 | |
| 318 | |
| 319 | def num2chn(number_string, numbering_type=NUMBERING_TYPES[1], big=False, |
| 320 | traditional=False, alt_zero=False, alt_one=False, alt_two=True, |
| 321 | use_zeros=True, use_units=True): |
| 322 | def get_value(value_string, use_zeros=True): |
| 323 | |
| 324 | striped_string = value_string.lstrip('0') |
| 325 | |
| 326 | # record nothing if all zeros |
| 327 | if not striped_string: |
| 328 | return [] |
| 329 | |
| 330 | # record one digits |
| 331 | elif len(striped_string) == 1: |
| 332 | if use_zeros and len(value_string) != len(striped_string): |
| 333 | return [system.digits[0], system.digits[int(striped_string)]] |
| 334 | else: |
| 335 | return [system.digits[int(striped_string)]] |
| 336 | |
| 337 | # recursively record multiple digits |
| 338 | else: |
| 339 | result_unit = next(u for u in reversed( |
| 340 | system.units) if u.power < len(striped_string)) |
| 341 | result_string = value_string[:-result_unit.power] |
| 342 | return get_value(result_string) + [result_unit] + get_value(striped_string[-result_unit.power:]) |
| 343 | |
| 344 | system = create_system(numbering_type) |
| 345 | |
| 346 | int_dec = number_string.split('.') |
| 347 | if len(int_dec) == 1: |
| 348 | int_string = int_dec[0] |
| 349 | dec_string = "" |
| 350 | elif len(int_dec) == 2: |
| 351 | int_string = int_dec[0] |
| 352 | dec_string = int_dec[1] |
| 353 | else: |
| 354 | raise ValueError( |
| 355 | "invalid input num string with more than one dot: {}".format(number_string)) |
| 356 | |
| 357 | if use_units and len(int_string) > 1: |
| 358 | result_symbols = get_value(int_string) |
| 359 | else: |
| 360 | result_symbols = [system.digits[int(c)] for c in int_string] |
| 361 | dec_symbols = [system.digits[int(c)] for c in dec_string] |
| 362 | if dec_string: |
| 363 | result_symbols += [system.math.point] + dec_symbols |
| 364 | |
| 365 | if alt_two: |
| 366 | liang = CND(2, system.digits[2].alt_s, system.digits[2].alt_t, |
| 367 | system.digits[2].big_s, system.digits[2].big_t) |
| 368 | for i, v in enumerate(result_symbols): |
| 369 | if isinstance(v, CND) and v.value == 2: |
| 370 | next_symbol = result_symbols[i + |
| 371 | 1] if i < len(result_symbols) - 1 else None |
| 372 | previous_symbol = result_symbols[i - 1] if i > 0 else None |
| 373 | if isinstance(next_symbol, CNU) and isinstance(previous_symbol, (CNU, type(None))): |
| 374 | if next_symbol.power != 1 and ((previous_symbol is None) or (previous_symbol.power != 1)): |
| 375 | result_symbols[i] = liang |
| 376 |
no test coverage detected