Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
01a3966b
Commit
01a3966b
authored
Feb 04, 2019
by
thomwolf
Browse files
more options on special tokens
parent
05f96184
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
4 deletions
+9
-4
pytorch_pretrained_bert/tokenization_openai.py
pytorch_pretrained_bert/tokenization_openai.py
+9
-4
No files found.
pytorch_pretrained_bert/tokenization_openai.py
View file @
01a3966b
...
@@ -131,6 +131,10 @@ class OpenAIGPTTokenizer(object):
...
@@ -131,6 +131,10 @@ class OpenAIGPTTokenizer(object):
return
len
(
self
.
encoder
)
+
len
(
self
.
special_tokens
)
return
len
(
self
.
encoder
)
+
len
(
self
.
special_tokens
)
def
set_special_tokens
(
self
,
special_tokens
):
def
set_special_tokens
(
self
,
special_tokens
):
""" Add a list of additional tokens to the encoder.
The additional tokens are indexed starting from the last index of the
current vocabulary in the order of the `special_tokens` list.
"""
if
not
special_tokens
:
if
not
special_tokens
:
self
.
special_tokens
=
{}
self
.
special_tokens
=
{}
self
.
special_tokens_decoder
=
{}
self
.
special_tokens_decoder
=
{}
...
@@ -210,18 +214,19 @@ class OpenAIGPTTokenizer(object):
...
@@ -210,18 +214,19 @@ class OpenAIGPTTokenizer(object):
)
)
return
ids
return
ids
def
convert_ids_to_tokens
(
self
,
ids
):
def
convert_ids_to_tokens
(
self
,
ids
,
skip_special_tokens
=
False
):
"""Converts a sequence of ids in BPE tokens using the vocab."""
"""Converts a sequence of ids in BPE tokens using the vocab."""
tokens
=
[]
tokens
=
[]
for
i
in
ids
:
for
i
in
ids
:
if
i
in
self
.
special_tokens_decoder
:
if
i
in
self
.
special_tokens_decoder
:
tokens
.
append
(
self
.
special_tokens_decoder
[
i
])
if
not
skip_special_tokens
:
tokens
.
append
(
self
.
special_tokens_decoder
[
i
])
else
:
else
:
tokens
.
append
(
self
.
decoder
[
i
])
tokens
.
append
(
self
.
decoder
[
i
])
return
tokens
return
tokens
def
decode
(
self
,
ids
):
def
decode
(
self
,
ids
,
skip_special_tokens
=
False
):
"""Converts a sequence of ids in a string."""
"""Converts a sequence of ids in a string."""
tokens
=
self
.
convert_ids_to_tokens
(
ids
)
tokens
=
self
.
convert_ids_to_tokens
(
ids
,
skip_special_tokens
=
skip_special_tokens
)
out_string
=
''
.
join
(
tokens
).
replace
(
'</w>'
,
' '
)
out_string
=
''
.
join
(
tokens
).
replace
(
'</w>'
,
' '
)
return
out_string
return
out_string
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