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.5.tar.gz (11.6 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.5-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: khmer_nlp_kcc-0.2.5.tar.gz
  • Upload date:
  • Size: 11.6 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.5.tar.gz
Algorithm Hash digest
SHA256 8c80a763f9a8d32edf85938a2911cdb2523c0a0a5e0bb639325d6a7d99993a3f
MD5 3735996a458bcc8178dce3082c51fd0e
BLAKE2b-256 80bab31c3fb8ec68b3c31c2d60b9d04d0bab0438f509288ecbc30e4135340eb1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: khmer_nlp_kcc-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 12.0 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1d777e0e20b8106d5edb017f116440435668bd7297a95c209d0701524625429f
MD5 07f429ca07c6c1089971c8375666c6c6
BLAKE2b-256 61f2bcb8c48a8dfbe4e722174ae42fc8602acd8b965a63ddbb36a1d1adf5f0b7

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