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 checkpoint (g2p_final_trans.pt) is downloaded automatically from HuggingFace Hub on first use, just like the main model.

nlp = KhmerNLP()

# 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).

To use a local checkpoint instead:

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

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.7.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.7-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: khmer_nlp_kcc-0.2.7.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.7.tar.gz
Algorithm Hash digest
SHA256 01d770b0bdfeb4827f214f411cce6ba8f7682d48ae764814851e5f7b860a6e06
MD5 4af6c169cdb82149651a8658873060f0
BLAKE2b-256 99631ff74de7412f662441c2e90ff35da71dcae6fd3fd7c07ff1fae9e65711bc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: khmer_nlp_kcc-0.2.7-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.7-py3-none-any.whl
Algorithm Hash digest
SHA256 9b8166aa6af64c20ac88670c864173e839e7ff1c19eeb9e70a3d9891fd079991
MD5 59537e79c889e907e82595d0bc0f0307
BLAKE2b-256 21a1d098d222ee2e67420458093e3b204b9a78c85245563e993d8e0f260a5c16

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