-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
100 lines (77 loc) · 2.79 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
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
import re
import time
import numpy as np
import torch
def get_stopwords():
fp = open('./stopwords-en.txt', 'r')
stopwords = []
for line in fp.readlines():
line = line.strip()
line = line.replace('\n', '')
stopwords.append(line)
fp.close()
return stopwords
def clean_str(string):
string = re.sub(r"[^A-Za-z0-9]", " ", string)
string = re.sub(r"\'s", " \'s", string)
string = re.sub(r"\'ve", " \'ve", string)
string = re.sub(r"n\'t", " n\'t", string)
string = re.sub(r"\'re", " \'re", string)
string = re.sub(r"\'d", " \'d", string)
string = re.sub(r"\'ll", " \'ll", string)
string = re.sub(r",", " , ", string)
string = re.sub(r"!", " ! ", string)
string = re.sub(r"\(", " \( ", string)
string = re.sub(r"\)", " \) ", string)
string = re.sub(r"\?", " \? ", string)
string = re.sub(r"\s{2,}", " ", string)
string = re.sub(r"\s{2,}", " ", string)
string = re.sub(r"sssss ", " ", string)
return string.strip().lower()
def review2ids(opt, review):
review = clean_str(review)
review_ids = []
for w in review.strip().split():
if w in opt.vocab:
review_ids.append(opt.word2index[w])
if len(review_ids) > opt.r_max_len:
review_ids = review_ids[:opt.r_max_len]
else:
review_ids += [0] * (opt.r_max_len - len(review_ids))
return review_ids
def now():
return str(time.strftime('%Y-%m-%d %H:%M:%S'))
def collate_fn(batch):
data, label = zip(*batch)
return data, label
def unpack_input(opt, x):
uids_batch , iids_batch = x
uids_batch = list(uids_batch)
iids_batch = list(iids_batch)
user_reviews_batch = []
user_item2id_batch = []
user_doc_batch = []
item_reviews_batch = []
item_user2id_batch = []
item_doc_batch = []
for i in range(len(uids_batch)):
uids = uids_batch[0]
iids = iids_batch[0]
# num_items * #reviews, review_max_length
user_reviews = opt.users_review_list[uids]
# num_items * #reviews
user_item2id = opt.user2itemid_list[uids]
# num_items * doc_max_length
user_doc = opt.user_doc[uids]
item_reviews = opt.items_review_list[iids]
item_user2id = opt.item2userid_list[iids]
item_doc = opt.item_doc[iids]
user_reviews_batch.append(user_reviews)
user_item2id_batch.append(user_item2id)
user_doc_batch.append(user_doc)
item_reviews_batch.append(item_reviews)
item_user2id_batch.append(item_user2id)
item_doc_batch.append(item_doc)
data = [user_reviews_batch, item_reviews_batch, uids_batch, iids_batch, user_item2id_batch, item_user2id_batch, user_doc_batch, item_doc_batch]
data = list(map(lambda x: torch.LongTensor(x).cuda(), data))
return data