Skip to main content

Khmer word segmentation, POS tagging, NER, and more — powered by a custom Khmer language model

Project description

khmer-nlp-kcc

Khmer NLP toolkit — word segmentation, POS tagging, sentiment polarity, and grapheme-to-phoneme conversion.

Powered by a custom Khmer language model. The NLP checkpoint is downloaded automatically from HuggingFace Hub on first use.

Installation

pip install khmer-nlp-kcc

Quick start

from khmer_nlp import KhmerNLP

nlp = KhmerNLP()  # NLP checkpoint downloads on first call

# Word segmentation
nlp.segment("គាត់និយាយថា៖ការសិក្សានាំមកនូវចំណេះដឹងតើឯកភាពទេ?")
# → "គាត់ និយាយ ថា ៖ ការសិក្សា នាំ មក នូវ ចំណេះដឹង តើ ឯកភាព ទេ ?"

# Word segmentation with sub-word markers
nlp.segment("គាត់និយាយថា៖ការសិក្សានាំមកនូវចំណេះដឹងតើឯកភាពទេ?", markers=True)

# POS tagging
nlp.pos("គាត់និយាយថា៖ការសិក្សានាំមកនូវចំណេះដឹងតើឯកភាពទេ?")
# → [{"word": "គាត់", "label": "PRO"}, {"word": "និយាយ", "label": "VB"}, ...]

# Nova POS tagging
nlp.nova_pos("គាត់និយាយថា៖ការសិក្សានាំមកនូវចំណេះដឹងតើឯកភាពទេ?")
# → [{"word": "គាត់", "label": "o"}, {"word": "និយាយ", "label": "v"}, ...]

# Sentiment polarity
nlp.polarity("គាត់និយាយថា៖ការសិក្សានាំមកនូវចំណេះដឹងតើឯកភាពទេ?")
# → {"label": "neutral", "confidence": ..., "scores": {...}}

# All tasks at once
nlp.analyze("គាត់និយាយថា៖ការសិក្សានាំមកនូវចំណេះដឹងតើឯកភាពទេ?")

Grapheme-to-phoneme (G2P)

The G2P model requires a separate checkpoint (g2p_final_trans.pt). Pass its path via g2p_checkpoint_path:

nlp = KhmerNLP(g2p_checkpoint_path="/path/to/g2p_final_trans.pt")

# Phoneme sequence for a single word
nlp.g2p("ខ្មែរ")
# → ['kh', 'ae', '.', 'm', 'ae']

# Phoneme CER between two Khmer words
nlp.phoneme_cer("ខ្មែរ", "ខ្មែរភូមិ")
# → {
#     "word1": "ខ្មែរ",   "phones1": ["kh", "ae", ".", "m", "ae"],
#     "word2": "ខ្មែរភូមិ", "phones2": ["kh", "ae", ".", "m", "ae", ".", "ph", "uu", "m", ".", "m", "ɨ"],
#     "cer": 0.7
#   }

CER is computed at phoneme-token level (edit distance / number of phonemes in word1).

Custom checkpoint

nlp = KhmerNLP(checkpoint_path="/path/to/your/model.pt")

Device selection

import torch
nlp = KhmerNLP(device=torch.device("cuda:0"))

Available methods

Method Returns Description
segment(text, *, markers=False) str Space-joined segmented words; markers=True adds sub-word markers (_)
pos(text) list[dict] Word + POS label
nova_pos(text) list[dict] Word + Nova POS label
polarity(text) dict Sentiment polarity
analyze(text) dict All tasks combined
g2p(word) list[str] Phoneme token sequence for a Khmer word
phoneme_cer(word1, word2) dict Phoneme CER between two Khmer words

Raw token-level outputs: seg_tokens(), pos_tokens().

License

MIT

Project details


Download files

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

Source Distribution

khmer_nlp_kcc-0.2.6.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

khmer_nlp_kcc-0.2.6-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file khmer_nlp_kcc-0.2.6.tar.gz.

File metadata

  • Download URL: khmer_nlp_kcc-0.2.6.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for khmer_nlp_kcc-0.2.6.tar.gz
Algorithm Hash digest
SHA256 86cb20927f3aa3ddd50613100c43fa90f5420385608fefbedd2649e951547f78
MD5 593ec912d641e37dc6eaa64d509dda81
BLAKE2b-256 186a0b146cbabd22ea1eb2373a9efb7c2b15a11ef815014562958cde6e8772ff

See more details on using hashes here.

File details

Details for the file khmer_nlp_kcc-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: khmer_nlp_kcc-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for khmer_nlp_kcc-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f8bffba1718c1dfab806b9d9350039e92f03198bd3d72cc7b8c907ff75c792b2
MD5 b246538e64816d6a2660c348a2dcdf15
BLAKE2b-256 577f34a8dc34d97f62cc6ae1fad59e68a5e2e97c2cce80a13e435a166dae1b07

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page