MCPcopy Create free account
hub / github.com/SooLab/CGFormer / split_on_token

Method split_on_token

bert/tokenization_utils.py:284–335  ·  view source on GitHub ↗
(tok, text)

Source from the content-addressed store, hash-verified

282 text = re.sub(pattern, lambda m: m.groups()[0] or m.groups()[1].lower(), text)
283
284 def split_on_token(tok, text):
285 result = []
286 tok_extended = all_special_tokens_extended.get(tok, None)
287 split_text = text.split(tok)
288 full_word = ""
289 for i, sub_text in enumerate(split_text):
290 # AddedToken can control whitespace stripping around them.
291 # We use them for GPT2 and Roberta to have different behavior depending on the special token
292 # Cf. https://github.com/huggingface/transformers/pull/2778
293 # and https://github.com/huggingface/transformers/issues/3788
294 if isinstance(tok_extended, AddedToken):
295 if tok_extended.single_word:
296 # Try to avoid splitting on token
297 if (
298 i < len(split_text) - 1
299 and not _is_end_of_word(sub_text)
300 and not _is_start_of_word(split_text[i + 1])
301 ):
302 # Don't extract the special token
303 full_word += sub_text + tok
304 elif full_word:
305 full_word += sub_text
306 result += [full_word]
307 full_word = ""
308 continue
309 # Strip white spaces on the right
310 if tok_extended.rstrip and i > 0:
311 # A bit counter-intuitive but we strip the left of the string
312 # since tok_extended.rstrip means the special token is eating all white spaces on its right
313 sub_text = sub_text.lstrip()
314 # Strip white spaces on the left
315 if tok_extended.lstrip and i < len(split_text) - 1:
316 sub_text = sub_text.rstrip() # Opposite here
317 else:
318 # We strip left and right by default
319 if i < len(split_text) - 1:
320 sub_text = sub_text.rstrip()
321 if i > 0:
322 sub_text = sub_text.lstrip()
323
324 if i == 0 and not sub_text:
325 result += [tok]
326 elif i == len(split_text) - 1:
327 if sub_text:
328 result += [sub_text]
329 else:
330 pass
331 else:
332 if sub_text:
333 result += [sub_text]
334 result += [tok]
335 return result
336
337 def split_on_tokens(tok_list, text):
338 if not text.strip():

Callers

nothing calls this directly

Calls 2

_is_end_of_wordFunction · 0.85
_is_start_of_wordFunction · 0.85

Tested by

no test coverage detected