pythainlp.classify
- class pythainlp.classify.GzipModel(training_data: list[tuple[str, str]] | None = None, model_path: str = '')[source]
Parameter-free text classifier using a gzip compressor.
This class is a re-implementation of “Low-Resource” Text Classification: A Parameter-Free Classification Method with Compressors (Jiang et al., Findings 2023).
- Parameters:
- __init__(training_data: list[tuple[str, str]] | None = None, model_path: str = '') None[source]
Initialize the model.
- training_data: NDArray[Any]
- predict(x1: str, k: int = 1) str[source]
Predict the label for the given text.
- Parameters:
- Returns:
predicted label
- Return type:
- Example:
>>> from pythainlp.classify import GzipModel
>>> training_data = [ ... ("รายละเอียดตามนี้เลยค่าา ^^", "Neutral"), ... ("กลัวพวกมึงหาย อดกินบาบิก้อน", "Neutral"), ... ("บริการแย่มากก เป็นหมอได้ไง😤", "Negative"), ... ("ขับรถแย่มาก", "Negative"), ... ("ดีนะครับ", "Positive"), ... ("ลองแล้วรสนี้อร่อย... ชอบๆ", "Positive"), ... ("ฉันรู้สึกโกรธ เวลามือถือแบตหมด", "Negative"), ... ("เธอภูมิใจที่ได้ทำสิ่งดี ๆ และดีใจกับเด็ก ๆ", "Positive"), ... ("นี่เป็นบทความหนึ่ง", "Neutral"), ... ] >>> model = GzipModel(training_data) >>> print(model.predict("ฉันดีใจ", k=1)) Positive
- class pythainlp.classify.LayaModel(model_path: str | None = None, providers: list[str] | None = None, revision: str | None = None, batch_size: int = 16, max_length: int | None = None, head_max_length: int | None = None)[source]
Laya Multilingual model for zero-shot text classification and decision making.
Laya is a non-autoregressive decision model designed for fast, calibrated, and reliable decision making. It supports over 100 languages including Thai, allowing zero-shot classification using an instruction prompt and candidate choices without any task-specific fine-tuning.
- Parameters:
model_path (Optional[str]) – Hugging Face model repository ID or local directory path containing the ONNX model files. Default is
"pythainlp/laya-multilingual-onnx".providers (Optional[list[str]]) – List of ONNX Runtime execution providers (e.g.
["CPUExecutionProvider"],["CUDAExecutionProvider"]). Default is["CPUExecutionProvider"].revision (Optional[str]) – Git revision (branch, tag, or commit hash) on Hugging Face Hub (optional).
batch_size (int) – Batch size for batched inference (default: 16).
max_length (Optional[int]) – Maximum sequence length in tokens (default from configuration, typically 1024).
head_max_length (Optional[int]) – Maximum token length for the question header and candidate options (default from configuration, typically 256).
- Example:
>>> from pythainlp.classify import LayaModel >>> model = LayaModel() >>> model.classify( ... "อาหารอร่อยมาก บรรยากาศดี พนักงานบริการยอดเยี่ยม", ... choices=["Positive", "Negative", "Neutral"], ... prompt="What is the sentiment of the text?", ... ) 'Positive'
- __init__(model_path: str | None = None, providers: list[str] | None = None, revision: str | None = None, batch_size: int = 16, max_length: int | None = None, head_max_length: int | None = None) None[source]
Initialize the Laya ONNX model and tokenizer.
- system_one(state: str | dict[str, Any] | list[Any], questions: dict[str, Any]) dict[str, Any][source]
Execute typed-decision questions over state (upstream Laya System 1 interface).
Supports question types: -
choice: Select best option among criteria. -score: Ordinal evaluation across score criteria. -noul: Binary yes/no statement evaluation.
- classify(text: str | dict[str, Any] | list[Any], choices: list[str] | dict[str, Any], prompt: str = 'Select the best choice for the given text.', return_details: bool = False) str | dict[str, Any][source]
Classify text into one of candidate choices using prompt guidance.
- Parameters:
text (Union[str, dict[str, Any], list[Any]]) – Input text or structured state.
choices (Union[list[str], dict[str, Any]]) – List of candidate choice labels or dictionary mapping each choice label to its description.
prompt (str) – Instruction prompt guiding the classification task. Default is
"Select the best choice for the given text.".return_details (bool) – If True, returns full details dictionary including choice, confidence, probabilities, and action probability. If False (default), returns the selected choice label string.
- Returns:
Selected choice string or detailed answer dictionary.
- Return type:
- classify_batch(texts: list[str | dict[str, Any] | list[Any]], choices: list[str] | dict[str, Any], prompt: str = 'Select the best choice for the given text.', return_details: bool = False) list[str | dict[str, Any]][source]
Classify multiple texts in batches.
- Parameters:
- Returns:
List of predicted choices or detailed dictionaries.
- Return type:
- predict(text: str | dict[str, Any] | list[Any], choices: list[str] | dict[str, Any] | None = None, prompt: str = 'Select the best choice for the given text.', questions: dict[str, Any] | None = None, return_details: bool = False) Any[source]
Predict classification choice or structured decisions.
If
choicesis provided, executes choice classification. Ifquestionsis provided, executes structured decision tasks.- Parameters:
- Returns:
Predicted choice or questions output.
- Return type:
Any