-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
38 lines (33 loc) · 1.07 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# ====================================================
# Copyright (C) 2021 All rights reserved.
#
# Author : Xinyu Zhu
# Email : [email protected]
# File Name : utils.py
# Last Modified : 2021-11-28 15:16
# Describe :
#
# ====================================================
import numpy as np
def truncate_sequences(maxlen, indices, *sequences):
"""
截断直至所有的sequences总长度不超过maxlen
参数:
maxlen:
所有sequence长度之和的最大值
indices:
truncate时删除单词在sequence中的位置
sequences:
一条或多条文本
"""
sequences = [s for s in sequences if s]
if not isinstance(indices, (list, tuple)):
indices = [indices] * len(sequences)
while True:
lengths = [len(s) for s in sequences]
if sum(lengths) > maxlen:
# 对sequence中最长的那个进行truncate
i = np.argmax(lengths)
sequences[i].pop(indices[i])
else:
return sequences