Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update JanomeSubwordsTokenizer for transformers>=4.34 #336

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions bunkai/algorithm/lbd/custom_tokenizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,37 +134,37 @@ def __init__(
:arg subword_tokenizer_type: (`optional`) string (default "wordpiece") Type of subword tokenizer.
:arg cls_token: No description.
"""
super(BertTokenizer, self).__init__(
unk_token=unk_token,
sep_token=sep_token,
pad_token=pad_token,
cls_token=cls_token,
mask_token=mask_token,
**kwargs,
)

if os.path.isfile(vocab_file):
self.vocab = load_vocab(vocab_file)
else:
self.vocab = load_vocab(cached_file(vocab_file, "vocab.txt"))

# add new vocab
self.add_tokens([" ", bunkai.constant.METACHAR_LINE_BREAK])

self.ids_to_tokens = collections.OrderedDict([(ids, tok) for tok, ids in self.vocab.items()])

self.do_word_tokenize = False
self.do_subword_tokenize = True
if do_subword_tokenize:
if subword_tokenizer_type == "wordpiece":
self.subword_tokenizer = WordpieceTokenizer(vocab=self.vocab, unk_token=self.unk_token)
self.subword_tokenizer = WordpieceTokenizer(vocab=self.vocab, unk_token=str(unk_token))
elif subword_tokenizer_type == "character":
self.subword_tokenizer = CharacterTokenizer(vocab=self.vocab, unk_token=self.unk_token)
self.subword_tokenizer = CharacterTokenizer(vocab=self.vocab, unk_token=str(unk_token))
else:
raise ValueError("Invalid subword_tokenizer_type '{}' is specified.".format(subword_tokenizer_type))

self.janome_tokenizer = JanomeTokenizer()

super(BertTokenizer, self).__init__(
unk_token=unk_token,
sep_token=sep_token,
pad_token=pad_token,
cls_token=cls_token,
mask_token=mask_token,
**kwargs,
)

# add new vocab
self.add_tokens([" ", bunkai.constant.METACHAR_LINE_BREAK])

def tokenize(self, text: typing.Union[str, typing.List[str]]) -> typing.List[str]:
if isinstance(text, str):
morphemes = self.janome_tokenizer.tokenize(text)
Expand Down