Skip to main content

bertopic-ja

日本語の文書から、どんなトピックが話されているかを探索するなら BERTopic です。埋め込み、UMAP、HDBSCAN、c-TF-IDF という今の探索的トピックモデルの手順を、日本語のトピック語が単語になる状態で始められます。

公式のトークナイザは空白で区切るため、分かち書きしない日本語では機能しません。トピック名が単語ではなく、文のかたまりになります。このパッケージはそこを Sudachi に置き換えた入口です。公式 BERTopic のサブプロジェクトではありません。手法と実装の本体は Maarten Grootendorst の BERTopic(論文: arXiv:2203.05794)です。

インストール

uv add bertopic-ja
pip install bertopic-ja

使い方

from bertopic_ja import BERTopic

topic_model = BERTopic()
topics, probs = topic_model.fit_transform(docs)
topic_model.get_topic_info()

何も指定しないときの埋め込みは、BERTopic の language="multilingual"(paraphrase-multilingual-MiniLM-L12-v2)です。トピック語は Sudachi(分割モード C)の名詞です。1文字の名詞、代名詞、数詞は出しません。「えー」「まあ」のようなフィラーはトピック語から外し、本文には残します。「あの本」の「あの」は消しません。

トピックの粗さは min_topic_size です。UMAP と HDBSCAN は自分で組みません。

topic_model = BERTopic(min_topic_size=8)

出てきた語を調整する

トピック語から外すだけなら、埋め込みはやり直しません。

topic_model.exclude("天気", "気温")
topic_model.suggest()
topic_model.explain("このレシピって作ったことあります?")
topic_model.preview_exclude(["天気"])

クラスタの数や粗さを変えるときは、保存してある埋め込みで学習し直します。

topic_model.refit(min_topic_size=8)
topic_model.refit(nr_topics=15)

本文からフィラーを消すとクラスタが変わるので、初期値では消しません。消すときだけ fillers= を渡します。

topic_model = BERTopic(fillers=["えー", "あのー"])

別の埋め込みモデルにするときは、名前を一つ渡します。

topic_model = BERTopic(embedding_model="cl-nagoya/ruri-v3-310m")

設定ファイル

引数と YAML は同じ項目です。両方あるときは引数が優先されます。

min_topic_size: 10
nr_topics: auto
reduce_outliers: true
embedding_model: null
exclude:
  - 天気
protect:
  - レシピ
fillers: []
topic_model = BERTopic.from_yaml("config.yaml", min_topic_size=8)
topic_model.to_yaml("config.yaml")

公式との違い

公式の BERTopic(language="multilingual") は日本語の文をベクトルにできます。トピック語を作るトークナイザは英語向けの空白区切りのままで、日本語では単語に切れません。このパッケージはそのトークナイザを Sudachi に置き換え、語の除外とクラスタの再調整をメソッドにしています。

分類器でも、特定の業界向けの分析パイプラインでもありません。ラベルの付いていない文書から話題を探すためのものです。

公開

ローカルでのビルドは次です。

uv build

PyPI へ出すときは、PyPI の Trusted Publishing に GitHub リポジトリを登録してからタグを切ります。このリポジトリのワークフローはテストの実行までです。

Metadata

Release files for bertopic-ja 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bertopic-ja 0.1.1
File Size Uploaded Provenance Actions
bertopic_ja-0.1.1.tar.gz 174.3 kB Without provenance Details for bertopic_ja-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bertopic-ja 0.1.1
File Interpreter ABI Platform Actions
bertopic_ja-0.1.1-py3-none-any.whl Python 3 none any Details for bertopic_ja-0.1.1-py3-none-any.whl

Total release size: 185.8 kB

Release files / bertopic_ja-0.1.1.tar.gz

Download URL bertopic_ja-0.1.1.tar.gz
Size 174.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5a7ab1a874382f817f7fbeef50b718ac8598cdc12c221c2db4730611c22cf89c
BLAKE2b-256 checksum
How to use checksums
7bedb24a8049ffb4de66adc676a35b6c6147d1b506e4978a786b9b9c26ee4931
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.29

Release files / bertopic_ja-0.1.1-py3-none-any.whl

Download URL bertopic_ja-0.1.1-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0a89cc7fa647d0b0f1749b20996a46980041d41c7eb761b5b5e316c09d9cd6a3
BLAKE2b-256 checksum
How to use checksums
c71845b55fe5c39af297b91b3c6ecbb5032c24e9bb2ea4dcc2e303550f90a5fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.29

Security for bertopic-ja 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page