pycnnum.constants API documentation

# pycnnum.constants

Chinese character constants

View Source

     1# pylint: disable=line-too-long
     2"""Chinese character constants"""
     3
     4from dataclasses import dataclass
     5from typing import Optional
     6
     7
     8@dataclass
     9class CNConstant:
    10    """Chinese number constant"""
    11
    12    SIMPLIFIED_LOWER: str
    13    """lower case simplified string"""
    14
    15    TRADITIONAL_LOWER: Optional[str] = None
    16    """lower case traditional string. Defaults to simplified string"""
    17
    18    SIMPLIFIED_UPPER: Optional[str] = None
    19    """upper case simplified string. Defaults to lower case traditional string"""
    20
    21    TRADITIONAL_UPPER: Optional[str] = None
    22    """upper case traditional string. Defaults to lower case traditional string"""
    23
    24    def __post_init__(self) -> None:
    25        """Post initialization"""
    26        self.TRADITIONAL_LOWER = self.TRADITIONAL_LOWER or self.SIMPLIFIED_LOWER
    27        self.SIMPLIFIED_UPPER = self.SIMPLIFIED_UPPER or self.TRADITIONAL_LOWER
    28        self.TRADITIONAL_UPPER = self.TRADITIONAL_UPPER or self.TRADITIONAL_LOWER
    29
    30    def __getitem__(self, key: int) -> str:
    31        """Get item by index"""
    32        if self.TRADITIONAL_LOWER is None:
    33            raise ValueError("TRADITIONAL_LOWER is not set")
    34        if self.SIMPLIFIED_UPPER is None:
    35            raise ValueError("SIMPLIFIED_UPPER is not set")
    36        if self.TRADITIONAL_UPPER is None:
    37            raise ValueError("TRADITIONAL_UPPER is not set")
    38        return "".join(
    39            (
    40                self.SIMPLIFIED_LOWER[key],
    41                self.TRADITIONAL_LOWER[key],
    42                self.SIMPLIFIED_UPPER[key],
    43                self.TRADITIONAL_UPPER[key],
    44            )
    45        )
    46
    47    def __iter__(self):
    48        """Iterate over the constant"""
    49        for i in range(len(self.SIMPLIFIED_LOWER)):
    50            yield self[i]
    51
    52
    53DIGITS = CNConstant("零一二三四五六七八九", "零壹贰叁肆伍陆柒捌玖")
    54"""Chinese number digits
    55
    56- Simplified: `"零一二三四五六七八九"`
    57- Traditional: `"零壹贰叁肆伍陆柒捌玖"`
    58- Upper simplified: `"零一二三四五六七八九"`
    59- Upper traditional: `"零壹贰叁肆伍陆柒捌玖"`
    60"""
    61
    62
    63UNITS = CNConstant("十百千万亿兆京垓秭穰沟涧正载", "拾佰仟萬億兆京垓秭穰溝澗正載")
    64r"""Chinese number units
    65
    66For $i \in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]$:
    67
    68| NumberingType | value              |
    69|---------------|--------------------|
    70| `LOW`         | $10^{8 + i}$       |
    71| `MID`         | $10^{8 + i*4}$     |
    72| `HIGH`        | $10^{8 + 2^{i+3}}$ |
    73
    74---
    75
    76| type  |  亿     | 兆      | 京      | 垓       | 秭       | 穰       | 沟       | 涧        | 正        | 载         |
    77|-------| --------|---------|---------|---------|----------|----------|----------|-----------|-----------|-----------|
    78|`LOW`  | $10^{8}$|$10^{9}$ |$10^{10}$|$10^{11}$|$10^{12}$ |$10^{13}$ |$10^{14}$ |$10^{15}$  |$10^{16}$  |$10^{17}$  |
    79|`MID`  | $10^{8}$|$10^{12}$|$10^{16}$|$10^{20}$|$10^{24}$ |$10^{28}$ |$10^{32}$ |$10^{36}$  |$10^{40}$  |$10^{44}$  |
    80|`HIGH` | $10^{8}$|$10^{16}$|$10^{32}$|$10^{64}$|$10^{128}$|$10^{256}$|$10^{512}$|$10^{1024}$|$10^{2048}$|$10^{4096}$|
    81
    82---
    83
    84Example:
    85
    86- "一兆" is ` 1 000 000 000` in `LOW` numbering type
    87- "一兆" is ` 1 000 000 000 000` in `MID` numbering type
    88- "一兆" is `10 000 000 000 000 000` in `HIGH` numbering type
    89"""
    90
    91
    92__all__ = [
    93    "DIGITS",
    94    "UNITS",
    95]

DIGITS = CNConstant(SIMPLIFIED_LOWER='零一二三四五六七八九', TRADITIONAL_LOWER='零壹贰叁肆伍陆柒捌玖', SIMPLIFIED_UPPER='零壹贰叁肆伍陆柒捌玖', TRADITIONAL_UPPER='零壹贰叁肆伍陆柒捌玖')

Chinese number digits

- Simplified: `"零一二三四五六七八九"`
- Traditional: `"零壹贰叁肆伍陆柒捌玖"`
- Upper simplified: `"零一二三四五六七八九"`
- Upper traditional: `"零壹贰叁肆伍陆柒捌玖"`

UNITS = CNConstant(SIMPLIFIED_LOWER='十百千万亿兆京垓秭穰沟涧正载', TRADITIONAL_LOWER='拾佰仟萬億兆京垓秭穰溝澗正載', SIMPLIFIED_UPPER='拾佰仟萬億兆京垓秭穰溝澗正載', TRADITIONAL_UPPER='拾佰仟萬億兆京垓秭穰溝澗正載')

Chinese number units

For $i \in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]$:

NumberingType | value
--- | ---
`LOW` | $10^{8 + i}$
`MID` | $10^{8 + i*4}$
`HIGH` | $10^{8 + 2^{i+3}}$

type | 亿 | 兆 | 京 | 垓 | 秭 | 穰 | 沟 | 涧 | 正 | 载
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
`LOW` | $10^{8}$ | $10^{9}$ | $10^{10}$ | $10^{11}$ | $10^{12}$ | $10^{13}$ | $10^{14}$ | $10^{15}$ | $10^{16}$ | $10^{17}$
`MID` | $10^{8}$ | $10^{12}$ | $10^{16}$ | $10^{20}$ | $10^{24}$ | $10^{28}$ | $10^{32}$ | $10^{36}$ | $10^{40}$ | $10^{44}$
`HIGH` | $10^{8}$ | $10^{16}$ | $10^{32}$ | $10^{64}$ | $10^{128}$ | $10^{256}$ | $10^{512}$ | $10^{1024}$ | $10^{2048}$ | $10^{4096}$

Example:

- "一兆" is `1 000 000 000` in `LOW` numbering type
- "一兆" is `1 000 000 000 000` in `MID` numbering type
- "一兆" is `10 000 000 000 000 000` in `HIGH` numbering type