-
Notifications
You must be signed in to change notification settings - Fork 24
/
helper.py
103 lines (90 loc) · 3.61 KB
/
helper.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
Copyright 2018 NAVER Corp.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the names of Facebook, Deepmind Technologies, NYU, NEC Laboratories America
and IDIAP Research Institute nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""
import time
import math
import numpy as np
def asHHMMSS(s):
m = math.floor(s / 60)
s -= m * 60
h = math.floor(m /60)
m -= h *60
return '%d:%d:%d'% (h, m, s)
def timeSince(since, percent):
now = time.time()
s = now - since
es = s / (percent)
rs = es - s
return '%s<%s'%(asHHMMSS(s), asHHMMSS(rs))
#######################################################################
def sent2indexes(sentence, vocab):
def convert_sent(sent, vocab):
return np.array([vocab[word] for word in sent.split(' ')])
if type(sentence) is list:
indexes=[convert_sent(sent, vocab) for sent in sentence]
sent_lens = [len(idxes) for idxes in indexes]
max_len = max(sent_lens)
inds = np.zeros((len(sentence), max_len), dtype=np.int)
for i, idxes in enumerate(indexes):
inds[i,:len(idxes)]=indexes[i]
return inds
else:
return convert_sent(sentence, vocab)
def indexes2sent(indexes, vocab, eos_tok, ignore_tok=0):
'''indexes: numpy array'''
def revert_sent(indexes, ivocab, eos_tok, ignore_tok=0):
toks=[]
length=0
indexes=filter(lambda i: i!=ignore_tok, indexes)
for idx in indexes:
toks.append(ivocab[idx])
length+=1
if idx == eos_tok:
break
return ' '.join(toks), length
ivocab = {v: k for k, v in vocab.items()}
if indexes.ndim==1:# one sentence
return revert_sent(indexes, ivocab, eos_tok, ignore_tok)
else:# dim>1
sentences=[] # a batch of sentences
lens=[]
for inds in indexes:
sentence, length = revert_sent(inds, ivocab, eos_tok, ignore_tok)
sentences.append(sentence)
lens.append(length)
return sentences, lens
import torch
from torch.nn import functional as F
use_cuda = torch.cuda.is_available()
def gData(data):
tensor=data
if isinstance(data, np.ndarray):
tensor = torch.from_numpy(data)
if use_cuda:
tensor=tensor.cuda()
return tensor
def gVar(data):
return gData(data)