pythainlp.generate
The pythainlp.generate module provides classes and functions for generating Thai text using n-gram language models.
N-gram generators
- class pythainlp.generate.Unigram(name: str = 'tnc')[source]
Text generator using Unigram
- Parameters:
name (str) – corpus name * tnc - Thai National Corpus (default) * ttc - Thai Textbook Corpus (TTC) * oscar - OSCAR Corpus
- gen_sentence(start_seq: str = '', N: int = 3, prob: float = 0.001, output_str: bool = True, duplicate: bool = False) list[str] | str[source]
Generate a sentence using the unigram model.
- Parameters:
- Returns:
list of words or a word string
- Return type:
- Example:
>>> from pythainlp.generate import Unigram
>>> gen = Unigram()
>>> gen.gen_sentence("แมว") 'แมวเวลานะนั้น'
- class pythainlp.generate.Bigram(name: str = 'tnc')[source]
Text generator using Bigram
- Parameters:
name (str) – corpus name * tnc - Thai National Corpus (default)
- gen_sentence(start_seq: str = '', N: int = 4, prob: float = 0.001, output_str: bool = True, duplicate: bool = False) list[str] | str[source]
Generate a sentence using the bigram model.
- Parameters:
- Returns:
list of words or a word string
- Return type:
- Example:
>>> from pythainlp.generate import Bigram
>>> gen = Bigram()
>>> gen.gen_sentence("แมว") 'แมวไม่ได้รับเชื้อมัน'
- class pythainlp.generate.Trigram(name: str = 'tnc')[source]
Text generator using Trigram
- Parameters:
name (str) – corpus name * tnc - Thai National Corpus (default)
- gen_sentence(start_seq: str | tuple[str, str] = '', N: int = 4, prob: float = 0.001, output_str: bool = True, duplicate: bool = False) list[str] | str[source]
Generate a sentence using the trigram model.
- Parameters:
- Returns:
list of words or a word string
- Return type:
- Example:
>>> from pythainlp.generate import Trigram
>>> gen = Trigram()
>>> gen.gen_sentence() 'ยังทำตัวเป็นเซิร์ฟเวอร์คือ'
Usage
Choose the generator class or function for the model you want, initialize it with appropriate parameters, and call its generation methods. Generated text can be used for chatbots, content generation, or data augmentation.
Example
- ::
from pythainlp.generate import Unigram
unigram = Unigram() sentence = unigram.gen_sentence(“สวัสดีครับ”) print(sentence)