-
Notifications
You must be signed in to change notification settings - Fork 21
/
utils.py
66 lines (53 loc) · 1.92 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
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
import os
import torch
from torch.autograd import Variable
import numpy as np
import nltk
import time
movie_list = []
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def load_model(model, resume, verbose=True):
if not os.path.isfile(resume):
raise ValueError("no checkpoint found at '{}'".format(resume))
if model.cuda_available:
checkpoint = torch.load(resume)
else:
checkpoint = torch.load(resume, map_location=lambda storage, loc: storage)
model.load_state_dict(checkpoint['state_dict'])
if verbose:
print("=> loaded checkpoint '{}' (epoch {} with best loss {})"
.format(resume, checkpoint['epoch'], checkpoint['best_loss']))
def sort_for_packed_sequence(lengths, cuda=False):
"""
:param lengths: 1D array of lengths
:return: sorted_lengths (lengths in descending order,
sorted_idx (indices to sort),
rev (indices to retrieve original order)
"""
sorted_idx = np.argsort(lengths)[::-1] # idx to sort by length
sorted_lengths = lengths[sorted_idx]
rev = np.argsort(sorted_idx) # idx to retrieve original order
tt = torch.cuda.LongTensor if cuda else torch.LongTensor
sorted_idx = Variable(tt(sorted_idx.copy()))
rev = Variable(tt(rev.copy()))
return sorted_lengths, sorted_idx, rev
def tokenize(message):
"""
Text processing: Sentence tokenize, then concatenate the word_tokenize of each sentence. Then lower.
:param message:
:return:
"""
sentences = nltk.sent_tokenize(message)
tokenized = []
for sentence in sentences:
tokenized += nltk.word_tokenize(sentence)
return [word.lower() for word in tokenized]
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
print("Executed {} in {} s".format(func.__name__, time.time() - start_time))
return result
return wrapper