Num2words trong Python là gì?

def number_to_words(n, locale="vi", num_type="cardinal"). """ Chuyển đổi một giá trị số nguyên thành văn bản ngôn ngữ tự nhiên. . thông số n. Số để chuyển đổi. . gõ n. int. ngôn ngữ tham số. Ngôn ngữ để chuyển đổi sang. Các giá trị hiện được hỗ trợ. - "de" - "en" - "en_gb" - "es" - "fr" - "lt". gõ ngôn ngữ. str. thông số num_type. Loại số. Phải là một trong các giá trị sau. - "hồng y" - "thứ tự". lợi nhuận. văn bản ngôn ngữ tự nhiên. . đánh máy. str. tăng. TypeError, ValueError """ nếu không phải là phiên bản (n, int). tăng TypeError("Expexpected int, got '%s' instead" % type(n)) if not isinstance(locale, basestring). tăng TypeError("Dự kiến ​​chuỗi cơ sở, thay vào đó là '%s'" % type(locale)) nếu không phải là isinstance(num_type, basestring). tăng TypeError("Chuỗi cơ sở dự kiến, thay vào đó có '%s'" % type(num_type)) nếu num_type == "ordinal" và n < 0. tăng ValueError("Không thể lấy giá trị thứ tự từ số âm") nếu num_type == "thứ tự". cố gắng. trả về num2words(n, thứ tự=True, lang=ngôn ngữ. Lower()) ngoại trừ NotImplementedError. nâng cao ValueError ("Ngôn ngữ hoặc num_type không hợp lệ. ") elif num_type == "hồng y". cố gắng. trả về num2words(n, thứ tự=Sai, lang=ngôn ngữ. Lower()) ngoại trừ NotImplementedError. nâng cao ValueError ("Ngôn ngữ hoặc num_type không hợp lệ. ") khác. trả về num2words(n, thứ tự=Sai, lang="vi")

Calicut

Công nghệ Cybrosys Pvt. TNHH
Không gian mới, Công viên kỹ thuật Kinfra
Kakkancherry, Calicut
Kerala, Ấn Độ - 673635

Kochi

Công nghệ Cybrosys Pvt. TNHH
Tầng 1, Tòa nhà Thapasya,
Công viên thông tin, Kakkanad,
Kochi, Ấn Độ - 682030

num2words là một thư viện chuyển đổi các số như 42 thành các từ như forty-two. Nó hỗ trợ nhiều ngôn ngữ (xem danh sách bên dưới để biết danh sách đầy đủ các ngôn ngữ) và thậm chí có thể tạo các số thứ tự như

python setup.py install
0 (mặc dù tính năng cuối cùng này có một chút lỗi đối với một số ngôn ngữ vào lúc này)

Dự án được lưu trữ trên GitHub. Đóng góp được hoan nghênh

Cài đặt

Cách dễ nhất để cài đặt num2words là sử dụng pip

pip install num2words

Nếu không, bạn có thể tải xuống gói nguồn và sau đó thực hiện

python setup.py install

Bộ test trong thư viện này mới nên hơi mỏng nhưng chạy được với

python setup.py test

Để chạy bộ thử nghiệm CI đầy đủ bao gồm linting và nhiều môi trường python

pip install tox
tox

Cách sử dụng

Dòng lệnh

$ num2words 10001
ten thousand and one
$ num2words 24,120.10
twenty-four thousand, one hundred and twenty point one
$ num2words 24,120.10 -l es
veinticuatro mil ciento veinte punto uno
$num2words 2.14 -l es --to currency
dos euros con catorce céntimos

Trong mã chỉ có một chức năng để sử dụng

>>> from num2words import num2words
>>> num2words(42)
forty-two
>>> num2words(42, to='ordinal')
forty-second
>>> num2words(42, lang='fr')
quarante-deux

Bên cạnh đối số số, có hai đối số tùy chọn chính

đến. Bộ chuyển đổi để sử dụng. Các giá trị được hỗ trợ là

  • python setup.py install
    
    2 (mặc định)
  • python setup.py install
    
    3
  • python setup.py install
    
    4
  • python setup.py install
    
    5
  • python setup.py install
    
    6

lang thang. Ngôn ngữ để chuyển đổi số. Các giá trị được hỗ trợ là

  • python setup.py install
    
    7 (Tiếng Anh, mặc định)
  • python setup.py install
    
    8 (tiếng Amharic)
  • python setup.py install
    
    9 (tiếng Ả Rập)
  • python setup.py test
    
    0 (Azerbaijan)
  • python setup.py test
    
    1 (tiếng Séc)
  • python setup.py test
    
    2 (tiếng Đức)
  • python setup.py test
    
    3 (tiếng Đan Mạch)
  • python setup.py test
    
    4 (Tiếng Anh - Vương quốc Anh)
  • python setup.py test
    
    5 (Anh - Ấn Độ)
  • python setup.py test
    
    6 (tiếng Tây Ban Nha)
  • python setup.py test
    
    7 (Tây Ban Nha - Colombia)
  • python setup.py test
    
    8 (Tây Ban Nha - Venezuela)
  • python setup.py test
    
    9 (CHÂU ÂU)
  • pip install tox
    tox
    
    0 (tiếng Farsi)
  • pip install tox
    tox
    
    1 (tiếng Phần Lan)
  • pip install tox
    tox
    
    2 (tiếng Pháp)
  • pip install tox
    tox
    
    3 (Pháp - Thụy Sĩ)
  • pip install tox
    tox
    
    4 (Pháp - Bỉ)
  • pip install tox
    tox
    
    5 (Pháp - An-giê-ri)
  • pip install tox
    tox
    
    6 (tiếng Do Thái)
  • pip install tox
    tox
    
    7 (tiếng Hungary)
  • pip install tox
    tox
    
    8 (tiếng Indonesia)
  • pip install tox
    tox
    
    9 (tiếng Iceland)
  • $ num2words 10001
    ten thousand and one
    $ num2words 24,120.10
    twenty-four thousand, one hundred and twenty point one
    $ num2words 24,120.10 -l es
    veinticuatro mil ciento veinte punto uno
    $num2words 2.14 -l es --to currency
    dos euros con catorce céntimos
    
    0 (tiếng Ý)
  • $ num2words 10001
    ten thousand and one
    $ num2words 24,120.10
    twenty-four thousand, one hundred and twenty point one
    $ num2words 24,120.10 -l es
    veinticuatro mil ciento veinte punto uno
    $num2words 2.14 -l es --to currency
    dos euros con catorce céntimos
    
    1 (tiếng Nhật)
  • $ num2words 10001
    ten thousand and one
    $ num2words 24,120.10
    twenty-four thousand, one hundred and twenty point one
    $ num2words 24,120.10 -l es
    veinticuatro mil ciento veinte punto uno
    $num2words 2.14 -l es --to currency
    dos euros con catorce céntimos
    
    2 (Kannada)
  • $ num2words 10001
    ten thousand and one
    $ num2words 24,120.10
    twenty-four thousand, one hundred and twenty point one
    $ num2words 24,120.10 -l es
    veinticuatro mil ciento veinte punto uno
    $num2words 2.14 -l es --to currency
    dos euros con catorce céntimos
    
    3 (Hàn Quốc)
  • $ num2words 10001
    ten thousand and one
    $ num2words 24,120.10
    twenty-four thousand, one hundred and twenty point one
    $ num2words 24,120.10 -l es
    veinticuatro mil ciento veinte punto uno
    $num2words 2.14 -l es --to currency
    dos euros con catorce céntimos
    
    4 (Kazakhstan)
  • $ num2words 10001
    ten thousand and one
    $ num2words 24,120.10
    twenty-four thousand, one hundred and twenty point one
    $ num2words 24,120.10 -l es
    veinticuatro mil ciento veinte punto uno
    $num2words 2.14 -l es --to currency
    dos euros con catorce céntimos
    
    5 (tiếng Litva)
  • $ num2words 10001
    ten thousand and one
    $ num2words 24,120.10
    twenty-four thousand, one hundred and twenty point one
    $ num2words 24,120.10 -l es
    veinticuatro mil ciento veinte punto uno
    $num2words 2.14 -l es --to currency
    dos euros con catorce céntimos
    
    6 (tiếng Latvia)
  • $ num2words 10001
    ten thousand and one
    $ num2words 24,120.10
    twenty-four thousand, one hundred and twenty point one
    $ num2words 24,120.10 -l es
    veinticuatro mil ciento veinte punto uno
    $num2words 2.14 -l es --to currency
    dos euros con catorce céntimos
    
    7 (tiếng Na Uy)
  • $ num2words 10001
    ten thousand and one
    $ num2words 24,120.10
    twenty-four thousand, one hundred and twenty point one
    $ num2words 24,120.10 -l es
    veinticuatro mil ciento veinte punto uno
    $num2words 2.14 -l es --to currency
    dos euros con catorce céntimos
    
    8 (tiếng Ba Lan)
  • $ num2words 10001
    ten thousand and one
    $ num2words 24,120.10
    twenty-four thousand, one hundred and twenty point one
    $ num2words 24,120.10 -l es
    veinticuatro mil ciento veinte punto uno
    $num2words 2.14 -l es --to currency
    dos euros con catorce céntimos
    
    9 (tiếng Bồ Đào Nha)
  • >>> from num2words import num2words
    >>> num2words(42)
    forty-two
    >>> num2words(42, to='ordinal')
    forty-second
    >>> num2words(42, lang='fr')
    quarante-deux
    
    0 (Bồ Đào Nha - Brazil)
  • >>> from num2words import num2words
    >>> num2words(42)
    forty-two
    >>> num2words(42, to='ordinal')
    forty-second
    >>> num2words(42, lang='fr')
    quarante-deux
    
    1 (Tiếng Slovenia)
  • >>> from num2words import num2words
    >>> num2words(42)
    forty-two
    >>> num2words(42, to='ordinal')
    forty-second
    >>> num2words(42, lang='fr')
    quarante-deux
    
    2 (tiếng Serbia)
  • >>> from num2words import num2words
    >>> num2words(42)
    forty-two
    >>> num2words(42, to='ordinal')
    forty-second
    >>> num2words(42, lang='fr')
    quarante-deux
    
    3 (tiếng Thụy Điển)
  • >>> from num2words import num2words
    >>> num2words(42)
    forty-two
    >>> num2words(42, to='ordinal')
    forty-second
    >>> num2words(42, lang='fr')
    quarante-deux
    
    4 (tiếng Rumani)
  • >>> from num2words import num2words
    >>> num2words(42)
    forty-two
    >>> num2words(42, to='ordinal')
    forty-second
    >>> num2words(42, lang='fr')
    quarante-deux
    
    5 (tiếng Nga)
  • >>> from num2words import num2words
    >>> num2words(42)
    forty-two
    >>> num2words(42, to='ordinal')
    forty-second
    >>> num2words(42, lang='fr')
    quarante-deux
    
    6 (Tiếng Telugu)
  • >>> from num2words import num2words
    >>> num2words(42)
    forty-two
    >>> num2words(42, to='ordinal')
    forty-second
    >>> num2words(42, lang='fr')
    quarante-deux
    
    7 (Tajik)
  • >>> from num2words import num2words
    >>> num2words(42)
    forty-two
    >>> num2words(42, to='ordinal')
    forty-second
    >>> num2words(42, lang='fr')
    quarante-deux
    
    8 (tiếng Thổ Nhĩ Kỳ)
  • >>> from num2words import num2words
    >>> num2words(42)
    forty-two
    >>> num2words(42, to='ordinal')
    forty-second
    >>> num2words(42, lang='fr')
    quarante-deux
    
    9 (tiếng Thái)
  • try:
        return num2words(42, lang=mylang)
    except NotImplementedError:
        return num2words(42, lang='en')
    
    0 (Tiếng Việt)
  • try:
        return num2words(42, lang=mylang)
    except NotImplementedError:
        return num2words(42, lang='en')
    
    1 (tiếng Hà Lan)
  • try:
        return num2words(42, lang=mylang)
    except NotImplementedError:
        return num2words(42, lang='en')
    
    2 (tiếng Ukraina)

Bạn có thể cung cấp các giá trị như

try:
    return num2words(42, lang=mylang)
except NotImplementedError:
    return num2words(42, lang='en')
3; . e.
pip install tox
tox
2). Nếu bạn cung cấp một ngôn ngữ không được hỗ trợ,
try:
    return num2words(42, lang=mylang)
except NotImplementedError:
    return num2words(42, lang='en')
5 được nâng lên. Do đó, nếu bạn muốn gọi num2words với dự phòng, bạn có thể thực hiện

try:
    return num2words(42, lang=mylang)
except NotImplementedError:
    return num2words(42, lang='en')

Ngoài ra, một số trình chuyển đổi và ngôn ngữ hỗ trợ các đối số tùy chọn khác cần thiết để làm cho trình chuyển đổi trở nên hữu ích trong thực tế

Wiki

Để biết thêm thông tin về một số nội địa hóa, vui lòng kiểm tra Wiki. Và thoải mái đề xuất cải tiến wiki

Lịch sử

num2words dựa trên một thư viện cũ,

try:
    return num2words(42, lang=mylang)
except NotImplementedError:
    return num2words(42, lang='en')
8, do Taro Ogawa tạo ra vào năm 2003. Thật không may, thư viện đã ngừng được duy trì và không thể liên hệ với tác giả. Có một nhà phát triển khác, Marius Grigaitis, vào năm 2011 đã thêm hỗ trợ của Litva, nhưng không đảm nhận việc bảo trì dự án