Skip to main content

chiecthuyenngoaixa

GitHub issues GitHub license Documentation Status PyPI PyPI Downloads

Tiếng Việt

chiecthuyenngoaixa is a Python library which provides functions and classes for various tasks in processing Vietnamese texts, such as removing diacritics, converting numbers to words, sorting strings, validations and more.

This library is written on pure Python with no dependencies. Python 3.8 and above is supported.

Installation

Chiecthuyenngoaixa is available on PyPI. Open a terminal or Command Prompt (on Windows) and run the following command:

pip install chiecthuyenngoaixa

If you are using Poetry, use this instead:

poetry add chiecthuyenngoaixa

Or if you are using uv:

uv add chiecthuyenngoaixa

Basic usage

The library will now be available as ctnx module (abbreviation of chiecthuyenngoaixa).

Some commonly used functions and classes can be imported directly. For example:

  • To convert Vietnamese text to ASCII-only text:
>>> from ctnx import remove_diacritics
>>> remove_diacritics("Đàn ong thấy cái lon thì bu vào.")
'Dan ong thay cai lon thi bu vao.'
  • To convert a number to Vietnamese text:
>>> from ctnx import num_to_words
>>> num_to_words(123456789021003.45)
'một trăm hai mươi ba nghìn bốn trăm năm mươi sáu tỉ bảy trăm tám mươi chín triệu không trăm hai mươi mốt nghìn không trăm linh ba phẩy bốn mươi lăm'
  • To sort Vietnamese texts:
>>> from ctnx import vi_sort_key
>>> lines = ['Hà Nam', 'Hải Dương', 'Hà Nội', 'Hà Tĩnh', 'Hải Phòng', 'Hậu Giang', 'Hoà Bình', 'Hưng Yên', 'Hạ Long', 'Hà Giang', 'Điện Biên']
>>> sorted(lines, key=vi_sort_key)
['Điện Biên', 'Hà Giang', 'Hà Nam', 'Hà Nội', 'Hà Tĩnh', 'Hải Dương', 'Hải Phòng', 'Hạ Long', 'Hậu Giang', 'Hoà Bình', 'Hưng Yên']

Other functions and classes are put into separate sub-modules. Some modules are introduced more below.

Sorting Vietnamese strings

The default tone-sorting order is ngang, sắc, huyền, hỏi, ngã, nặng. If you prefers a different order, you should use ctnx.sort.ViCollator instead of ctnx.vi_sort_key.

>>> from ctnx.sort import ViCollator, vi_sort_key
>>> ds = ['mạn', 'mạ', 'màn', 'mà', 'man', 'ma', 'má', 'mán']
>>> sorted(ds, key=vi_sort_key)
['ma', 'man', 'má', 'mán', 'mà', 'màn', 'mạ', 'mạn']
>>> sorter = ViCollator(["\\", "?", "~", "/", "."])
>>> sorted(ds, key=sorter.key)
['mà', 'màn', 'má', 'mán', 'mạ', 'mạn', 'ma', 'man']

Normalizing text

The library provides ctnx.normalize_text to do basic text cleaning and normalization.

>>> from ctnx import normalize_text
>>> normalize_text("------- “Họa sĩ :𝕋𝕠̂ ℕ𝕘𝕠̣𝕔 𝕍𝕒̂𝕟 ”", strip_punctuation=True, do_normalize_confusables=True)
'Hoạ sĩ Tô Ngọc Vân'

Other normalization functions live in the ctnx.misc module.

  • To convert a likely confusing text of Vietnamese to the normal text:
>>> from ctnx.misc import normalize_confusables
>>> normalize_confusables("𝕮𝖍𝖎ế𝖈 𝖙𝖍𝖚𝖞ề𝖓 𝖓𝖌𝖔à𝖎 𝖝𝖆")
'Chiếc thuyền ngoài xa'
  • To normalize tone characters to either the "new" style (oà, oẻ, uý) or the "old" style (òa, ỏe, úy):
>>> from ctnx.misc import normalize_tone_placement_new_style, normalize_tone_placement_old_style
>>> text = "mũi thuyền in một nét lòe nhoè vào bầu sương mù"
>>> normalize_tone_placement_new_style(text)
'mũi thuyền in một nét loè nhoè vào bầu sương mù'
>>> normalize_tone_placement_old_style(text)
'mũi thuyền in một nét lòe nhòe vào bầu sương mù'
  • To normalize i/y characters in tokens like "hi, ki, li, mi, quy, si, ty, vi":
>>> from ctnx.misc import IYNormalizer
>>> normer = IYNormalizer.from_preset_style("sinoviet_hklmqt_y")
>>> normer("Con lạy quí toà...")
'Con lạy quý toà...'
>>> IYNormalizer.from_preset_style("unified_i").replace("cái thằng trẻ con lạ kỳ nhất trần đời.")
'cái thằng trẻ con lạ kì nhất trần đời.'

Validation

  • To extract information from a Vietnamese National Citizen ID (Căn cước công dân) number:
>>> from ctnx import validation
>>> validation.is_valid_cccd("024192123456")
True
>>> validation.parse_cccd("024192123456")
CccdResult(id='123456', is_male=False, birth_year=1992, birth_country='vn', birth_province='Bắc Giang')

Manipulating syllable

chiecthuyenngoaixa provides the syllable.Syllable class to deal with Vietnamese syllables.

  • To manipulate Vietnamese syllables:
>>> from ctnx.syllable import Syllable
>>> text = "ba ngày một trận nhẹ năm ngày một trận nặng"
>>> a = [Syllable.from_string(x) for x in text.split(' ')]
>>> a
[Syllable(b, a, ), Syllable(ng, ay, , \), Syllable(m, ô, t, .), Syllable(tr, â, n, .), Syllable(nh, e, , .), Syllable(n, ă, m), Syllable(ng, ay, , \), Syllable(m, ô, t, .), Syllable(tr, â, n, .), Syllable(n, ă, ng, .)]
>>> for syll in a:
...     syll.onset = 'nh'
...
>>> a
[Syllable(nh, a, ), Syllable(nh, ay, , \), Syllable(nh, ô, t, .), Syllable(nh, â, n, .), Syllable(nh, e, , .), Syllable(nh, ă, m), Syllable(nh, ay, , \), Syllable(nh, ô, t, .), Syllable(nh, â, n, .), Syllable(nh, ă, ng, .)]
>>> ' '.join(str(x) for x in a)
'nha nhày nhột nhận nhẹ nhăm nhày nhột nhận nhặng'

Other

  • To extract tones from a Vietnamese syllable or text:
>>> from ctnx.misc import separate_tone
>>> separate_tone("Đẩu")
('Đâu', '?')
>>> toneNames = {'': 'thanh', '/': 'sắc', '\\': 'huyền', '?': 'hỏi', '~': 'ngã', '.': 'nặng'}
>>> ' '.join(toneNames[separate_tone(syll)[1]] for syll in "Tôi thầm cảm ơn Đẩu đã giữ mình ở nán lại".split(' '))
'thanh huyền hỏi thanh hỏi ngã ngã huyền hỏi sắc nặng'

For further usages, see the documentation, which is hosted on chiecthuyenngoaixa.readthedocs.io.

Metadata

Release files for chiecthuyenngoaixa 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chiecthuyenngoaixa 0.3.0
File Size Uploaded
chiecthuyenngoaixa-0.3.0.tar.gz 38.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chiecthuyenngoaixa 0.3.0
File Interpreter ABI Platform
chiecthuyenngoaixa-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 74.9 kB

Release files / chiecthuyenngoaixa-0.3.0.tar.gz

Download URL chiecthuyenngoaixa-0.3.0.tar.gz
Size 38.1 kB
Tags Source
SHA-256 checksum
How to use checksums
369437bfb40b3603304cc7c863629dbfc9d3f185f8290b2f1fb66816f1bc45ab
BLAKE2b-256 checksum
How to use checksums
662931a8544109e6e78d2c6a4e9bce5bd80a87836cbb364134ad3acfc6a28cd9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.8.10 Linux/6.17.0-1018-azure

Release files / chiecthuyenngoaixa-0.3.0-py3-none-any.whl

Download URL chiecthuyenngoaixa-0.3.0-py3-none-any.whl
Size 36.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c2f5de4fed95d4d8757516b51a741cef79a3ccec7c0cbc75e8f3a96fca5a6be
BLAKE2b-256 checksum
How to use checksums
451579bae5804113327d8275b11c98285c1bb2e0ab1adc02581f56684af8d71d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.8.10 Linux/6.17.0-1018-azure

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page