atsuki-yamaguchi commited on
Commit
4c2baf6
β€’
1 Parent(s): 13b17ca

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +85 -0
README.md CHANGED
@@ -1,3 +1,88 @@
1
  ---
2
  license: cc-by-nc-sa-4.0
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: cc-by-nc-sa-4.0
3
+ datasets:
4
+ - wikipedia
5
+ - cc100
6
+ language:
7
+ - ja
8
+ library_name: transformers
9
+ pipeline_tag: fill-mask
10
  ---
11
+
12
+ BERT-base (Vaporetto + BPE)
13
+ ===
14
+
15
+ ## How to load the tokenizer
16
+ Please download the dictionary file for Vaporetto + BPE from [our GitHub repository](https://github.com/hitachi-nlp/compare-ja-tokenizer/blob/public/data/dict/vaporetto_bpe.json).
17
+ Then you can load the tokenizer by specifying the path of the dictionary file to `dict_path`.
18
+
19
+ ```python
20
+ from typing import Optional
21
+
22
+ from tokenizers import Tokenizer, NormalizedString, PreTokenizedString
23
+ from tokenizers.processors import BertProcessing
24
+ from tokenizers.pre_tokenizers import PreTokenizer
25
+ from transformers import PreTrainedTokenizerFast
26
+
27
+ import vaporetto
28
+ import textspan
29
+
30
+ class VaporettoPreTokenizer:
31
+ def __init__(self, unidic_path: str):
32
+ with open(unidic_path, 'rb') as fp:
33
+ model = fp.read()
34
+ self.tokenizer = vaporetto.Vaporetto(model, predict_tags=False)
35
+
36
+ def tokenize(self, sequence: str) -> list[str]:
37
+ tokens = self.tokenizer.tokenize(sequence)
38
+ return [token.surface() for token in tokens]
39
+
40
+ def custom_split(self, i: int, normalized_string: NormalizedString) -> list[NormalizedString]:
41
+ text = str(normalized_string)
42
+ tokens = self.tokenize(text)
43
+ tokens_spans = textspan.get_original_spans(tokens, text)
44
+ return [normalized_string[st:ed] for cahr_spans in tokens_spans for st,ed in cahr_spans]
45
+
46
+ def pre_tokenize(self, pretok: PreTokenizedString):
47
+ pretok.split(self.custom_split)
48
+
49
+ # load a pre-tokenizer
50
+ pre_tokenizer = VaporettoPreTokenizer("/path/to/bccwj-suw+unidic+tag.model.zst")
51
+
52
+ # load a tokenizer
53
+ dict_path = /path/to/vaporetto_bpe.json
54
+ tokenizer = Tokenizer.from_file(dict_path)
55
+ tokenizer.post_processor = BertProcessing(
56
+ cls=("[CLS]", tokenizer.token_to_id('[CLS]')),
57
+ sep=("[SEP]", tokenizer.token_to_id('[SEP]'))
58
+ )
59
+
60
+ # convert to PreTrainedTokenizerFast
61
+ tokenizer = PreTrainedTokenizerFast(
62
+ tokenizer_object=tokenizer,
63
+ unk_token='[UNK]',
64
+ cls_token='[CLS]',
65
+ sep_token='[SEP]',
66
+ pad_token='[PAD]',
67
+ mask_token='[MASK]'
68
+ )
69
+
70
+ # set a pre-tokenizer
71
+ tokenizer._tokenizer.pre_tokenizer = PreTokenizer.custom(pre_tokenizer)
72
+ ```
73
+
74
+ ```python
75
+ # Test
76
+ test_str = "γ“γ‚“γ«γ‘γ―γ€‚η§γ―ε½’ζ…‹η΄ θ§£ζžε™¨γ«γ€γ„γ¦η ”η©Άγ‚’γ—γ¦γ„γΎγ™γ€‚"
77
+ tokenizer.convert_ids_to_tokens(tokenizer(test_str).input_ids)
78
+ # -> ['[CLS]','こん','にけ','は','。','私','は','ε½’ζ…‹','η΄ ','解析','器','に','぀い','て','η ”η©Ά','γ‚’','し','て','い','ます','。','[SEP]']
79
+ ```
80
+
81
+ ## How to load the model
82
+ ```python
83
+ from transformers import AutoModelForMaskedLM
84
+ model = AutoModelForMaskedLM.from_pretrained("hitachi-nlp/bert-base_vaporetto-bpe")
85
+ ```
86
+
87
+
88
+ **See [our repository](https://github.com/hitachi-nlp/compare-ja-tokenizer) for more details!**