Python uses cn2an to realize the conversion between Chinese numerals and Arabic numerals

  • 2021-09-12 01:42:39
  • OfStack

In my work, I often encounter the conversion of Arabic numerals into Chinese numerals or amount in words. After searching on the Internet, cn2an has a good reputation, so I studied it once.

Installation


pip install cn2an

Dependent libraries are setuptools, PyYAML

View version


In [1]: import cn2an
In [2]: cn2an.__version__
Out[2]: '0.5.8'

Viewing Identifiers for Module Definitions


In [3]: dir(cn2an)
Out[3]:
['An2Cn',
 'Cn2An',
 'Transform',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '__version__',
 'an2cn',
 'cn2an',
 'transform',
 'utils']

Modular structure

The cn2an module structure is as follows:


an2cn.py
an2cn_test.py
cn2an.py
cn2an_test.py
config.yaml
performance.py
transform.py
transform_test.py
utils.py
__init__.py

Looking at the __init__. py file under the module, we can see that the module mainly exposes three methods: cn2an, an2cn and transform.


from .cn2an import Cn2An
from .an2cn import An2Cn
from .transform import Transform

__version__ = "0.5.8"

cn2an = Cn2An().cn2an
an2cn = An2Cn().an2cn
transform = Transform().transform

__all__ = [
  "__version__",
  "cn2an",
  "an2cn",
  "transform"
]

Instructions for use

1. Conversion of Chinese numerals to Arabic numerals


In [4]: help(cn2an.cn2an)
Help on method cn2an in module cn2an.cn2an:

cn2an(inputs: str = None, mode: str = 'strict') -> int method of cn2an.cn2an.Cn2An instance

Check the help to see:
cn2an method has two parameters: inputs and mode, inputs type is string, the default value is null, mode type is string, the default value is strict, look at the source code, mode only supports strict, normal, smart and other three values.
The return value is of type int.
The maximum support is 10**16, that is, trillion yuan, and the minimum support is 10**-16.

1.1 Strict Mode (strict)

The default is strict mode. In strict mode, only those that strictly conform to the spelling of numbers can be converted.


In [1]: import cn2an
In [2]: output = cn2an.cn2an("1 Hundred 2103")
In [3]: output
Out[3]: 123
In [4]: output = cn2an.cn2an("123")
---------------------------------------------------------------------------
ValueError                Traceback (most recent call last)
<ipython-input-4-4c55ae4aa4a9> in <module>
----> 1 output = cn2an.cn2an("123")

c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages\cn2an\cn2an.py in cn2an(self, inputs, mode)
   30
   31       #  Check whether the input data is valid 
---> 32       sign, integer_data, decimal_data, is_all_num = self.__check_input_data_is_valid(inputs, mode)
   33
   34       # smart  Special circumstances under 

c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages\cn2an\cn2an.py in __check_input_data_is_valid(self, check_data, mode)
  155     else:
  156       if mode == "strict":
--> 157         raise ValueError(f" Unformatted data: {integer_data}")
  158       elif mode == "normal":
  159         #  Pure number mode: 123

ValueError:  Unformatted data: 123

1.2 Normal Mode (normal)

In normal mode, 123 can be converted


In [5]: output = cn2an.cn2an("123",'normal')
In [6]: output
Out[6]: 123

1.3 Flexible Mode (smart)

In the flexible mode, you can convert the mixed spelling 123, and you can also convert the colloquial number


In [7]: output = cn2an.cn2an("1 Hundred 23", "smart")
In [8]: output
Out[8]: 123
In [9]: output = cn2an.cn2an("1 Ten thousand 2", "smart")
In [10]: output
Out[10]: 12000

2. Conversion of Arabic numerals to Chinese numerals


In [1]: import cn2an

In [2]: help(cn2an.an2cn)
Help on method an2cn in module cn2an.an2cn:

an2cn(inputs: Union[str, int, float] = None, mode: str = 'low') -> str method of cn2an.an2cn.An2Cn instance
   Arabic numerals to Chinese numerals 
  :param inputs:  Arabic numerals 
  :param mode:  Lowercase digits, uppercase digits, RMB uppercase, direct conversion 
  :return:  Chinese numerals 

Check the help to see:

an2cn method has two parameters: inputs and mode, inputs type can be string, integer or floating point number, the default value is null, mode type is string, the default value is low, look at the source code, mode supports low, up, rmb, direct and other four values.
The return value is of type str.

2.1 Lowercase Chinese Mode (low)

In low mode (default), numbers are converted to lowercase Chinese numerals.


In [1]: import cn2an
In [2]: cn2an.__version__
Out[2]: '0.5.8'
0

2.2 Capital Chinese Mode (up)

In up mode, numbers are converted to uppercase Chinese numerals.


In [1]: import cn2an
In [2]: cn2an.__version__
Out[2]: '0.5.8'
1

2.3 RMB Model (rmb)

In rmb mode, numbers are converted into RMB-specific descriptions.


In [1]: import cn2an
In [2]: cn2an.__version__
Out[2]: '0.5.8'
2

2.4 Direct Mode (direct)

In direct mode, Arabic numerals are directly converted into corresponding Chinese lowercase digits, and decimal points are converted into Chinese character points.


In [11]: output = cn2an.an2cn("-1.23", "direct")
In [12]: output
Out[12]: ' Negative 1 Point 23'

3. Number Conversion in Sentences

It is stated in the document that this function is experimental and unstable.


In [1]: import cn2an
In [2]: cn2an.__version__
Out[2]: '0.5.8'
4

The transform method has two parameters: inputs and method, the type of inputs is string, the type of method is string, and the default value is cn2an. Look at the source code and see that method supports cn2an and an2cn. The return value is a string.

3.1 Conversion of Chinese numerals to Arabic numerals

Under the cn2an method (default), Chinese numerals in sentences can be converted into Arabic numerals
View the source code to know that by default, the conversion of Chinese numerals to Arabic numerals using smart mode.


In [1]: import cn2an
In [2]: cn2an.__version__
Out[2]: '0.5.8'
5

The transform method converts according to special circumstances such as regular expression, detection date, degree Celsius, fraction, percentage, etc.


In [1]: import cn2an
In [2]: cn2an.__version__
Out[2]: '0.5.8'
6

3.2 Conversion of Arabic numerals to Chinese numerals

Under an2cn method, Arabic numerals in sentences can be converted into Chinese numerals.
In an2cn mode, special conditions such as date, degree Celsius, fraction and percentage will still be detected for conversion


In [1]: import cn2an
In [2]: cn2an.__version__
Out[2]: '0.5.8'
7

3.3 Questions (in capitals)

According to the source code, transform method gives priority to date, score, percentage, degrees Celsius, etc., and those who do not conform to these situations are judged as ordinary numbers. Therefore, capital numbers are not good in transform method!


In [14]: output = cn2an.transform(" The amount is twelve thousand yuan ", "cn2an")
WARN:  Unformatted data: ten thousand 
WARN:  Data that does not conform to the format: thousands 
In [15]: output
Out[15]: ' The amount is twelve thousand yuan  '

Test

cn2an provides the web demo experience. https://www. dovolopor. com/cn2an

API

cn2an provides API and supports Java, Javascript, Go, Julia, Python and so on.
https://github.com/Ailln/cn2an/wiki/API#http-api


Related articles: