File size: 7,664 Bytes
c81207b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""MolParser Mobile tokenizer for Hugging Face Hub remote loading."""

from __future__ import annotations

import json
import os
import re
from pathlib import Path
from typing import Dict, Iterable, List, Optional, Sequence, Union

from huggingface_hub import hf_hub_download
from transformers import PreTrainedTokenizer


TOKENIZER_CONFIG_NAME = "tokenizer_config.json"
VOCAB_NAME = "vocab.txt"


class MolParserTokenizer(PreTrainedTokenizer):
    model_input_names = ["input_ids", "attention_mask"]
    padding_side = "right"

    def __init__(
        self,
        vocab_list: Optional[List[str]] = None,
        special_tokens: Optional[Dict[str, str]] = None,
        additional_special_tokens: Optional[Sequence[str]] = None,
        **kwargs,
    ):
        if vocab_list is None:
            vocab_list = []
        if special_tokens is None:
            special_tokens = {}
        if additional_special_tokens is None:
            additional_special_tokens = []

        self.special_tokens = {
            "cls_token": special_tokens.get("cls_token", "[CLS]"),
            "pad_token": special_tokens.get("pad_token", "[PAD]"),
            "sep_token": special_tokens.get("sep_token", "[SEP]"),
            "unk_token": special_tokens.get("unk_token", "[UNK]"),
        }
        self.additional_special_tokens = list(dict.fromkeys(additional_special_tokens))
        self.vocab_list = list(vocab_list)
        all_tokens = self._build_full_vocab(self.vocab_list)
        self.vocab = {token: idx for idx, token in enumerate(all_tokens)}
        self.ids_to_tokens = {idx: token for token, idx in self.vocab.items()}
        self._decode_skip_tokens = set(self.special_tokens.values())
        self._compile_pattern()

        super().__init__(
            cls_token=self.special_tokens["cls_token"],
            pad_token=self.special_tokens["pad_token"],
            sep_token=self.special_tokens["sep_token"],
            unk_token=self.special_tokens["unk_token"],
            bos_token=self.special_tokens["cls_token"],
            eos_token=self.special_tokens["sep_token"],
            additional_special_tokens=self.additional_special_tokens,
            **kwargs,
        )

    def _build_full_vocab(self, vocab_list: Sequence[str]) -> List[str]:
        ordered_tokens: List[str] = []
        for token in list(self.special_tokens.values()) + list(vocab_list) + list(self.additional_special_tokens):
            if token not in ordered_tokens:
                ordered_tokens.append(token)
        return ordered_tokens

    def _compile_pattern(self) -> None:
        multi_char_tokens = sorted(self.vocab.keys(), key=len, reverse=True)
        pattern = "(" + "|".join(re.escape(token) for token in multi_char_tokens) + "|.)"
        self.pattern = re.compile(pattern)

    @property
    def vocab_size(self) -> int:
        return len(self.vocab)

    def __len__(self) -> int:
        return len(self.vocab)

    def get_vocab(self) -> Dict[str, int]:
        return dict(self.vocab)

    def _tokenize(self, text: str) -> List[str]:
        return [token for token in self.pattern.findall(str(text)) if token]

    def tokenize(self, text: str, **kwargs) -> List[str]:
        return self._tokenize(text)

    def _convert_token_to_id(self, token: str) -> int:
        return self.vocab.get(token, self.unk_token_id)

    def _convert_id_to_token(self, index: int) -> str:
        return self.ids_to_tokens.get(int(index), self.unk_token)

    def convert_tokens_to_string(self, tokens: Sequence[str]) -> str:
        return "".join(tokens)

    def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
        if token_ids_1 is None:
            return list(token_ids_0)
        return list(token_ids_0) + list(token_ids_1)

    def encode(self, text: str, add_special_tokens: bool = False, **kwargs) -> List[int]:
        token_ids = [self._convert_token_to_id(token) for token in self._tokenize(text)]
        if add_special_tokens:
            return [self.bos_token_id] + token_ids + [self.eos_token_id]
        return token_ids

    def decode(self, token_ids: Iterable[int], skip_special_tokens: bool = False, **kwargs) -> str:
        tokens = [self._convert_id_to_token(idx) for idx in token_ids]
        if skip_special_tokens:
            # Match deploy/tokenizer.py: keep MolParser business tokens such as
            # <sep>, <a>, </a>, <r>, </r>, <c>, </c>, and |Sg:n|.
            tokens = [token for token in tokens if token not in self._decode_skip_tokens]
        return "".join(tokens)

    def batch_encode(self, texts: Sequence[str], add_special_tokens: bool = False) -> List[List[int]]:
        return [self.encode(text, add_special_tokens=add_special_tokens) for text in texts]

    def batch_decode(
        self,
        sequences: Sequence[Sequence[int]],
        skip_special_tokens: bool = False,
        **kwargs,
    ) -> List[str]:
        return [self.decode(ids, skip_special_tokens=skip_special_tokens, **kwargs) for ids in sequences]

    def to_dict(self) -> Dict[str, object]:
        return {
            "vocab_list": self.vocab_list,
            "special_tokens": self.special_tokens,
            "additional_special_tokens": self.additional_special_tokens,
            "tokenizer_class": self.__class__.__name__,
            "auto_map": {
                "AutoTokenizer": [
                    "tokenization_molparser_mobile.MolParserTokenizer",
                    None,
                ]
            },
        }

    @classmethod
    def from_dict(cls, config: Dict[str, object]) -> "MolParserTokenizer":
        return cls(
            vocab_list=list(config["vocab_list"]),
            special_tokens=dict(config["special_tokens"]),
            additional_special_tokens=list(config.get("additional_special_tokens", [])),
        )

    def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None):
        path = Path(save_directory)
        path.mkdir(parents=True, exist_ok=True)
        name = f"{filename_prefix}-{VOCAB_NAME}" if filename_prefix else VOCAB_NAME
        vocab_path = path / name
        vocab_path.write_text("\n".join(self.vocab_list) + "\n", encoding="utf-8")
        return (str(vocab_path),)

    def save_pretrained(self, save_directory: str, **kwargs):
        os.makedirs(save_directory, exist_ok=True)
        config_path = os.path.join(save_directory, TOKENIZER_CONFIG_NAME)
        with open(config_path, "w", encoding="utf-8") as f:
            json.dump(self.to_dict(), f, ensure_ascii=False, indent=2)
        vocab_files = self.save_vocabulary(save_directory)
        return (config_path, *vocab_files)

    @classmethod
    def from_pretrained(cls, pretrained_model_name_or_path: str, *args, **kwargs) -> "MolParserTokenizer":
        config_path = Path(pretrained_model_name_or_path)
        if config_path.is_dir():
            config_path = config_path / TOKENIZER_CONFIG_NAME
        elif config_path.is_file():
            pass
        else:
            config_path = Path(
                hf_hub_download(
                    repo_id=str(pretrained_model_name_or_path),
                    filename=TOKENIZER_CONFIG_NAME,
                    repo_type=kwargs.get("repo_type"),
                    revision=kwargs.get("revision"),
                    cache_dir=kwargs.get("cache_dir"),
                    token=kwargs.get("token"),
                    local_files_only=kwargs.get("local_files_only", False),
                )
            )
        with open(config_path, "r", encoding="utf-8") as f:
            config = json.load(f)
        return cls.from_dict(config)


__all__ = ["MolParserTokenizer"]