A tiny sentence/word tokenizer for Japanese text written in Python
Project description
🌿 Konoha: Simple wrapper of Japanese Tokenizers
Konoha
is a Python library for providing easy-to-use integrated interface of various Japanese tokenizers,
which enables you to switch a tokenizer and boost your pre-processing.
Supported tokenizers
Also, konoha
provides rule-based tokenizers (whitespace, character) and a rule-based sentence splitter.
Quick Start with Docker
Simply run followings on your computer:
docker run --rm -p 8000:8000 -t himkt/konoha # from DockerHub
Or you can build image on your machine:
git clone https://github.com/himkt/konoha # download konoha
cd konoha && docker-compose up --build # build and launch container
Tokenization is done by posting a json object to localhost:8000/api/v1/tokenize
.
You can also batch tokenize by passing texts: ["1つ目の入力", "2つ目の入力"]
to the server.
(API documentation is available on localhost:8000/redoc
, you can check it using your web browser)
Send a request using curl
on your terminal.
Note that a path to an endpoint is changed in v4.6.4.
Please check our release note (https://github.com/himkt/konoha/releases/tag/v4.6.4).
$ curl localhost:8000/api/v1/tokenize -X POST -H "Content-Type: application/json" \
-d '{"tokenizer": "mecab", "text": "これはペンです"}'
{
"tokens": [
[
{
"surface": "これ",
"part_of_speech": "名詞"
},
{
"surface": "は",
"part_of_speech": "助詞"
},
{
"surface": "ペン",
"part_of_speech": "名詞"
},
{
"surface": "です",
"part_of_speech": "助動詞"
}
]
]
}
Installation
I recommend you to install konoha by pip install 'konoha[all]'
or pip install 'konoha[all_with_integrations]'
.
(all_with_integrations
will install AllenNLP
)
- Install konoha with a specific tokenizer:
pip install 'konoha[(tokenizer_name)]
. - Install konoha with a specific tokenizer and AllenNLP integration:
pip install 'konoha[(tokenizer_name),allennlp]
. - Install konoha with a specific tokenizer and remote file support:
pip install 'konoha[(tokenizer_name),remote]'
If you want to install konoha with a tokenizer, please install konoha with a specific tokenizer
(e.g. konoha[mecab]
, konoha[sudachi]
, ...etc) or install tokenizers individually.
Example
Word level tokenization
from konoha import WordTokenizer
sentence = '自然言語処理を勉強しています'
tokenizer = WordTokenizer('MeCab')
print(tokenizer.tokenize(sentence))
# => [自然, 言語, 処理, を, 勉強, し, て, い, ます]
tokenizer = WordTokenizer('Sentencepiece', model_path="data/model.spm")
print(tokenizer.tokenize(sentence))
# => [▁, 自然, 言語, 処理, を, 勉強, し, ています]
For more detail, please see the example/
directory.
Remote files
Konoha supports dictionary and model on cloud storage (currently supports Amazon S3).
It requires installing konoha with the remote
option, see Installation.
# download user dictionary from S3
word_tokenizer = WordTokenizer("mecab", user_dictionary_path="s3://abc/xxx.dic")
print(word_tokenizer.tokenize(sentence))
# download system dictionary from S3
word_tokenizer = WordTokenizer("mecab", system_dictionary_path="s3://abc/yyy")
print(word_tokenizer.tokenize(sentence))
# download model file from S3
word_tokenizer = WordTokenizer("sentencepiece", model_path="s3://abc/zzz.model")
print(word_tokenizer.tokenize(sentence))
Sentence level tokenization
from konoha import SentenceTokenizer
sentence = "私は猫だ。名前なんてものはない。だが,「かわいい。それで十分だろう」。"
tokenizer = SentenceTokenizer()
print(tokenizer.tokenize(sentence))
# => ['私は猫だ。', '名前なんてものはない。', 'だが,「かわいい。それで十分だろう」。']
AllenNLP integration
Konoha provides AllenNLP integration, it enables users to specify konoha tokenizer in a Jsonnet config file.
By running allennlp train
with --include-package konoha
, you can train a model using konoha tokenizer!
For example, konoha tokenizer is specified in xxx.jsonnet
like following:
{
"dataset_reader": {
"lazy": false,
"type": "text_classification_json",
"tokenizer": {
"type": "konoha", // <-- konoha here!!!
"tokenizer_name": "janome",
},
"token_indexers": {
"tokens": {
"type": "single_id",
"lowercase_tokens": true,
},
},
},
...
"model": {
...
},
"trainer": {
...
}
}
After finishing other sections (e.g. model config, trainer config, ...etc), allennlp train config/xxx.jsonnet --include-package konoha --serialization-dir yyy
works!
(remember to include konoha by --include-package konoha
)
For more detail, please refer my blog article (in Japanese, sorry).
Test
python -m pytest
Article
Acknowledgement
Sentencepiece model used in test is provided by @yoheikikuta. Thanks!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file konoha-4.6.5.tar.gz
.
File metadata
- Download URL: konoha-4.6.5.tar.gz
- Upload date:
- Size: 17.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.5 CPython/3.9.5 Darwin/20.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c83023e3bda68aa910bfc115c6ab72d10b2b9478093253b4e17a20b0acf6bb7f |
|
MD5 | db61549c513bf7e3ff106682c47ca88b |
|
BLAKE2b-256 | 38da0758bd71abe53033f2a148e118e245a8eb27eff95b052480d9832b35f157 |
File details
Details for the file konoha-4.6.5-py3-none-any.whl
.
File metadata
- Download URL: konoha-4.6.5-py3-none-any.whl
- Upload date:
- Size: 20.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.5 CPython/3.9.5 Darwin/20.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d402665d1986a2494db66247faf728d999388084b5ae2abd4295172705bbb78 |
|
MD5 | 5b8309426c53d14e3211f2f7d89d8725 |
|
BLAKE2b-256 | 717048a0bd55f79c328504fe6fe7ae8ff651f77a2aadbb1911701385d9bb5ca3 |