Metadata-Version: 2.5
Name: openthai2.0-nlp-core-lite
Version: 0.2.2
Summary: All core Thai NLP tasks on the CPU from one small model: sentences, clauses, words, POS tags, named entities and parse trees
Author: OpenThai, iApp Technology Co., Ltd.
License-Expression: Apache-2.0 AND CC-BY-SA-4.0 AND Unicode-DFS-2016
License-File: LICENSES/Apache-2.0.txt
License-File: LICENSES/CC-BY-SA-4.0.txt
License-File: LICENSES/Unicode-DFS-2016.txt
License-File: NOTICE
Keywords: constituency parsing,dependency parsing,named entity recognition,nlp,onnx,pos tagging,sentence segmentation,thai,word segmentation
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Natural Language :: Thai
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Text Processing :: Linguistic
Requires-Python: >=3.10
Requires-Dist: numpy>=1.26
Requires-Dist: onnxruntime>=1.16
Description-Content-Type: text/markdown

# OpenThai2.0-NLP-Core-Lite

All core Thai NLP tasks in one lightweight network, from the OpenThai 2.0 model family. From raw text, it splits
sentences and clauses, segments words, tags parts of speech, builds dependency and constituency trees, and finds
named entities, on one CPU thread.

- **One network instead of a chain of tools:** sentences, clauses, words in three units, POS tags (UD, and 37 tags
  based on the ORCHID tag set), dependency trees (UD), constituency trees (Penn Treebank style) and named entities
  (the 16 types of ThaiNER). All tasks share one network and read the same words.
- **Text without spaces:** it finds sentence ends in running Thai text and in speech transcripts, where spaces are
  missing.
- **Lightweight, CPU only:** 26.5M parameters in 31 MB of int8 model files. It needs only ONNX Runtime and NumPy,
  no GPU.
- **Fast on a CPU:** on one thread, one line per call, about 25,000 characters per second for sentences, 20,000 for
  POS tags, 18,000 for named entities and 8,900 for dependency trees. That is 14 to 69 times as fast as the
  best other tool of each task.
- **Commercial use:** trained only on data whose licences allow commercial use. Model: CC BY-SA 4.0.

## Tasks

Four examples, as the model gives them:

| Task | Output |
|---|---|
| Words, `long` | เจ้าหน้าที่ \| การศึกษา \| เปิดเผย \| ว่า |
| Words, `short` (default) | เจ้า \| หน้าที่ \| การ \| ศึกษา \| เปิด \| เผย \| ว่า |
| Words, `minimal` | เจ้า \| หน้า \| ที่ \| การ \| ศึกษา \| เปิด \| เผย \| ว่า |
| Sentences | ร้านนี้อาหารอร่อยแต่ราคาค่อนข้างแพง ‖ ครั้งหน้าจะลองสั่งเมนูอื่นดู |
| Clauses | ร้านนี้อาหารอร่อย ‖ แต่ราคาค่อนข้างแพง ‖ ครั้งหน้าจะลองสั่งเมนูอื่นดู |
| POS, UD | นักเรียน NOUN · อ่าน VERB · หนังสือ NOUN · ใน ADP · ห้องสมุด NOUN |
| POS, ORCHID-based | นักเรียน NCMN · อ่าน VTRA · หนังสือ NCMN · ใน RPRE · ห้องสมุด NCMN |
| Dependencies | อ่าน is the root, over นักเรียน (nsubj), หนังสือ (obj) and ห้องสมุด (obl); ใน hangs on ห้องสมุด (case) |
| Constituency | (S (NP (NCMN นักเรียน)) (VP (VTRA อ่าน) (NP (NCMN หนังสือ)) (PP (RPRE ใน) (NP (NCMN ห้องสมุด))))) |
| Named entities | กรมอุตุนิยมวิทยา ORGANIZATION · จังหวัดเชียงใหม่ LOCATION · 3 ธันวาคม 2569 DATE, in กรมอุตุนิยมวิทยาประกาศเตือนฝนตกหนักในจังหวัดเชียงใหม่ตั้งแต่วันที่ 3 ธันวาคม 2569 |

## Quick start

```bash
pip install "iapp-ai[nlp]"
```

One call runs every task and returns one document:

```python
from iapp_ai.nlp import core

doc = core.analyze("นักเรียนอ่านหนังสือในห้องสมุด")
for w in doc.sentences[0].words:
    print(w.id, w.text, w.upos, w.head, w.deprel)
# 1 นักเรียน NOUN 2 nsubj
# 2 อ่าน VERB 0 root
# 3 หนังสือ NOUN 2 obj
# 4 ใน ADP 5 case
# 5 ห้องสมุด NOUN 2 obl
doc.sentences[0].tree   # (S (NP (NCMN นักเรียน)) (VP (VTRA อ่าน) (NP (NCMN หนังสือ)) (PP (RPRE ใน) (NP (NCMN ห้องสมุด)))))
core.analyze("กรมอุตุนิยมวิทยาประกาศเตือนฝนตกหนักในจังหวัดเชียงใหม่", tasks=["ner"]).entities
# [Entity(text='กรมอุตุนิยมวิทยา', label='ORGANIZATION', start=0, end=16),
#  Entity(text='จังหวัดเชียงใหม่', label='LOCATION', start=37, end=53)]
```

Or one function per task:

```python
from iapp_ai.nlp import core

core.tokenize("เจ้าหน้าที่การศึกษาเปิดเผยว่า")                # ['เจ้า', 'หน้าที่', 'การ', 'ศึกษา', 'เปิด', 'เผย', 'ว่า']
core.tokenize("เจ้าหน้าที่การศึกษาเปิดเผยว่า", unit="long")   # ['เจ้าหน้าที่', 'การศึกษา', 'เปิดเผย', 'ว่า']
core.sent_tokenize("ร้านนี้อาหารอร่อยแต่ราคาค่อนข้างแพงครั้งหน้าจะลองสั่งเมนูอื่นดู")
# ['ร้านนี้อาหารอร่อยแต่ราคาค่อนข้างแพง', 'ครั้งหน้าจะลองสั่งเมนูอื่นดู']
core.pos_tag("นักเรียนอ่านหนังสือในห้องสมุด")
# [('นักเรียน', 'NOUN'), ('อ่าน', 'VERB'), ('หนังสือ', 'NOUN'), ('ใน', 'ADP'), ('ห้องสมุด', 'NOUN')]
```

`pip install "iapp-ai[nlp]"` installs NLP Core with iApp's Python package, as `iapp_ai.nlp.core`. The model
package alone is `pip install openthai2.0-nlp-core-lite`, with the same functions in `openthai_nlp_core_lite`. It
holds the model and runs offline on the CPU, with NumPy and ONNX Runtime (Python 3.10 or later).

## Functions

| To get | Call |
|---|---|
| every task at once | `analyze(text)`: one document with its sentences, clauses, words (POS tags, heads, relations), trees and named entities; `tasks=["pos", "ner"]` for some of them |
| words | `tokenize(text)`, or `word_tokenize(text)` without the whitespace tokens |
| words of many lines, other readings | `tokenize_batch(texts)`, `tokenize_nbest(text, n=3)` |
| sentences | `sent_tokenize(text)`, or `sent_spans(text)` with their offsets |
| clauses | `clause_tokenize(text)`, or `clause_spans(text)` |
| POS tags | `pos_tag(text, tagset="ud")`, with `"ud"` (17 tags) or `"orchid"` (37 tags based on ORCHID); also on a list of words |
| dependency trees | `dependency_parse(text)`, one tree per sentence |
| constituency trees | `constituency_parse(text)`, one Penn tree per sentence |
| named entities | `ner(text)`: an Entity (text, label, start, end) for each entity, or `ner_batch(texts)` |

Every task reads the same words. `tokenize`, `tokenize_batch` and `tokenize_nbest` take the arguments of the
OpenThai 2.0 word segmenter. Only what a call needs runs: words
load the word part of the model alone, and `pos_tag` never loads the parsers.

## Options

| To | Use |
|---|---|
| choose the unit | `unit="long"`, `"short"` (the default) or `"minimal"` |
| see other readings | `tokenize_nbest(text, n=3)`: the n most probable segmentations, each with its probability |
| read many lines faster | `analyze_batch(texts)`, `tokenize_batch(texts)`, `ner_batch(texts)`, or `NLPCore().pipe(lines)` for a file |
| save the results | `doc.to_dict()` for JSON, `doc.to_conllu()` for CoNLL-U |
| add your own words | `NLPCore(extra_words=[...])`: names or terms the dictionary lacks (below) |
| use more threads | `NLPCore(threads=4)` |
| use the command line | `openthai-nlp-core-lite --task all < input.txt` (one JSON object per line), or `--task ner`, `--format conllu` |

Your own words are hints that the model reads, not cuts it must make. They reach every task. In the model
package alone, the argument is `user_words`:

```python
from iapp_ai.nlp import NLPCore

nlp = NLPCore(extra_words=["ไอโอที", "ดิจิทัลทวิน"])
nlp.tokenize("โรงงานใช้ระบบไอโอทีกับดิจิทัลทวิน", unit="long")
# ['โรงงาน', 'ใช้', 'ระบบ', 'ไอโอที', 'กับ', 'ดิจิทัลทวิน']   (without them: ไอโอ | ที, ดิจิทัล | ทวิน)
```

## In iApp's package

`pip install "iapp-ai[nlp]"` installs NLP Core with [iapp-ai](https://pypi.org/project/iapp-ai/), iApp's Python
package, next to its word segmenter. The two stay apart: `iapp_ai.nlp.word_tokenize` and the other functions of the
segmenter give its words, and `iapp_ai.nlp.core` gives the outputs of NLP Core, whose tags and trees read its own
words.

## Results

NLP Core against the state-of-the-art (SOTA) system of each task, the best tool from other authors, on raw
text. Each system in this table gets what NLP Core gets:

| Task | Score and test data | SOTA system | Size | SOTA | NLP Core | Speed |
|---|---|---|---:|---:|---:|---:|
| Sentences | boundary F1, 12 sets | SaT-12l-sm, tuned | 277M | 77.5 | **78.6**† | 14× |
| Clauses | boundary F1, 3 treebanks | HoogBERTa | 143M | 30.0\* | 70.8 | 25× |
| Words | word F1, 7 sets | SEFR CUT-best | 0.54M | 84.5 | **85.7**† | 20× |
| POS tags | UPOS F1, 3 treebanks | Stanza 1.15 accurate | 179M | 73.1 | **74.7**† | 69× |
| Dependency trees | LAS F1, 3 treebanks | | 259M | 49.8 | **52.8**† | 59× |
| Constituency trees | bracket F1, TUD | | 259M | 78.6\* | **79.4** | 58× |
| Named entities | entity F1, ThaiNER | phayathaibert-thainer | 277M | **85.7** | 84.7 | 19× |

- Stanza 1.15 accurate gives the POS tags and both kinds of tree, so the table names it once. It reaches
  constituency trees by converting its dependency trees.
- Scores over several sets are means. The 12 sentence sets are four sets, each as written, without spaces and as
  speech transcripts, and the three treebanks are UD Thai-TUD, PUD and TDT.
- Bold marks the higher score, † a lead beyond the 95% interval of a paired bootstrap (a higher score without † is a
  tie), and \* the closest tool where no SOTA system exists. Clauses get no bold, as HoogBERTa cuts another unit and
  can end a clause only at a space.
- Speed is how many times as fast NLP Core runs, on one CPU core and one line per call. Size is the number of
  parameters. The five SOTA systems hold 957M in all, and NLP Core 26.5M.
- What each system gets. Sentences: the threshold of SaT-12l-sm is chosen on the same development sets as that of
  NLP Core (at its default, SaT scores 74.6). On text as written the two are level, and NLP Core leads without
  spaces by 2.0 and on speech-like text by 1.9. Tags and trees: each system gets one sentence and finds its own words, and Stanza runs with its sentence
  splitter off. On TUD alone, NLP Core has 83.8 UPOS F1 against 83.1 and 67.2 LAS F1 against 65.5. Given the gold
  words of TUD, it has 92.0 UPOS accuracy against 91.4, 78.8 LAS against 76.3 and 80.2 bracket F1 against 79.6.
- Named entities: phayathaibert-thainer, like thainer-v2, tags the first piece of each newmm word, and both learnt
  from ThaiNER train, while NLP Core learnt from no ThaiNER text. On the 1,385 test texts that share no text with
  ThaiNER train, the two are level (84.8 against 85.4). They are level on all texts too, and phayathaibert-thainer
  has 277M parameters.
- Another system leads on some single sets. At gold words, Stanza 1.11 accurate tags PUD better (78.4 against
  77.8), and Stanza 1.15 default tags TDT better (80.0 against 79.3). On the older TUD release of the TUD paper,
  the models trained on its own split lead. Systems trained on a test set are not counted.
- Every system, test set and interval of sentences, clauses, POS, dependencies, constituency and named entities
  is in OpenThai2.0-NLP-Core-Benchmark, which is not public yet. The word scores come from [OpenThai2.0-WordSeg-Benchmark](https://huggingface.co/datasets/iapp/OpenThai2.0-WordSeg-Benchmark)
  1.0. The named
  entities are scored on the test split of
  [ThaiNER v2.2](https://huggingface.co/datasets/pythainlp/thainer-corpus-v2.2), on character spans.

## Training data

Its teachers learnt from UD Thai-TUD, scb-mt-en-th-2020, Tatoeba, VISTEC-TP-TH-2021, Wisesight-1000 and the CC BY-SA
rows of the khammoon data. Its sentence and clause head also learnt the training articles of ALT (NICT, CC BY 4.0).
The model also read unlabelled text from FineWeb-2, Thai Wikipedia and Wisesight. Its
named entities come from our own labels on that text and on Blognone news, written by Qwen3.8-27B (Apache-2.0) and
by a teacher trained on them. No ThaiNER, BEST, ORCHID, LST20 or other non-commercial data were used, and the
training text was filtered against every test set above. Sources, licences and attributions: the NOTICE inside
the package.

## Limitations

- Thai corpora disagree on what a word is. POS follows UD Thai-TUD, which tags many state words VERB where a
  dictionary has ADJ.
- Parsing reads short words, one sentence at a time. A long paragraph without a sentence end inside parses slowly.
- The constituency trees have phrase labels and word tags, but no function tags (-SBJ) and no empty elements. They
  were learnt from trees converted from TUD dependency trees.
- The int8 results can change in the last digits with the number of threads, or with the other texts of one
  `ner_batch` call, which can flip a borderline decision.
- Named entities follow the 16 types of ThaiNER. Rare types are weak: on ThaiNER test, FACILITY 46.4 and URL 51.7,
  against PERSON 94.0 and ORGANIZATION 85.6.

## Files

`chars.int8.onnx` and `tower.int8.onnx` hold the shared network, and each `head.*.int8.onnx` one task, with
`head.dep.npz` and `head.span.npz` for the two parsers. `manifest.json` lists the labels, the two thresholds and
the SHA-256 of every file. The Python package holds the same files with the runtime, and the dictionary and
character table of the OpenThai 2.0 word segmenter that the model reads. Licences: Apache-2.0 for the code, CC BY-SA
4.0 for the model files, and the Unicode licence for part of the dictionary. Their texts and the NOTICE are inside
the package.

