The BERT models trained on Japanese text.
There are models with two different tokenization methods:
To use MecabTokenizer, you should pip install transformers["ja"] (or pip install -e .["ja"] if you install
from source) to install dependencies.
See details on cl-tohoku repository.
Example of using a model with MeCab and WordPiece tokenization:
>>> import torch
>>> from transformers import AutoModel, AutoTokenizer
>>> bertjapanese = AutoModel.from_pretrained("cl-tohoku/bert-base-japanese")
>>> tokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese")
>>> ## Input Japanese Text
>>> line = "吾輩は猫である。"
>>> inputs = tokenizer(line, return_tensors="pt")
>>> print(tokenizer.decode(inputs["input_ids"][0]))
[CLS] 吾輩 は 猫 で ある 。 [SEP]
>>> outputs = bertjapanese(**inputs)Example of using a model with Character tokenization:
>>> bertjapanese = AutoModel.from_pretrained("cl-tohoku/bert-base-japanese-char")
>>> tokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese-char")
>>> ## Input Japanese Text
>>> line = "吾輩は猫である。"
>>> inputs = tokenizer(line, return_tensors="pt")
>>> print(tokenizer.decode(inputs["input_ids"][0]))
[CLS] 吾 輩 は 猫 で あ る 。 [SEP]
>>> outputs = bertjapanese(**inputs)Tips:
This model was contributed by cl-tohoku.
( vocab_file do_lower_case = False do_word_tokenize = True do_subword_tokenize = True word_tokenizer_type = 'basic' subword_tokenizer_type = 'wordpiece' never_split = None unk_token = '[UNK]' sep_token = '[SEP]' pad_token = '[PAD]' cls_token = '[CLS]' mask_token = '[MASK]' mecab_kwargs = None sudachi_kwargs = None jumanpp_kwargs = None **kwargs )
Parameters
str) —
Path to a one-wordpiece-per-line vocabulary file.
bool, optional, defaults to True) —
Whether to lower case the input. Only has an effect when do_basic_tokenize=True.
bool, optional, defaults to True) —
Whether to do word tokenization.
bool, optional, defaults to True) —
Whether to do subword tokenization.
str, optional, defaults to "basic") —
Type of word tokenizer. Choose from [“basic”, “mecab”, “sudachi”, “jumanpp”].
str, optional, defaults to "wordpiece") —
Type of subword tokenizer. Choose from [“wordpiece”, “character”].
dict, optional) —
Dictionary passed to the MecabTokenizer constructor.
dict, optional) —
Dictionary passed to the SudachiTokenizer constructor.
dict, optional) —
Dictionary passed to the JumanppTokenizer constructor.
Construct a BERT tokenizer for Japanese text.