forked from to-aoki/my-pytorch-bert
/
train-sentencepiece.py
63 lines (50 loc) · 2.17 KB
/
train-sentencepiece.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# This file is based on https://github.com/yoheikikuta/bert-japanese/blob/master/src/train-sentencepiece.py.
# Fix to read the json-file to make the setting method same as other code.
#
# Author : Yohei Kikuta
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Sentencepiece training."""
import json
import glob
from typing import NamedTuple
import sentencepiece as sp
class Config(NamedTuple):
text_dir: str = "/work/data/wiki/"
prefix: str = "/work/model/wiki-ja"
vocab_size: int = 32000
ctl_symbols: str = "[PAD],[CLS],[SEP],[MASK]"
@classmethod
def from_json(cls, file):
with open(file, "r", encoding="UTF-8") as reader:
config = json.load(reader)
return cls(**config)
def _get_text_file(text_dir):
file_list = glob.glob(f'{text_dir}/*')
files = ",".join(file_list)
return files
def train(text_dir, prefix, vocab_size, ctl_symbols):
files = _get_text_file(text_dir)
command = f'--input={files} --model_prefix={prefix} --vocab_size={vocab_size} --control_symbols={ctl_symbols} --add_dummy_prefix=False --treat_whitespace_as_suffix=True'
sp.SentencePieceTrainer.Train(command)
def main(config_poth="config/test_sp.json"):
config = Config.from_json(config_poth)
print(config)
train(config.text_dir, config.prefix, config.vocab_size, config.ctl_symbols)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='SentencePiece training.', usage='%(prog)s [options]')
parser.add_argument('--config_path', help='JSON file path for defines training.', nargs='?',
type=str, default='tests/test_sp.json')
args = parser.parse_args()
main(args.config_path)