pythainlp.summarize

The summarize is Thai text summarizer.

Modules

pythainlp.summarize.summarize(text: str, n: int = 1, engine: str = 'frequency', tokenizer: str = 'newmm') List[str][source]

This function summarizes text based on frequency of words.

Under the hood, this function first tokenize sentence from the given text with pythainlp.tokenize.sent_tokenize(). Then, computes frequencies of tokenized words (with pythainlp.tokenize.word_tokenize()) in all sentences and normalized with maximum word frequency. The words with normalized frequncy that are less than 0.1 or greater than 0.9 will be filtered out from frequency dictionary. Finally, it picks n sentences with highest sum of normalized frequency from all words in the sentence and also appear in the frequency dictionary.

Parameters
  • text (str) – text to be summarized

  • n (int) – number of sentences to be included in the summary By default, n is 1 (effective for frequency engine only)

  • engine (str) – text summarization engine (By default: frequency).

  • tokenizer (str) – word tokenizer engine name (refer to pythainlp.tokenize.word_tokenize()). By default, tokenizer is newmm (effective for frequency engine only)

Returns

list of selected sentences

Options for engine
  • frequency (default) - frequency of words

  • 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

Example

from pythainlp.summarize import summarize

text = '''
        ทำเนียบท่าช้าง หรือ วังถนนพระอาทิตย์
        ตั้งอยู่บนถนนพระอาทิตย์ เขตพระนคร กรุงเทพมหานคร
        เดิมเป็นบ้านของเจ้าพระยามหาโยธา (ทอเรียะ คชเสนี)
        บุตรเจ้าพระยามหาโยธานราธิบดีศรีพิชัยณรงค์ (พญาเจ่ง)
        ต้นสกุลคชเสนี เชื้อสายมอญ เจ้าพระยามหาโยธา (ทอเรีย)
        เป็นปู่ของเจ้าจอมมารดากลิ่นในพระบาทสมเด็จพระจอมเกล้าเจ้าอยู่หัว
        และเป็นมรดกตกทอดมาถึง พระเจ้าบรมวงศ์เธอ กรมพระนเรศรวรฤทธิ์
        (พระองค์เจ้ากฤดาภินิหาร)
        ต่อมาในรัชสมัยพระบาทสมเด็จพระจุลจอมเกล้าเจ้าอยู่หัวโปรดเกล้าฯ
        ให้สร้างตำหนัก 2 ชั้น
        เป็นที่ประทับของพระเจ้าบรมวงศ์เธอ
        กรมพระนเรศวรฤทิธิ์และเจ้าจอมมารดา
        ต่อมาเรียกอาคารหลักนี้ว่า ตำหนักเดิม
    '''

summarize(text, n=1)
# output: ['บุตรเจ้าพระยามหาโยธานราธิบดีศรีพิชัยณรงค์']

summarize(text, n=3)
# output: ['บุตรเจ้าพระยามหาโยธานราธิบดีศรีพิชัยณรงค์',
# 'เดิมเป็นบ้านของเจ้าพระยามหาโยธา',
# 'เจ้าพระยามหาโยธา']

summarize(text, engine="mt5-small")
# output: ['<extra_id_0> ท่าช้าง หรือ วังถนนพระอาทิตย์
# เขตพระนคร กรุงเทพมหานคร ฯลฯ ดังนี้:
# ที่อยู่ - ศิลปวัฒนธรรม']