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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chiecthuyenngoaixa-0.3.0.tar.gz (38.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chiecthuyenngoaixa-0.3.0-py3-none-any.whl (36.8 kB view details)

Uploaded Python 3

File details

Details for the file chiecthuyenngoaixa-0.3.0.tar.gz.

File metadata

  • Download URL: chiecthuyenngoaixa-0.3.0.tar.gz
  • Upload date:
  • Size: 38.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.8.10 Linux/6.17.0-1018-azure

File hashes

Hashes for chiecthuyenngoaixa-0.3.0.tar.gz
Algorithm Hash digest
SHA256 369437bfb40b3603304cc7c863629dbfc9d3f185f8290b2f1fb66816f1bc45ab
MD5 d8acbf5078d5fdaecb09244ac36a5e34
BLAKE2b-256 662931a8544109e6e78d2c6a4e9bce5bd80a87836cbb364134ad3acfc6a28cd9

See more details on using hashes here.

File details

Details for the file chiecthuyenngoaixa-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: chiecthuyenngoaixa-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 36.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.8.10 Linux/6.17.0-1018-azure

File hashes

Hashes for chiecthuyenngoaixa-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c2f5de4fed95d4d8757516b51a741cef79a3ccec7c0cbc75e8f3a96fca5a6be
MD5 497503241fe745d933a80b8f00a9c4ff
BLAKE2b-256 451579bae5804113327d8275b11c98285c1bb2e0ab1adc02581f56684af8d71d

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.1

2 files

0.2.0

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 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