Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pinyin2hanzi
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
邵子睿(21软)
pinyin2hanzi
Commits
d833c315
Commit
d833c315
authored
Jan 17, 2022
by
szr712
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改logits拼接函数
parent
ea10822c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
4 deletions
+37
-4
shengmu.txt
data/voc/shengmu.txt
+22
-0
translate_file2.py
translate_file2.py
+15
-4
No files found.
data/voc/shengmu.txt
0 → 100644
View file @
d833c315
b
m
d
p
f
t
n
g
j
h
x
ch
r
c
l
k
q
zh
sh
z
s
\ No newline at end of file
translate_file2.py
View file @
d833c315
...
...
@@ -30,11 +30,20 @@ def get_yunmus(file_path):
return
yunmus
def
get_hanzi_logits
(
logits
,
SRC
,
TRG
,
opt
):
def
get_hanzi_logits
_yunmu
(
src
,
logits
,
SRC
,
TRG
,
opt
):
preds
=
torch
.
argmax
(
logits
,
dim
=-
1
)
result
=
torch
.
randn
(
1
,
logits
.
shape
[
2
])
.
cuda
()
for
i
,
tok
in
enumerate
(
preds
[
0
][:]):
if
'
\u4e00
'
<=
TRG
.
vocab
.
itos
[
tok
]
<=
'
\u9fa5
'
:
# 判断是否是中文
for
i
,
index
in
enumerate
(
src
[
0
][:]):
if
SRC
.
vocab
.
itos
[
index
]
in
opt
.
yunmus
:
# 判断是否是韵母
result
=
torch
.
cat
((
result
,
logits
[
0
][
i
:
i
+
1
]),
dim
=
0
)
result
=
result
.
unsqueeze
(
dim
=
0
)
return
result
def
get_hanzi_logits_shengmu
(
src
,
logits
,
SRC
,
TRG
,
opt
):
preds
=
torch
.
argmax
(
logits
,
dim
=-
1
)
result
=
torch
.
randn
(
1
,
logits
.
shape
[
2
])
.
cuda
()
for
i
,
index
in
enumerate
(
src
[
0
][:]):
if
SRC
.
vocab
.
itos
[
index
]
in
opt
.
shengmu
:
# 判断是否是声母
result
=
torch
.
cat
((
result
,
logits
[
0
][
i
:
i
+
1
]),
dim
=
0
)
result
=
result
.
unsqueeze
(
dim
=
0
)
return
result
...
...
@@ -91,7 +100,7 @@ def get_result(src, model, SRC, TRG, opt):
return
''
.
join
(
result
)
.
replace
(
"_"
,
""
)
.
replace
(
" "
,
""
)
else
:
# output=get_hanzi_logits
(
output, SRC, TRG, opt)
# output=get_hanzi_logits
_shengmu(src,
output, SRC, TRG, opt)
preds
=
torch
.
argmax
(
output
,
dim
=-
1
)
return
''
.
join
([
TRG
.
vocab
.
itos
[
tok
]
for
tok
in
preds
[
0
][:]
if
tok
.
item
()
!=
0
])
.
replace
(
"_"
,
""
)
.
replace
(
" "
,
""
)
# return ' '.join([TRG.vocab.itos[tok] for tok in preds[0][:]])
...
...
@@ -208,6 +217,8 @@ def main():
SRC
,
TRG
=
create_fields
(
opt
)
model
=
get_model_token_classification
(
opt
,
len
(
SRC
.
vocab
),
len
(
TRG
.
vocab
))
opt
.
yunmus
=
get_yunmus
(
"./data/voc/yunmus.txt"
)
opt
.
shengmu
=
get_yunmus
(
"./data/voc/shengmu.txt"
)
for
file
in
os
.
listdir
(
opt
.
test_dir
):
print
(
"filename:{}"
.
format
(
file
))
...
...
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