Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
ad1f7bef
Unverified
Commit
ad1f7bef
authored
Apr 29, 2021
by
Sylvain Gugger
Committed by
GitHub
Apr 29, 2021
Browse files
Reformat to make code clearer in tokenizer call (#11497)
* Reformat to make code clearer * Reformat to make code clearer
parent
f748bd42
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
37 deletions
+32
-37
src/transformers/tokenization_utils_base.py
src/transformers/tokenization_utils_base.py
+32
-37
No files found.
src/transformers/tokenization_utils_base.py
View file @
ad1f7bef
...
...
@@ -2236,47 +2236,42 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin):
:obj:`is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
"""
# Input type checking for clearer error
assert
isinstance
(
text
,
str
)
or
(
isinstance
(
text
,
(
list
,
tuple
))
and
(
len
(
text
)
==
0
or
(
isinstance
(
text
[
0
],
str
)
or
(
isinstance
(
text
[
0
],
(
list
,
tuple
))
and
(
len
(
text
[
0
])
==
0
or
isinstance
(
text
[
0
][
0
],
str
)))
)
)
),
(
def
_is_valid_text_input
(
t
):
if
isinstance
(
t
,
str
):
# Strings are fine
return
True
elif
isinstance
(
t
,
(
list
,
tuple
)):
# List are fine as long as they are...
if
len
(
t
)
==
0
:
# ... empty
return
True
elif
isinstance
(
t
[
0
],
str
):
# ... list of strings
return
True
elif
isinstance
(
t
[
0
],
(
list
,
tuple
)):
# ... list with an empty list or with a list of strings
return
len
(
t
[
0
])
==
0
or
isinstance
(
t
[
0
][
0
],
str
)
else
:
return
False
else
:
return
False
if
not
_is_valid_text_input
(
text
):
raise
ValueError
(
"text input must of type `str` (single example), `List[str]` (batch or single pretokenized example) "
"or `List[List[str]]` (batch of pretokenized examples)."
)
assert
(
text_pair
is
None
or
isinstance
(
text_pair
,
str
)
or
(
isinstance
(
text_pair
,
(
list
,
tuple
))
and
(
len
(
text_pair
)
==
0
or
(
isinstance
(
text_pair
[
0
],
str
)
or
(
isinstance
(
text_pair
[
0
],
(
list
,
tuple
))
and
(
len
(
text_pair
[
0
])
==
0
or
isinstance
(
text_pair
[
0
][
0
],
str
))
)
)
)
)
),
(
"text_pair input must of type `str` (single example), `List[str]` (batch or single pretokenized example) "
if
text_pair
is
not
None
and
not
_is_valid_text_input
(
text_pair
):
raise
ValueError
(
"text input must of type `str` (single example), `List[str]` (batch or single pretokenized example) "
"or `List[List[str]]` (batch of pretokenized examples)."
)
is_batched
=
bool
(
(
not
is_split_into_words
and
isinstance
(
text
,
(
list
,
tuple
)))
or
(
is_split_into_words
and
isinstance
(
text
,
(
list
,
tuple
))
and
text
and
isinstance
(
text
[
0
],
(
list
,
tuple
))
)
)
if
is_split_into_words
:
is_batched
=
isinstance
(
text
,
(
list
,
tuple
))
and
text
and
isinstance
(
text
[
0
],
(
list
,
tuple
))
else
:
is_batched
=
isinstance
(
text
,
(
list
,
tuple
))
if
is_batched
:
if
isinstance
(
text_pair
,
str
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment