Skip to content

Conversation

noppayut
Copy link
Contributor

@noppayut noppayut commented Oct 29, 2022

What does this changes

Implement keyword extraction feature and unit test. (#145 )
The engine has two options, namely 'keybert' and 'frequency' for KeyBERT and naive frequency ranking respectively.
KeyBERT is an algorithm to rank keywords by cosine similarity between embedding of each ngram in a document to embedding of the whole document. Embeddings are produced by a large language model. I use airesearch/wangchanberta-base-att-spm-uncased in this work.
The original implementation has more variety to rank keywords. Please check https://github.com/MaartenGr/KeyBERT for more details and better explanations 🙂 . I'm re-implementing core features and making it support Thai.

from pythainlp.summarize import extract_keywords

text = (
    "เบียร์ เป็นหนึ่งในเครื่องดื่มแอลกอฮอล์ที่เก่าแก่ที่สุดและบริโภคกันอย่างแพร่หลายมากที่สุดในโลก "
    "และเป็นเครื่องดื่มยอดนิยมอันดับสามทั้งหมด รองจากน้ำดื่มและชา "
    "ถูกผลิตขึ้นโดยการกลั่นเบียร์ (brewing) และกระบวนการหมักของแป้ง ซึ่งส่วนใหญ่ได้มาจากธัญพืช - "
    "ส่วนมากมาจากมอลต์ข้าวบาร์เลย์ แม้กระทั่งข้าวสาลี ข้าวโพด ข้าว และข้าวโอ๊ตก็ใช้ได้เช่น ในช่วงขั้นตอนการกลั่นเบียร์ "
    "กระบวนการหมักของแป้งนั้น น้ำตาลในวอร์ต(wort)จะก่อให้เกิดเอทานอลและคาร์บอนเนชั่นในเบียร์ที่ได้ออกมา "
    "เบียร์สมัยใหม่ส่วนใหญ่จะกลั่นด้วยฮอปส์ ซึ่งจะเป็นการเพิ่มความขมและรสชาติอื่น ๆ "
    "และทำหน้าที่เป็นสารกันบูดและสารคงตัวตามธรรมชาติ สารแต่งกลิ่นรสอื่น ๆ "
    "เช่น กรู๊ต สมุนไพรหรือผลไม้ซึ่งอาจจะรวมทั้งหรือการใช้แทนฮอปส์ ในการกลั่นเบียร์เชิงพาณิชย์ "
    "ผลของการเกิดคาร์บอนเนชั่นตามธรรมชาติมักจะถูกขจัดออกในช่วงกระบวนการผลิตและแทนที่ด้วยการอัดลมด้วยคาร์บอนเนชั่นแบบบังคับ"
)

keywords = extract_keywords(text)
# output: ['wort)', 'brewing)', 'ที่เก่าแก่', 'ขจัดออก', 'อัดลมด้วย']

keywords = extract_keywords(text, engine='frequency')
# output: ['เบียร์', 'กลั่น', 'คาร์บอน', 'เนชั่น', 'เครื่องดื่ม']

What was wrong

Just a brand new feature.

How this fixes it

Fixes #145

Some issues related to airesearch/wangchanberta-base-att-spm-uncased. It's spamming warning messages upon init.

- This IS expected if you are initializing CamembertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing CamembertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
Some weights of the model checkpoint at airesearch/wangchanberta-base-att-spm-uncased were not used when initializing CamembertModel: ['lm_head.decoder.weight', 'lm_head.layer_norm.weight', 'lm_head.dense.weight', 'lm_head.layer_norm.bias', 'lm_head.decoder.bias', 'lm_head.dense.bias', 'lm_head.bias']

Your checklist for this pull request

🚨Please review the guidelines for contributing to this repository.

  • [✓] Passed code styles and structures
  • [✓] Passed code linting checks and unit test

@coveralls
Copy link

coveralls commented Oct 29, 2022

Coverage Status

Coverage decreased (-2.0%) to 92.327% when pulling 9d70de8 on noppayut:feature/keyword-extraction-keybert into 0bed123 on PyThaiNLP:dev.

@wannaphong
Copy link
Member

Thank you for pull request! Can you add the function to docs/api/summarize.rst?

@wannaphong wannaphong added hacktoberfest-accepted hacktoberfest accepted pull requests. enhancement enhance functionalities labels Oct 29, 2022
@noppayut
Copy link
Contributor Author

Sure. Please check.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Member

@wannaphong wannaphong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work 👍

@wannaphong wannaphong merged commit b002db0 into PyThaiNLP:dev Oct 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement enhance functionalities hacktoberfest-accepted hacktoberfest accepted pull requests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Keyword extraction
3 participants