ja-stopword-filter
分かち書き済みの日本語トークンから、ストップワードや指定した種類のトークンを除去する Python ライブラリです。実行時の外部依存はありません。Python 3.12 以上に対応しています。
このライブラリは分かち書きを行いません。 文全体を文字列で渡さず、文字列または Token のリストを渡してください。
インストールと最小例
pip install ja-stopword-filter
配布名は ja-stopword-filter、インポート名は ja_stopword_filter です。
from ja_stopword_filter import JaStopwordFilter
words = ["これ", "は", "東京", "。", "🤖"]
print(JaStopwordFilter().remove(words)) # Output: ['は', '東京', '。', '🤖']
デフォルトで有効なのは内蔵の語リストと、照合用の NFKC 正規化だけです。内蔵リストは 310 語で、は、が、です、ます などは含みません。必要な語や品詞を追加してください。
ルールを組み合わせる
from ja_stopword_filter import JaStopwordFilter
stopword_filter = JaStopwordFilter(
use_date=True,
use_numbers=True,
use_symbols=True,
use_spaces=True,
use_emojis=True,
custom_wordlist=["は"],
)
words = ["これ", "は", "東京", "。", "!!!", "🤖", "2024年11月", "123", "", "2024年11月発売"]
print(stopword_filter.remove(words)) # Output: ['東京', '2024年11月発売']
| パラメータ | デフォルト | 動作 |
|---|---|---|
convert_full_to_half: bool |
True |
内蔵ルールの照合に NFKC を使用。出力やコールバックの入力は変えません。 |
use_slothlib: bool |
True |
内蔵の非空ストップワード 310 語を使用。 |
filter_length: int |
0 |
正規化後の長さが指定値以下のトークンを除去。0 以下では無効。 |
use_date: bool |
False |
トークン全体が YYYY年M月、M月D日、YYYY年M月D日 の形に一致すれば除去。 |
use_numbers: bool |
False |
正規化後の str.isdigit() が真なら除去。 |
use_symbols: bool |
False |
全文字が Unicode の句読点・記号カテゴリ P / S である非空トークンを除去。 |
use_spaces: bool |
False |
空文字列や、空白・タブ・改行だけのトークンを除去。 |
use_emojis: bool |
False |
元の表記に Unicode 17.0 の絵文字を含むトークン全体を除去。 |
custom_wordlist: list[str] | None |
None |
内蔵リストに追加するストップワード。 |
custom_filter: Callable[[T], bool] | None |
None |
元のトークンを受け取り、True なら除去。 |
挙動の境界
- NFKC は単なる全角→半角変換ではありません。
カタカナ→カタカナ、①→1、㍑→リットルも含みます。convert_to_halfwidth()で同じ変換を明示的に利用できます。 remove()は新しいリストを返します。順序、重複、残した文字列の元の表記、Tokenのオブジェクト同一性を保ちます。入力リストやsurfaceは変更しません。- 日付は形式の照合だけです。
2024年99月99日も一致します。実在日付の検証や2024-11-26の解析は行いません。2024年11月発売は日付ルールでは除去しません。 - 数字ルールは
123や123を除去しますが、-123、3.14、1,000、一は残します。 - 記号ルールは
。、!!!、¥を除去し、C++やa-bは残します。一部の絵文字もSカテゴリなので、このルールと絵文字ルールは重複します。複合絵文字にはuse_emojisを使用してください。 - 絵文字は Unicode の fully-qualified / minimally-qualified / unqualified 表記を検出します。
❤、国旗、肌色修飾、ZWJ、キーキャップを含みます。単独の1、#、*、国旗の片側の地域指示文字は絵文字扱いしません。絵文字列の厳密な妥当性検証ではなく、含有判定です。 - 文字数は正規化後の Python 文字列の長さです。表示上の一文字とは限りません。空白は
str.strip()に従い、ゼロ幅スペース U+200B は除去しません。 - どれか一つの有効なルールに一致すれば除去します。空文字列を自分で
custom_wordlistに追加した場合、use_spaces=Falseでも除去されます。
品詞・独自属性によるフィルタ
Token(surface, pos) の pos は呼び出し側が与える文字列です。内蔵ルールは品詞を判定しません。コールバックは内蔵ルールより先に実行され、正規化前の文字列または元の Token を受け取ります。
from ja_stopword_filter import JaStopwordFilter, Token
class CountedToken(Token):
def __init__(self, surface: str, pos: str, frequency: int) -> None:
super().__init__(surface, pos)
self.frequency = frequency
def exclude(token: CountedToken) -> bool:
return token.pos == "助詞" or token.frequency < 2
words = [CountedToken("東京", "名詞", 5), CountedToken("は", "助詞", 8), CountedToken("例", "名詞", 1)]
filtered = JaStopwordFilter(custom_filter=exclude).remove(words)
print([token.surface for token in filtered]) # Output: ['東京']
コールバックは入力の型に合わせてください。文字列と Token を混在させる場合は isinstance で分岐します。混在入力の例 と、分かち書きから実行する Janome の例 を用意しています。Janome は例専用で、本ライブラリの依存には含めません。
型注釈と py.typed を同梱しています。JaStopwordFilter はコールバックの型を推論し、remove() は入力の要素型(Token のサブクラスを含む)を維持します。
公開 API
JaStopwordFilter(...).remove(tokens):分かち書き済みのリストやタプルをフィルタ。生の文字列・bytes や、str/Token以外の要素にはTypeError。Token(surface: str, pos: str):拡張可能なトークン。残されたオブジェクトはコピーしません。get_stopwords() -> list[str]:空行を除いた内蔵語リストの新しいリストを返します。convert_to_halfwidth(text: str) -> str:Unicode NFKC を適用します。関数名は互換性のため維持しています。
License
Python の実装は Apache License 2.0、Unicode データから生成した絵文字パターンは Unicode License v3 に基づきます。
Metadata
Release files for ja-stopword-filter 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ja_stopword_filter-0.3.0.tar.gz | 46.4 kB | Without provenance | Details for ja_stopword_filter-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ja_stopword_filter-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for ja_stopword_filter-0.3.0-py3-none-any.whl |
Total release size: 62.9 kB
Release files / ja_stopword_filter-0.3.0.tar.gz
| Download URL | ja_stopword_filter-0.3.0.tar.gz |
|---|---|
| Size | 46.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bc03eaf9e3fcbdcb0ec30cb78a1c32b1614e42842ba88c3dd8f00b361b481319
|
|
BLAKE2b-256 checksum How to use checksums |
bab4de7fdb09e4b59a67b0669a6e3b814581d6e82816a68b3032a661f9355dce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / ja_stopword_filter-0.3.0-py3-none-any.whl
| Download URL | ja_stopword_filter-0.3.0-py3-none-any.whl |
|---|---|
| Size | 16.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ef02d01cbe3a03b5f4886fa017d25b8ce5e106662e579a4bee7f8bfe95f46aaa
|
|
BLAKE2b-256 checksum How to use checksums |
4e474965672f680ab257aa58e738617aef0cb49abcbff0853d7710b9659f8583
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for ja-stopword-filter 0.3.0
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.