pythainlp.summarize
The pythainlp.summarize module provides functions for Thai text summarization and keyword extraction.
Functions
- pythainlp.summarize.summarize(text: str, n: int = 1, engine: str = 'frequency', tokenizer: str = 'newmm') list[str][source]
Summarize text based on word frequency.
Under the hood, this function first tokenizes sentences from the given text with
pythainlp.tokenize.sent_tokenize(). Then, it computes frequencies of tokenized words (withpythainlp.tokenize.word_tokenize()) in all sentences and normalizes them with the maximum word frequency. It filters out the words with normalized frequencies that are less than 0.1 or greater than 0.9 from the frequency dictionary. Finally, it picks n sentences with the highest sum of normalized frequency from all words that are in the sentence and also appear in the frequency dictionary.- Parameters:
text (str) – text to summarize
n (int) – number of sentences in the summary (default is 1, effective for the frequency engine only)
engine (str) –
text summarization engine
frequency - word frequency (default)
mt5 - mT5-small model
mt5-small - mT5-small model
mt5-base - mT5-base model
mt5-large - mT5-large model
mt5-xl - mT5-xl model
mt5-xxl - mT5-xxl model
mt5-cpe-kmutt-thai-sentence-sum - mT5 Thai sentence summarization by CPE KMUTT
tokenizer (str) – engine to tokenize text with. See
pythainlp.tokenize.word_tokenize()for available engines (default is"newmm", effective for the frequency engine only)
- Returns:
list of selected sentences
- Return type:
- Example:
>>> from pythainlp.summarize import summarize
>>> text = ''' ... ทำเนียบท่าช้าง หรือ วังถนนพระอาทิตย์ ... ตั้งอยู่บนถนนพระอาทิตย์ เขตพระนคร กรุงเทพมหานคร ... เดิมเป็นบ้านของเจ้าพระยามหาโยธา (ทอเรียะ คชเสนี) ... บุตรเจ้าพระยามหาโยธานราธิบดีศรีพิชัยณรงค์ (พญาเจ่ง) ... ต้นสกุลคชเสนี เชื้อสายมอญ เจ้าพระยามหาโยธา (ทอเรีย) ... เป็นปู่ของเจ้าจอมมารดากลิ่นในพระบาทสมเด็จพระจอมเกล้าเจ้าอยู่หัว ... และเป็นมรดกตกทอดมาถึง พระเจ้าบรมวงศ์เธอ กรมพระนเรศรวรฤทธิ์ ... (พระองค์เจ้ากฤดาภินิหาร) ... ต่อมาในรัชสมัยพระบาทสมเด็จพระจุลจอมเกล้าเจ้าอยู่หัวโปรดเกล้าฯ ... ให้สร้างตำหนัก 2 ชั้น ... เป็นที่ประทับของพระเจ้าบรมวงศ์เธอ ... กรมพระนเรศวรฤทิธิ์และเจ้าจอมมารดา ... ต่อมาเรียกอาคารหลักนี้ว่า ตำหนักเดิม ... '''
>>> summarize(text, n=1) ['บุตรเจ้าพระยามหาโยธานราธิบดีศรีพิชัยณรงค์']
>>> summarize(text, n=3) ['บุตรเจ้าพระยามหาโยธานราธิบดีศรีพิชัยณรงค์', 'เดิมเป็นบ้านของเจ้าพระยามหาโยธา', 'เจ้าพระยามหาโยธา']
>>> summarize(text, engine="mt5-small") ['<extra_id_0> ท่าช้าง หรือ วังถนนพระอาทิตย์ เขตพระนคร กรุงเทพมหานคร ฯลฯ ดังนี้: ที่อยู่ - ศิลปวัฒนธรรม']
>>> text = "ถ้าพูดถึงขนมหวานในตำนานที่ชื่นใจที่สุดแล้วละก็ต้องไม่พ้น น้ำแข็งใส แน่ๆ เพราะว่าเป็นอะไรที่ชื่นใจสุดๆ" >>> summarize( ... text, engine="mt5-cpe-kmutt-thai-sentence-sum" ... ) ['น้ําแข็งใสเป็นอะไรที่ชื่นใจที่สุด']
- pythainlp.summarize.extract_keywords(text: str, keyphrase_ngram_range: tuple[int, int] = (1, 2), max_keywords: int = 5, min_df: int = 1, engine: str = 'keybert', tokenizer: str = 'newmm', stop_words: Iterable[str] | None = None) list[str][source]
Return the most relevant keywords (and keyphrases) from a document.
Each algorithm may produce completely different keywords, so choose the algorithm carefully.
Note
Calling
extract_keywords()is expensive. For repeated use of KeyBERT (the default engine), creating aKeyBERTobject directly is recommended.- Parameters:
text (str) – text to extract keywords from
keyphrase_ngram_range (tuple[int, int]) – range of words per keyword.
(1, 1)allows unigrams only (e.g. “เสา”, “ไฟฟ้า”);(1, 2)allows unigrams and bigrams (e.g. “เสา”, “ไฟฟ้า”, “เสาไฟฟ้า”) (default is(1, 2))max_keywords (int) – maximum number of keywords to return (default is 5)
min_df (int) – minimum word frequency to qualify as a keyword (default is 1)
engine (str) –
keyword extraction engine
keybert - KeyBERT keyword extraction (default)
frequency - word frequency
tokenizer (str) – engine to tokenize text with. See
pythainlp.tokenize.word_tokenize()for available engines (default is"newmm")stop_words (Optional[collections.abc.Iterable[str]]) – words to ignore. If None, use
pythainlp.corpus.thai_stopwords()(default is None)
- Returns:
list of keywords
- Return type:
- Example:
>>> from pythainlp.summarize import extract_keywords
>>> text = ''' ... อาหาร หมายถึง ของแข็งหรือของเหลว ... ที่กินหรือดื่มเข้าสู่ร่างกายแล้ว ... จะทำให้เกิดพลังงานและความร้อนแก่ร่างกาย ... ทำให้ร่างกายเจริญเติบโต ... ซ่อมแซมส่วนที่สึกหรอ ควบคุมการเปลี่ยนแปลงต่างๆ ในร่างกาย ... ช่วยทำให้อวัยวะต่างๆ ทำงานได้อย่างปกติ ... อาหารจะต้องไม่มีพิษและไม่เกิดโทษต่อร่างกาย ... '''
>>> keywords = extract_keywords(text)
[‘อวัยวะต่างๆ’, ‘ซ่อมแซมส่วน’, ‘เจริญเติบโต’, ‘ควบคุมการเปลี่ยนแปลง’, ‘มีพิษ’]
>>> keywords = extract_keywords( ... text, max_keywords=10 ... )
[‘อวัยวะต่างๆ’, ‘ซ่อมแซมส่วน’, ‘เจริญเติบโต’, ‘ควบคุมการเปลี่ยนแปลง’, ‘มีพิษ’, ‘ทำให้ร่างกาย’, ‘ร่างกายเจริญเติบโต’, ‘จะทำให้เกิด’, ‘มีพิษและ’, ‘เกิดโทษ’]
Keyword extraction engines
KeyBERT
Re-implement KeyBERT in a minimal form.
KeyBERT is a minimal and easy-to-use keyword extraction technique that leverages BERT embeddings to create keywords and keyphrases that are most similar to a document.
https://github.com/MaartenGr/KeyBERT
- class pythainlp.summarize.keybert.KeyBERT(model_name: str = 'airesearch/wangchanberta-base-att-spm-uncased')[source]
Extract keywords and keyphrases with the KeyBERT algorithm.
- __init__(model_name: str = 'airesearch/wangchanberta-base-att-spm-uncased') None[source]
Initialize the KeyBERT feature extraction pipeline.
- Parameters:
model_name (str) – name of the model to extract features with
- ft_pipeline: Pipeline
- extract_keywords(text: str, keyphrase_ngram_range: tuple[int, int] = (1, 2), max_keywords: int = 5, min_df: int = 1, tokenizer: str = 'newmm', return_similarity: bool = False, stop_words: Iterable[str] | None = None) list[str] | list[tuple[str, float]][source]
Extract Thai keywords and keyphrases with the KeyBERT algorithm.
See https://github.com/MaartenGr/KeyBERT.
- Parameters:
text (str) – text to extract keywords from
keyphrase_ngram_range (tuple[int, int]) – range of words per keyword. The word unit depends on the
tokenizerengine. For example,(1, 1)means each word (unigram) can be a keyword (e.g. “เสา”, “ไฟฟ้า”), and(1, 2)means one or two consecutive words (unigram and bigram) can be keywords (e.g. “เสา”, “ไฟฟ้า”, “เสาไฟฟ้า”) (default is(1, 2))max_keywords (int) – maximum number of keywords to return (default is 5)
min_df (int) – minimum word frequency to qualify as a keyword (default is 1)
tokenizer (str) – engine to tokenize text with. See
pythainlp.tokenize.word_tokenize()for available engines (default is"newmm")return_similarity (bool) – return keywords with their similarity scores (default is False)
stop_words (Optional[collections.abc.Iterable[str]]) – words to ignore. If None, use
pythainlp.corpus.thai_stopwords()(default is None)
- Returns:
list of keywords, or list of (keyword, score) tuples if
return_similarityis True- Return type:
- Example:
>>> from pythainlp.summarize.keybert import ( ... KeyBERT, ... )
>>> text = ''' ... อาหาร หมายถึง ของแข็งหรือของเหลว ... ที่กินหรือดื่มเข้าสู่ร่างกายแล้ว ... จะทำให้เกิดพลังงานและความร้อนแก่ร่างกาย ... ทำให้ร่างกายเจริญเติบโต ... ซ่อมแซมส่วนที่สึกหรอ ควบคุมการเปลี่ยนแปลงต่างๆ ในร่างกาย ... ช่วยทำให้อวัยวะต่างๆ ทำงานได้อย่างปกติ ... อาหารจะต้องไม่มีพิษและไม่เกิดโทษต่อร่างกาย ... '''
>>> kb = KeyBERT()
>>> keywords = kb.extract_keyword(text)
[‘อวัยวะต่างๆ’, ‘ซ่อมแซมส่วน’, ‘เจริญเติบโต’, ‘ควบคุมการเปลี่ยนแปลง’, ‘มีพิษ’]
>>> keywords = kb.extract_keyword( ... text, max_keywords=10, return_similarity=True ... )
[(‘อวัยวะต่างๆ’, 0.3228477063109462), (‘ซ่อมแซมส่วน’, 0.31320597838000375), (‘เจริญเติบโต’, 0.29115434699705506), (‘ควบคุมการเปลี่ยนแปลง’, 0.2678430841321016), (‘มีพิษ’, 0.24996827960821494), (‘ทำให้ร่างกาย’, 0.23876962942443258), (‘ร่างกายเจริญเติบโต’, 0.23191285218852364), (‘จะทำให้เกิด’, 0.22425422716846247), (‘มีพิษและ’, 0.22162962875299588), (‘เกิดโทษ’, 0.20773497763458507)]
- class pythainlp.summarize.keybert.KeyBERT(model_name: str = 'airesearch/wangchanberta-base-att-spm-uncased')[source]
Extract keywords and keyphrases with the KeyBERT algorithm.
- __init__(model_name: str = 'airesearch/wangchanberta-base-att-spm-uncased') None[source]
Initialize the KeyBERT feature extraction pipeline.
- Parameters:
model_name (str) – name of the model to extract features with
- ft_pipeline: Pipeline
- extract_keywords(text: str, keyphrase_ngram_range: tuple[int, int] = (1, 2), max_keywords: int = 5, min_df: int = 1, tokenizer: str = 'newmm', return_similarity: bool = False, stop_words: Iterable[str] | None = None) list[str] | list[tuple[str, float]][source]
Extract Thai keywords and keyphrases with the KeyBERT algorithm.
See https://github.com/MaartenGr/KeyBERT.
- Parameters:
text (str) – text to extract keywords from
keyphrase_ngram_range (tuple[int, int]) – range of words per keyword. The word unit depends on the
tokenizerengine. For example,(1, 1)means each word (unigram) can be a keyword (e.g. “เสา”, “ไฟฟ้า”), and(1, 2)means one or two consecutive words (unigram and bigram) can be keywords (e.g. “เสา”, “ไฟฟ้า”, “เสาไฟฟ้า”) (default is(1, 2))max_keywords (int) – maximum number of keywords to return (default is 5)
min_df (int) – minimum word frequency to qualify as a keyword (default is 1)
tokenizer (str) – engine to tokenize text with. See
pythainlp.tokenize.word_tokenize()for available engines (default is"newmm")return_similarity (bool) – return keywords with their similarity scores (default is False)
stop_words (Optional[collections.abc.Iterable[str]]) – words to ignore. If None, use
pythainlp.corpus.thai_stopwords()(default is None)
- Returns:
list of keywords, or list of (keyword, score) tuples if
return_similarityis True- Return type:
- Example:
>>> from pythainlp.summarize.keybert import ( ... KeyBERT, ... )
>>> text = ''' ... อาหาร หมายถึง ของแข็งหรือของเหลว ... ที่กินหรือดื่มเข้าสู่ร่างกายแล้ว ... จะทำให้เกิดพลังงานและความร้อนแก่ร่างกาย ... ทำให้ร่างกายเจริญเติบโต ... ซ่อมแซมส่วนที่สึกหรอ ควบคุมการเปลี่ยนแปลงต่างๆ ในร่างกาย ... ช่วยทำให้อวัยวะต่างๆ ทำงานได้อย่างปกติ ... อาหารจะต้องไม่มีพิษและไม่เกิดโทษต่อร่างกาย ... '''
>>> kb = KeyBERT()
>>> keywords = kb.extract_keyword(text)
[‘อวัยวะต่างๆ’, ‘ซ่อมแซมส่วน’, ‘เจริญเติบโต’, ‘ควบคุมการเปลี่ยนแปลง’, ‘มีพิษ’]
>>> keywords = kb.extract_keyword( ... text, max_keywords=10, return_similarity=True ... )
[(‘อวัยวะต่างๆ’, 0.3228477063109462), (‘ซ่อมแซมส่วน’, 0.31320597838000375), (‘เจริญเติบโต’, 0.29115434699705506), (‘ควบคุมการเปลี่ยนแปลง’, 0.2678430841321016), (‘มีพิษ’, 0.24996827960821494), (‘ทำให้ร่างกาย’, 0.23876962942443258), (‘ร่างกายเจริญเติบโต’, 0.23191285218852364), (‘จะทำให้เกิด’, 0.22425422716846247), (‘มีพิษและ’, 0.22162962875299588), (‘เกิดโทษ’, 0.20773497763458507)]