-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_word.py
187 lines (177 loc) · 7.23 KB
/
user_word.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -*-coding:UTF-8-*-
__author__ = 'M'
import time
import util
import lang
import classifiers
def get_user_map(f_train_lines):
user_f = {}
user_c = {}
user_l = {}
user_count = {}
start = time.time()
train_total_num = len(f_train_lines)
start_inside = time.time()
for index in range(len(f_train_lines)):
single_line = f_train_lines[index].decode("utf-8").split('\t')
userid = single_line[0]
f_num = int(single_line[3])
c_num = int(single_line[4])
l_num = int(single_line[5])
user_count_current = 1
if user_count.has_key(userid):
user_count_current = user_count[userid]
user_count[userid] += 1
else:
user_count[userid] = 1
if user_f.has_key(userid):
user_f[userid] = (user_f[userid] + f_num) / user_count_current
else:
user_f[userid] = f_num / user_count_current
if user_c.has_key(userid):
user_c[userid] = (user_c[userid] + c_num) / user_count_current
else:
user_c[userid] = c_num / user_count_current
if user_l.has_key(userid):
user_l[userid] = (user_l[userid] + l_num) / user_count_current
else:
user_l[userid] = l_num / user_count_current
if index % 100000 == 0:
end_inside = time.time()
print 'NO. ' + str(index) + ' completed with ' + str(
(end_inside - start_inside)) + 's, and still left ' + str(
train_total_num - index) + ' with ' + str(
(end_inside - start_inside) * (train_total_num - index) / 100000) + 's'
start_inside = time.time()
end = time.time()
print 'get user map fininshed with: ' + str(end - start)
return user_f, user_c, user_l
def user_only_add(f_train_lines, f_test_lines):
user_f = {}
user_c = {}
user_l = {}
user_count = {}
start = time.time()
train_total_num = len(f_train_lines)
start_inside = time.time()
for index in range(len(f_train_lines)):
single_line = f_train_lines[index].decode("utf-8").split('\t')
userid = single_line[0]
f_num = int(single_line[3])
c_num = int(single_line[4])
l_num = int(single_line[5])
user_count_current = 1
if user_count.has_key(userid):
user_count_current = user_count[userid]
user_count[userid] += 1
else:
user_count[userid] = 1
if user_f.has_key(userid):
user_f[userid] = (user_f[userid] + f_num) / user_count_current
else:
user_f[userid] = f_num / user_count_current
if user_c.has_key(userid):
user_c[userid] = (user_c[userid] + c_num) / user_count_current
else:
user_c[userid] = c_num / user_count_current
if user_l.has_key(userid):
user_l[userid] = (user_l[userid] + l_num) / user_count_current
else:
user_l[userid] = l_num / user_count_current
if index % 100000 == 0:
end_inside = time.time()
print 'NO. ' + str(index) + ' completed with ' + str(
(end_inside - start_inside)) + 's, and still left ' + str(
train_total_num - index) + ' with ' + str(
(end_inside - start_inside) * (train_total_num - index) / 100000) + 's'
start_inside = time.time()
end = time.time()
print 'get user map fininshed with: ' + str(end - start)
test_userid_value = []
start = time.time()
for line in f_test_lines:
single_line = line.decode("utf-8").split('\t')
userid = single_line[0]
weiboid = single_line[1]
out_str = userid + '\t' + weiboid + '\t'
out_f = 0
out_c = 0
out_l = 0
if user_f.has_key(userid):
out_f = user_f[userid]
if user_c.has_key(userid):
out_c = user_c[userid]
if user_l.has_key(userid):
out_l = user_l[userid]
out_str = out_str + str(out_f) + ',' + str(out_c) + ',' + str(out_l) + '\n'
test_userid_value.append(out_str)
end = time.time()
print 'compute result fininshed with: ' + str(end - start)
return test_userid_value
def user_word(f_train_lines, f_test_lines):
train_total_num = len(f_train_lines)
test_total_num = len(f_test_lines)
start_inside = time.time()
train_text_index = 6
test_text_index = 3
forward_class_predicted, comment_class_predicted, like_class_predicted = classifiers.mnbclf_compute(f_train_lines,
f_test_lines,
train_text_index,
test_text_index)
test_userid_value = []
start = time.time()
user_f, user_c, user_l = get_user_map(f_train_lines)
for line_index in range(len(f_test_lines)):
single_line = f_test_lines[line_index].decode("utf-8").split('\t')
userid = single_line[0]
weiboid = single_line[1]
out_str = userid + '\t' + weiboid + '\t'
out_f_predict = int(forward_class_predicted[line_index])
out_c_predict = int(comment_class_predicted[line_index])
out_l_predict = int(like_class_predicted[line_index])
userid_f_num = 0
userid_c_num = 0
userid_l_num = 0
if user_f.has_key(userid):
userid_f_num = user_f[userid]
if user_c.has_key(userid):
userid_c_num = user_c[userid]
if user_l.has_key(userid):
userid_l_num = user_l[userid]
out_f = lang.return_target_map(out_f_predict, userid_f_num)
out_c = lang.return_target_map(out_c_predict, userid_c_num)
out_l = lang.return_target_map(out_l_predict, userid_l_num)
out_str = out_str + str(out_f) + ',' + str(out_c) + ',' + str(out_l) + '\n'
test_userid_value.append(out_str)
end = time.time()
print 'compute result fininshed with: ' + str(end - start)
return test_userid_value
# corpus, train_forward, train_comment, train_like = pre.file_to_arr(f_train_lines, 6, "train")
#
# word_forward_map = {}
# word_comment_map = {}
# word_like_map = {}
# for i in range(len(corpus)):
# text = corpus[i]
# forward = train_forward[i]
# comment = train_comment[i]
# like = train_like[i]
# for word in text and word != 'no_features':
# if word in word_forward_map.keys():
# old_forward_num = word_forward_map[word]
# new_forward_num = old_forward_num + forward
# word_forward_map[word] = new_forward_num
# else:
# word_forward_map[word] = forward
# if word in word_comment_map.keys():
# old_comment_num = word_comment_map[word]
# new_comment_num = old_comment_num + comment
# word_comment_map[word] = new_comment_num
# else:
# word_comment_map[word] = comment
# if word in word_like_map.keys():
# old_like_num = word_like_map[word]
# new_like_num = old_like_num + like
# word_like_map[word] = new_like_num
# else:
# word_like_map[word] = like