This release is a pre-release and may not be stable for production use.
JpTokenPreprocessing – Japanese Token Preprocessing
JpTokenPreprocessing is a Python library for token preprocessing. It supports filtering noize (e.g. too short token, only number or only symbol token) and normalizing (support alphabet case and unicode normalize). There are common preprocessing for natural language processing (NLP).
Usage
#coding: utf-8
# Python3
from jp_token_preprocessing import JpTokenPreprocessing
import MeCab
# Return japanese word tokens using morphological analyzer MeCab.
# And select only noun.
def tokenize(text):
tagger = MeCab.Tagger()
node = tagger.parseToNode(text)
while node:
if '名詞' in node.feature:
surface = node.surface
yield surface
node = node.next
if __name__=='__main__':
text = """
これは自然言語処理に必須な前処理のためのモジュールです。
形態素解析や、n-gramでトークン化した後のフィルタリング、正規化を補助します。
一語だけのトークンや'1234'のような数字だけのトークン、'!!'のような記号だけのトークンのフィルタリング、
全角文字'PYTHON'の半角化、英単語'Word'の小文字化といった正規化も行えます。
さらに必ず除外したいトークンをストップワードに設定することもできます。
"""
stopwords = ['これ', 'こと']
tokens = tokenize(text)
"""
>>> print(list(tokens))
['', '', '言語', '処理', '必須', '前', '処理', 'ため', 'モジュール', '形態素',
'解析', 'n', '-', 'gram', 'トー', 'クン', '化', '後', 'フィルタ', 'リング', '正規',
'化', '補助', '一語', 'トーク', 'ン', "'", '1234', "'", 'よう', '数字','トー',
'クン', "'!!'", 'よう', '記号', 'トー', 'クン', 'フィルタ', 'リング', '全角',
'文字', "'", 'PYTHON', "'", '半角', '化', '英単語', "'", 'Word',"'", '小文字',
'化', '正規', '化', '除外', 'トーク', 'ン', 'ストップ', 'ワード', '設定', 'こと']
"""
tokens = tokenize(text)
preprocessor = JpTokenPreprocessing(number=False,
symbol=False,
case='lower',
unicode='NFKC',
min_len=2,
stopwords=stopwords)
tokens = preprocessor.preprocessing(tokens)
# Return iterator of tokens. Using list() for print sample.
"""
>>> print(list(tokens))
['言語', '処理', '必須', '処理', 'ため', 'モジュール', '形態素', '解析', 'gram',
'トー', 'クン', 'フィルタ', 'リング', '正規', '補助', '一語', 'トーク', 'よう',
'数字', 'トー', 'クン', 'よう', '記号', 'トー', 'クン', 'フィルタ', 'リング',
'全角', '文字', 'python', '半角', '英単語', 'word', '小文字', '正規', '除外',
'トーク', 'ストップ', 'ワード', '設定']
"""
Installation
pip install JpTokenPreprocessing
MeCab for python3
Please apply below patch for installing and using MeCab module with python3. (2014/09/07 MeCab 0.996)
METHODS
JpTokenPreprocessing(args)
number = BOOL (default: False)
Allow only number token.
symbol = BOOL (default: False)
Allow only symbol token.
case = ‘lower’ or ‘upper’ or ‘capitalize’
Normalize alphabet case.
unicode = ‘NFC’ or ‘NFKC’ or ‘NFD’ or ‘NFKD’a (default: ‘NFKC’)
Normalize unicode string with unicodedata.normalize().
min_len = int (default: 2)
Filter out few character token. If min_len = 2 filter out token that has only 1 or 0 character.
stopwords = list (default: [])
Filter out any token that are contained in stopword list.
JpTokenPreprocessing.preprocessing(iterable)
Return preprocessed tokens iterator.
Future work
Add some hook point for extending own preprocess.
License
MIT License
Metadata
Release files for JpTokenPreprocessing 0.1.5a2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| JpTokenPreprocessing-0.1.5a2.tar.gz | 4.3 kB | Details |
Release files / JpTokenPreprocessing-0.1.5a2.tar.gz
| Download URL | JpTokenPreprocessing-0.1.5a2.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b3c4d4520cf676f2fb236aed302195c4761751569332ebd11f2ee3ab07766f85
|
|
BLAKE2b-256 checksum How to use checksums |
b9cbc5c3d000513afaad2c8b2216da83cd64cc4a7b1801d10d9a80f6bc607559
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |