Skip to main content

Scikit-compatible Japanese text vectorizer for CPU-based Japanese natural language processing.

Project description

sumire

形態素解析器などの事前インストールなしで使える, CPUベースの日本語自然言語処理のための, Scikit-learn互換の日本語の単語分割器と, テキストのベクトル化ツール.

PyPI - Version PyPI - Python Version Lint Test Coverage


Table of Contents

Installation

pre-requirements

  • Tested OS: ubuntu 22.04.
  • python >=3.9
  • make
  • cmake
  • git

MeCab-IPAdic-Neologdや, MeCab-Unidic-Neologdを使う場合, もしかしたらgitアカウントにログインが必要かもしれません (TODO: テスト).

# Jumanpp dependencies.
sudo apt update -y;
sudo apt install -y cmake libeigen3-dev libprotobuf-dev protobuf-c-compiler;

pip install sumireだけで, MeCabもJumanppも, インストールなしで使えます. MeCabやJumanppの実行バイナリや各種辞書がなければ, $HOME/.local/sumire/にTokenizerをインスタンス化した時にインストールされます.

Usage

Tokenizer usage

from sumire.tokenizer import MecabTokenizer, JumanppTokenizer


text = "これはテスト文です。" 
texts = ["これはテスト文です。", "別のテキストもトークン化します。"]

mecab = MecabTokenizer("unidic-lite")
text_mecab_tokenized = mecab.tokenize(text)
texts_mecab_tokenized = mecab.tokenize(texts)

jumanpp = JumanppTokenizer()
jumanpp.tokenize(text)
text_jumanpp_tokenized = jumanpp.tokenize(text)
texts_jumanpp_tokenized = jumanpp.tokenize(texts)

Vectorizer usage

from sumire.tokenizer.mecab import MecabTokenizer
from sumire.vectorizer.count import CountVectorizer
from sumire.vectorizer.swem import W2VSWEMVectorizer
from sumire.vectorizer.transformer_emb import TransformerEmbeddingVectorizer

texts = ["これはテスト文です。", "別のテキストもトークン化します。"]

count_vectorizer = CountVectorizer()  # this automatically use MecabTokenizer()
swem_vectorizer = W2VSWEMVectorizer()
bert_cls_vectorizer = TransformerEmbeddingVectorizer()

# fit and transform at the same time. (Of course, you can .fit() and .transform() separately!)
count_vectorized = count_vectorizer.fit_transform(texts)
swem_vectorized = swem_vectorizer.fit_transform(texts)
bert_cls_vectorized = bert_cls_vectorizer.fit_transform(texts)

# save and load vectorizer.
count_vectorizer.save_pretrained("path/to/count_vectorizer")
count_vectorizer = CountVectorizer.from_pretrained("path/to/count_vectorizer")
swem_vectorizer.save_pretrained("path/to/swem_vectorizer")
swem_vectorizer = W2VSWEMVectorizer.from_pretrained("path/to/swem_vectorizer")
bert_cls_vectorizer.save_pretrained("path/to/bert_cls_vectorizer")
bert_cls_vectorizer = TransformerEmbeddingVectorizer.from_pretrained("path/to/beert_cls_vectorizer")

各単語分割器や文分散表現モジュールの詳細なドキュメントはドキュメントページを参照してください. また, Transformersやgensimの動作済みmodelの情報は, /sumire/resources/model_cardを参照してください.

Development background

LLMの隆盛に伴い, 検索, 感情分析, その他テキスト分類・回帰などの日本語のNLPの実用タスクへの注目も高まりつつある. これらの基本的なタスクにおいて, 日本語のテキストを単語分割や, 単語や文の分散表現を得ることは, 最も基礎的な処理の一つである. LLMの時代において, 事前訓練済みTransformerモデルのチューニングや, Open AI APIによるEmbeddingsは, テキスト分散表現技術において最も重要な技術であることはいうまでもなく, また, 簡単に実装できるといえば実装できる.

しかし, 実用の現場において, BERTや, OpenAI APIなどの, 高価なGPUが必要な手法や, 1 Queryごとに費用が発生するAPIを用いた最先端の手法を使うことは, 計算量・運用コストの両面から負荷が高い. また, データセット構築段階などのプロジェクトの初期段階での概念実証 (PoC) において, 辞書データや形態素解析器のめんどくさいインストール作業や, それぞれやや異なるAPIのメソッドやプロパティを調べながら作業を行うのは少しばかり手間である.

これらの点を踏まえて, GPUがあるとは限らない手元環境で, PoCにおけるモデリング・分析部分へ速やかに注力できように, Scikit-learnのように, 機能ごとに統一的なAPIインターフェースで, テキストを与えればとりあえず色々な文の分散表現を取得できるライブラリを開発した.

Unmotivated development tasks (at this moment.)

  • Open-AI Embedding modelを使うこと. (高い.)
  • 事前訓練済みTransformerモデルによるEmbeddingについて, GPUが必要なチューニング機能を実装すること. (手元にGPUがない.)
  • 実行速度のためにライブラリ内部の可読性を大きく下げること.
    • 小規模なPoCにおいて, コードの実行速度より, 実装速度のほうが重要である.
    • PoC後の大規模な運用にて, 速度やディスク容量が問題になった場合があれば, 本ライブラリ中の不要な処理をそれぞれの開発者が削除したりカスタマイズしやすいように, 可読性を維持することが望ましい.

Roadmap (motivated development tasks)

  • vectorizer inputsのdecode().
  • Google colabでの動作環境検証.

Coding rule

https://pep8-ja.readthedocs.io/ja/latest/

License

sumire is distributed under the terms of the MIT license.

Acknowledgements (Dependent libraries, data, and models.)

See dependent_licenses.csv.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sumire-1.0.1.tar.gz (32.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sumire-1.0.1-py3-none-any.whl (57.6 kB view details)

Uploaded Python 3

File details

Details for the file sumire-1.0.1.tar.gz.

File metadata

  • Download URL: sumire-1.0.1.tar.gz
  • Upload date:
  • Size: 32.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for sumire-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1db0ff9c1d98727970b58f613f4b3abf42184320a0a285939d2b33fa0ea95706
MD5 924495a1a16a56e365859a877b31acf7
BLAKE2b-256 dd2be9a378e1f51bc4df4457e19252162752d4c72c813377d4f112cf170081ce

See more details on using hashes here.

File details

Details for the file sumire-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: sumire-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 57.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for sumire-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6c72f8fb4728a3aa82180c883ac66ad524760d1dae96996b101c1ea97bd6ba63
MD5 9f9843fa4ac55834291d0c5eb28354c2
BLAKE2b-256 33e72207476a505a1398b419963e273321e494d7100492e78308f31757e1d0fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page