pythainlp.lm.phayathaibert
Note
pythainlp.phayathaibert has moved to pythainlp.lm.phayathaibert.
Importing from pythainlp.phayathaibert still works but emits a
DeprecationWarning and will be removed in 6.0.
The pythainlp.lm.phayathaibert module is built upon the phayathaibert base model.
Modules
- class pythainlp.lm.phayathaibert.ThaiTextProcessor[source]
Preprocess Thai text for PhayaThaiBERT.
- replace_newlines(text: str) str[source]
Replace newlines in text with spaces.
- param str text:
text to replace all newlines with spaces
- return:
text where all newlines are replaced with spaces
- rtype:
str
- Example:
>>> rm_useless_spaces("hey whats
- up”)
hey whats up
- replace_spaces(text: str, space_token: str = '<_>') str[source]
Replace spaces in text with a space token.
- replace_wrep_post(toks: list[str]) list[str][source]
Remove repeated words after tokenization.
The replace_wrep function of fastai does not work well with Thai.
- class pythainlp.lm.phayathaibert.ThaiTextAugmenter[source]
Generate and augment text with PhayaThaiBERT.
- generate(sample_text: str, word_rank: int, max_length: int = 3, sample: bool = False) str[source]
Generate text from PhayaThaiBERT.
- augment(text: str, num_augs: int = 3, sample: bool = False) list[str][source]
Text augmentation from PhayaThaiBERT.
- Parameters:
- Returns:
list of text augment
- Return type:
- Example:
>>> from pythainlp.augment.lm import ThaiTextAugmenter
>>> aug = ThaiTextAugmenter() >>> aug.augment("ช้างมีทั้งหมด 50 ตัว บน", num_args=5)
- [‘ช้างมีทั้งหมด 50 ตัว บนโลกใบนี้ครับ.’,
‘ช้างมีทั้งหมด 50 ตัว บนพื้นดินครับ…’, ‘ช้างมีทั้งหมด 50 ตัว บนท้องฟ้าครับ…’, ‘ช้างมีทั้งหมด 50 ตัว บนดวงจันทร์.‼’, ‘ช้างมีทั้งหมด 50 ตัว บนเขาค่ะ😁’]
- class pythainlp.lm.phayathaibert.PartOfSpeechTagger(model: str = 'lunarlist/pos_thai_phayathai', revision: str | None = None)[source]
Tag Thai text with part-of-speech labels using PhayaThaiBERT.
- __init__(model: str = 'lunarlist/pos_thai_phayathai', revision: str | None = None) None[source]
Initialize the part-of-speech tagger.
- get_tag(sentence: str, strategy: str = 'simple') list[list[tuple[str, str]]][source]
Tag sentences with part-of-speech (POS) labels.
- Parameters:
sentence (str) – a list of lists of tokenized words
- Returns:
a list of lists of tuples (word, POS tag)
- Return type:
- Example:
Labels POS for given sentence:
>>> from pythainlp.lm.phayathaibert.core import ( ... PartOfSpeechTagger, ... )
>>> tagger = PartOfSpeechTagger() >>> tagger.get_tag("แมวทำอะไรตอนห้าโมงเช้า") [[('แมว', 'NOUN'), ('ทําอะไร', 'VERB'), ('ตอนห้าโมงเช้า', 'NOUN')]]
- class pythainlp.lm.phayathaibert.NamedEntityTagger(model: str = 'Pavarissy/phayathaibert-thainer', revision: str | None = None)[source]
Tag named entities with PhayaThaiBERT.
- __init__(model: str = 'Pavarissy/phayathaibert-thainer', revision: str | None = None) None[source]
Initialize the named entity tagger.
- get_ner(text: str, tag: bool = False, pos: bool = False, strategy: str = 'simple') list[tuple[str, str]] | list[tuple[str, str, str]] | str[source]
Tag named entities in text.
- Parameters:
- Returns:
a list of tuples associated with tokenized words, NER tags, POS tags (if the parameter pos is specified as True), and output HTML-like tags (if the parameter tag is specified as True). Otherwise, return a list of tuples associated with tokenized words and NER tags
- Return type:
Union[list[tuple[str, str]], list[tuple[str, str, str]], str]
- Example:
>>> from pythainlp.lm.phayathaibert.core import NamedEntityTagger >>> >>> tagger = NamedEntityTagger() >>> tagger.get_ner("ทดสอบนายปวริศ เรืองจุติโพธิ์พานจากประเทศไทย") [('นายปวริศ เรืองจุติโพธิ์พานจากประเทศไทย', 'PERSON'), ('จาก', 'LOCATION'), ('ประเทศไทย', 'LOCATION')] >>> ner.tag("ทดสอบนายปวริศ เรืองจุติโพธิ์พานจากประเทศไทย", tag=True) 'ทดสอบ<PERSON>นายปวริศ เรืองจุติโพธิ์พาน</PERSON> <LOCATION>จาก</LOCATION><LOCATION>ประเทศไทย</LOCATION>'