自动标记 BERTopic 生成的主题

问题描述 投票:0回答:1

我训练了 BERTopic 并获得了我的主题。我现在想为这些主题自动分配标签。我遇到了一个名为 Yake 的框架。我想知道是否有 python 代码来完成此任务,或者您是否有任何资源推荐。

python topic-modeling multilabel-classification
1个回答
0
投票

在我看来,有两种方法可以实现您的目标:

  1. 减少文档的初始关键字或词汇。亚克似乎是一种方式。 BERTopic 官方文档建议使用 KeyBERT。所以,我宁愿从 KeyBERT 而不是 Yake 开始:
from sklearn.datasets import fetch_20newsgroups
from keybert import KeyBERT

# Prepare documents 
docs = fetch_20newsgroups(subset='all',  remove=('headers', 'footers', 'quotes'))['data']

# Extract keywords
kw_model = KeyBERT()
keywords = kw_model.extract_keywords(docs)

# Create our vocabulary
vocabulary = [k[0] for keyword in keywords for k in keyword]
vocabulary = list(set(vocabulary))
# Then, we pass our vocabulary to BERTopic and train the model:

from bertopic import BERTopic
from sklearn.feature_extraction.text import CountVectorizer

vectorizer_model= CountVectorizer(vocabulary=vocabulary)
topic_model = BERTopic(vectorizer_model=vectorizer_model)
topics, probs = topic_model.fit_transform(docs)

来源:https://maartengr.github.io/BERTopic/getting_started/tips_and_tricks/tips_and_tricks.html#keybert-bertopic

  1. 将主题标签
    n
    中的单词数量减少到所需的数字(我假设主要是1):
topic_model.generate_topic_labels(nr_words=1)

来源:https://maartengr.github.io/BERTopic/api/bertopic.html#bertopic._bertopic.BERTopic.generate_topic_labels

© www.soinside.com 2019 - 2024. All rights reserved.