Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
6b81d938
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
6b81d938
编写于
7月 16, 2018
作者:
G
guosheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add util.py to decode wordpiece ids in Transformer
上级
b0dc90db
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
73 addition
and
0 deletion
+73
-0
fluid/neural_machine_translation/transformer/util.py
fluid/neural_machine_translation/transformer/util.py
+73
-0
未找到文件。
fluid/neural_machine_translation/transformer/util.py
0 → 100644
浏览文件 @
6b81d938
import
sys
import
re
import
six
import
unicodedata
# Regular expression for unescaping token strings.
# '\u' is converted to '_'
# '\\' is converted to '\'
# '\213;' is converted to unichr(213)
# Inverse of escaping.
_UNESCAPE_REGEX
=
re
.
compile
(
r
"\\u|\\\\|\\([0-9]+);"
)
# This set contains all letter and number characters.
_ALPHANUMERIC_CHAR_SET
=
set
(
six
.
unichr
(
i
)
for
i
in
range
(
sys
.
maxunicode
)
if
(
unicodedata
.
category
(
six
.
unichr
(
i
)).
startswith
(
"L"
)
or
unicodedata
.
category
(
six
.
unichr
(
i
)).
startswith
(
"N"
)))
def
tokens_to_ustr
(
tokens
):
"""
Convert a list of tokens to a unicode string.
"""
token_is_alnum
=
[
t
[
0
]
in
_ALPHANUMERIC_CHAR_SET
for
t
in
tokens
]
ret
=
[]
for
i
,
token
in
enumerate
(
tokens
):
if
i
>
0
and
token_is_alnum
[
i
-
1
]
and
token_is_alnum
[
i
]:
ret
.
append
(
u
" "
)
ret
.
append
(
token
)
return
""
.
join
(
ret
)
def
subtoken_ids_to_tokens
(
subtoken_ids
,
vocabs
):
"""
Convert a list of subtoken(wordpiece) ids to a list of tokens.
"""
concatenated
=
""
.
join
(
[
vocabs
.
get
(
subtoken_id
,
u
""
)
for
subtoken_id
in
subtoken_ids
])
split
=
concatenated
.
split
(
"_"
)
ret
=
[]
for
t
in
split
:
if
t
:
unescaped
=
unescape_token
(
t
+
"_"
)
if
unescaped
:
ret
.
append
(
unescaped
)
return
ret
def
unescape_token
(
escaped_token
):
"""
Inverse of encoding escaping.
"""
def
match
(
m
):
if
m
.
group
(
1
)
is
None
:
return
u
"_"
if
m
.
group
(
0
)
==
u
"
\\
u"
else
u
"
\\
"
try
:
return
six
.
unichr
(
int
(
m
.
group
(
1
)))
except
(
ValueError
,
OverflowError
)
as
_
:
return
u
"
\u3013
"
# Unicode for undefined character.
trimmed
=
escaped_token
[:
-
1
]
if
escaped_token
.
endswith
(
"_"
)
else
escaped_token
return
_UNESCAPE_REGEX
.
sub
(
match
,
trimmed
)
def
subword_ids_to_str
(
ids
,
vocabs
):
"""
Convert a list of subtoken(word piece) ids to a native string.
Refer to SubwordTextEncoder in Tensor2Tensor.
"""
return
tokens_to_ustr
(
subtoken_ids_to_tokens
(
ids
,
vocabs
)).
decode
(
"utf-8"
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录