-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_vocab.py
136 lines (114 loc) · 4.15 KB
/
build_vocab.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import numpy as np
import pickle
import os
if not os.path.exists("text_emb.pkl"):
vocab = {}
w2v = []
"""
Word2Vec embedding build
"""
with open("sgns.weibo.bigram-char", "r", encoding="utf-8") as f:
_ = f.readline()
line = f.readline()
while line:
line = line[:-2].split(" ")
vocab[line[0]] = len(vocab)
w2v.append([float(line[i]) for i in range(1, len(line))])
line = f.readline()
w2v = np.array(w2v, dtype=np.float32)
"""
User Text Feature
"""
doc_total = 0
doc_count = np.zeros(len(vocab), dtype=int)
with open("./data/train/train_status.txt", "r", encoding="utf-8") as f:
line = f.readline()
while line:
text = line[:-1].split(",")[-1]
flag = np.zeros(len(vocab), dtype=int)
for i in text.split(" "):
if "\xa0" in i:
break
if i in vocab:
flag[vocab[i]] = 1
doc_count += flag
doc_total += 1
line = f.readline()
with open("./data/test/test_status.txt", "r", encoding="utf-8") as f:
line = f.readline()
while line:
text = line[:-1].split(",")[-1]
flag = np.zeros(len(vocab), dtype=int)
for i in text.split(" "):
if "\xa0" in i:
break
if i in vocab:
flag[vocab[i]] = 1
doc_count += flag
doc_total += 1
line = f.readline()
idf = np.log10(doc_total / (doc_count + 1))
count_user = {}
with open("./data/train/train_status.txt", "r", encoding="utf-8") as f:
line = f.readline()
while line:
user = line[:-1].split(",")[0]
if user not in count_user:
count_user[user] = 0
for i in text.split(" "):
if "\xa0" in i:
break
if i in vocab:
count_user[user] += 1
line = f.readline()
with open("./data/test/test_status.txt", "r", encoding="utf-8") as f:
line = f.readline()
while line:
user = line[:-1].split(",")[0]
if user not in count_user:
count_user[user] = 0
for i in text.split(" "):
if "\xa0" in i:
break
if i in vocab:
count_user[user] += 1
line = f.readline()
text_embedding = {u: np.zeros(300) for u in count_user}
with open("./data/train/train_status.txt", "r", encoding="utf-8") as f:
line = f.readline()
while line:
line = line[:-1].split(",")
user = line[0]
text = line[-1]
for i in text.split(" "):
if "\xa0" in i:
break
if i in vocab:
text_embedding[user] += w2v[vocab[i]] / count_user[user] * idf[vocab[i]]
line = f.readline()
with open("./data/test/test_status.txt", "r", encoding="utf-8") as f:
line = f.readline()
while line:
line = line[:-1].split(",")
user = line[0]
text = line[-1]
for i in text.split(" "):
if "\xa0" in i:
break
if i in vocab:
text_embedding[user] += w2v[vocab[i]] / count_user[user] * idf[vocab[i]]
line = f.readline()
mean_embedding = np.zeros(300, dtype=np.float32)
for i in text_embedding:
text_embedding[i] = text_embedding[i].astype(np.float32)
mean_embedding += text_embedding[i]
with open("text_emb.pkl", "wb") as f:
pickle.dump(text_embedding, f)
mean_embedding /= len(text_embedding)
else:
with open("text_emb.pkl", "rb") as f:
text_embedding = pickle.load(f)
mean_embedding = np.zeros(300, dtype=np.float32)
for i in text_embedding:
mean_embedding += text_embedding[i]
mean_embedding /= len(text_embedding)