pythainlp.parse
The pythainlp.parse module provides dependency parsing for the Thai language. Dependency parsing is a fundamental task in natural language processing that involves identifying the grammatical relationships between words in a sentence, which helps to analyze sentence structure and meaning.
Modules
dependency_parsing
- pythainlp.parse.dependency_parsing(text: str, model: str | None = None, tag: str = 'str', engine: str = 'esupar') list[list[str]] | str[source]
Parse the dependency structure of a text.
- Parameters:
text (str) – text to be parsed
model (Optional[str]) – model to use with the engine (for esupar, transformers_ud, and ud_goeswith)
tag (str) – output type,
"str"(CoNLL-U text, default) or"list"engine (str) –
engine to use for parsing. Options are:
esupar (default) - tokenizer, POS tagger, and dependency parser using BERT/RoBERTa/DeBERTa models. GitHub
spacy_thai - tokenizer, POS tagger, and dependency parser for the Thai language, using Universal Dependencies. GitHub
transformers_ud - TransformersUD. GitHub
ud_goeswith - POS tagger and dependency parser using
goeswithfor subwordsattaparse - Thai dependency parser using Stanza and PhayaThaiBERT. GitHub
- Returns:
CoNLL-U text if
tagis"str", otherwise a list of lists of fields- Return type:
- Raises:
NotImplementedError – if the engine is not supported
Options for
modelwith the esupar engine:th (default) - KoichiYasuoka/roberta-base-thai-spm-upos model. Hugging Face
KoichiYasuoka/deberta-base-thai-upos - DeBERTa(V2) model pre-trained on Thai Wikipedia texts for POS tagging and dependency parsing. Hugging Face
KoichiYasuoka/roberta-base-thai-syllable-upos - RoBERTa model pre-trained on Thai Wikipedia texts for POS tagging and dependency parsing (syllable level). Hugging Face
KoichiYasuoka/roberta-base-thai-char-upos - RoBERTa model pre-trained on Thai Wikipedia texts for POS tagging and dependency parsing (character level). Hugging Face
To train models for esupar, see the esupar GitHub repository.
Options for
modelwith the transformers_ud engine:KoichiYasuoka/deberta-base-thai-ud-head (default) - DeBERTa(V2) model pre-trained on Thai Wikipedia texts for dependency parsing (head detection using Universal Dependencies) and question answering, derived from deberta-base-thai and trained on th_blackboard.conll. Hugging Face
KoichiYasuoka/roberta-base-thai-spm-ud-head - RoBERTa model pre-trained on Thai Wikipedia texts for dependency parsing. Hugging Face
Options for
modelwith the ud_goeswith engine:KoichiYasuoka/deberta-base-thai-ud-goeswith (default) - DeBERTa(V2) model pre-trained on Thai Wikipedia texts for POS tagging and dependency parsing (using
goeswithfor subwords). Hugging Face
- Example:
>>> from pythainlp.parse import dependency_parsing
>>> print( ... dependency_parsing("ผมเป็นคนดี", engine="esupar") ... ) 1 ผม _ PRON _ _ 3 nsubj _ SpaceAfter=No 2 เป็น _ VERB _ _ 3 cop _ SpaceAfter=No 3 คน _ NOUN _ _ 0 root _ SpaceAfter=No 4 ดี _ VERB _ _ 3 acl _ SpaceAfter=No
>>> print( ... dependency_parsing("ผมเป็นคนดี", engine="spacy_thai") ... ) 1 ผม PRON PPRS _ 2 nsubj _ SpaceAfter=No 2 เป็น VERB VSTA _ 0 ROOT _ SpaceAfter=No 3 คนดี NOUN NCMN _ 2 obj _ SpaceAfter=No
The dependency_parsing function is the core component of the pythainlp.parse module. It offers dependency parsing capabilities for the Thai language. Given a Thai sentence as input, this function parses the sentence to identify the grammatical relationships between words, creating a dependency tree that represents the sentence’s structure.
Usage
To use the dependency_parsing function for Thai dependency parsing, follow these steps:
Import the pythainlp.parse module.
Use the dependency_parsing function with a Thai sentence as input.
The function will return the dependency parsing results, which include information about the grammatical relationships between words.
Example
Here’s a basic example of how to use the dependency_parsing function:
from pythainlp.parse import dependency_parsing
# Input Thai sentence
sentence = "พี่น้องชาวบ้านกำลังเลี้ยงสตางค์ในสวน"
# Perform dependency parsing
parsing_result = dependency_parsing(sentence)
# Print the parsing result
print(parsing_result)