-
Notifications
You must be signed in to change notification settings - Fork 1
/
lda.cpp
executable file
·234 lines (216 loc) · 9.03 KB
/
lda.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "model.h"
#include "transmit.h"
#include "utils.h"
//#include <boost/thread.hpp>
using namespace std;
void operation(transmit& sv, model& lda, transmit * client) {
strtokenizer cmd;
database db;
string command;
cout << "here1" << endl;
try {
*client >> command;
cout << command << endl;
cmd.parse(command, "*_*");
string operation = cmd.token(0);
//1. createuser + username + password, e.g. createuser*_*jianan*_*123456 response: Success! or Fail!
//2. login + username + password, e.g. login*_*jianan*_*123456 response: Success! or Fail!
//3. changeuser + username + old + password, e.g. login*_*jianan*_*12345678 response: Success! or Fail!
if(operation == "createuser"){
assert(cmd.count_tokens() == 3);
bool ans = db.addUser(cmd.token(1),cmd.token(2));
if (ans) {
*client << "Success";
} else {
*client << "Fail";
}
} else if(operation == "login"){
assert(cmd.count_tokens() == 3);
bool ans = db.login(cmd.token(1),cmd.token(2));
if (ans) {
*client << "Success";
} else {
*client << "Fail";
}
} else if(operation == "changeuser"){
assert(cmd.count_tokens() == 4);
bool ans = db.changePassword(cmd.token(1), cmd.token(2), cmd.token(3));
if (ans) {
*client << "Success";
} else {
*client << "Fail";
}
}
//4. display + username + interfacename + category + movie or TV show, e.g. display*_*jianan*_*main*_*action
// display + number + interface
else if(operation == "display"){
assert(cmd.count_tokens() >=3);
string interface = cmd.token(2);
if(interface == "main"){
assert(cmd.count_tokens() == 3);
/*functions for finding 12 hotest films or TV shows*/
vector<int> col = db.hotCollect(cmd.token(1), 10);
vector<int> can = lda.ranking(col,"","");
for (int i = 0; i < N_DISP; i++) {
sv.accept(*client);
vector<string> info = db.fetchPic(can[i]);
// info[0] - number ; info[1] - name ; info[2] - picname
client->SendFile(info);
client->close();
}
} else if(interface == "new"){
assert(cmd.count_tokens() == 3);
/*functions for finding 12 lastest films or TV shows*/
vector<int> late = db.fetchLatest(N_DISP);
cout << late.size() << endl;
for (int i = 0; i < N_DISP; i++) {
sv.accept(*client);
vector<string> info = db.fetchPic(late[i]);
client->SendFile(info);
client->close();
}
} else if(interface == "movies" && cmd.count_tokens() == 3){
/*functions for finding 12 random films*/
vector<int> col = db.hotTypeCollect(cmd.token(1), N_DISP, "Movies");
vector<int> can = lda.ranking(col, "movies", "");
for (int i = 0; i < N_DISP/2; i++) {
sv.accept(*client);
vector<string> info = db.fetchPic(can[i]);
client->SendFile(info);
client->close();
}
} else if(interface == "movies" && cmd.count_tokens() == 4){
/*functions for finding 12 random films with category*/
vector<int> col = db.hotTypeCollectWithType(cmd.token(1), N_DISP, "Movies", cmd.token(3));
vector<int> can = lda.ranking(col, "movies", cmd.token(3));
for (int i = 0; i < N_DISP/2; i++) {
sv.accept(*client);
vector<string> info = db.fetchPic(can[i]);
client->SendFile(info);
client->close();
}
} else if(interface == "TV" && cmd.count_tokens() == 3){
/*functions for finding 12 random TV shows*/
vector<int> col = db.hotTypeCollect(cmd.token(1), N_DISP, "TV");
vector<int> can = lda.ranking(col, "TV", "");
for (int i = 0; i < N_DISP/2; i++) {
sv.accept(*client);
vector<string> info = db.fetchPic(can[i]);
client->SendFile(info);
client->close();
}
} else if(interface == "TV" && cmd.count_tokens() == 4){
/*functions for finding 12 random TV shows with category*/
vector<int> col = db.hotTypeCollectWithType(cmd.token(1), N_DISP, "TV", cmd.token(3));
vector<int> can = lda.ranking(col, "TV", cmd.token(3));
for (int i = 0; i < N_DISP/2; i++) {
sv.accept(*client);
vector<string> info = db.fetchPic(can[i]);
client->SendFile(info);
client->close();
}
} else if(interface == "favourites"){
assert(cmd.count_tokens() == 3);
/*functions for finding 12 favourites for user*/
vector<int> can = db.FavoriteCollect(cmd.token(1), N_DISP);
int num = can.size();
cout << "favourites number=" << num << endl;
if (can.empty()) {
*client << "Emtpy favourites";
} else {
stringstream out;
out << num;
*client << out.str();
for (int i = 0; i < num; i++) {
sv.accept(*client);
vector<string> info = db.fetchPic(can[i]);
client->SendFile(info);
client->close();
}
}
} else if(interface == "MTDetails"){
//display + number + MTDetails
assert(cmd.count_tokens() == 3);
int i = atoi(cmd.token(1).c_str());
cout << "asking for " << i << endl;
string arg = db.preciseFetch(i);
cout << arg << endl;
client->SendStruct(arg);
client->close();
// send pic
sv.accept(*client);
vector<string> info = db.fetchPic(i);
client->SendFile(info);
client->close();
}
}
//5. changemovie + username + number + comments + rate(5 to 1) + favourite or not
//, e.g. changemovie*_*jianan*_*23*_*this is awesome*_*5*_*1
//if something is missing, use blank " "
else if (operation == "changemovie") {
string comments = cmd.token(3);
cout << "The comments is: " << comments << endl;
bool ans = db.addComment(cmd.token(1), atoi(cmd.token(2).c_str()), cmd.token(3), atoi(cmd.token(5).c_str()), atoi(cmd.token(4).c_str()));
if (ans) {
*client << "Success";
} else {
*client << "Fail";
}
}
//6. search + movie or TV show + movie/TV name
//default 10 pictures
else if(operation == "search"){
/*functions for finding 10 related movies*/
assert(cmd.count_tokens() == 2);
cout << cmd.token(1)<< endl;
vector<int> p = db.search(cmd.token(1), 10, 0);
stringstream out;
out << p.size();
*client << out.str();
for (int i = 0; i < (int)p.size(); i++) {
sv.accept(*client);
vector<string> info = db.fetchPic(p[i]);
client->SendFile(info);
client->close();
}
}
} catch (SocketException &) {
delete client;
return;
}
}
int main(int argc, char ** argv) {
model lda;
int port;
if (utils::parse_args(argc, argv, &lda, port)) {
return 1;
}
lda.init();
if (lda.model_status == MODEL_STATUS_EST || lda.model_status == MODEL_STATUS_ESTC) {
lda.estimate();
}
if (lda.model_status == MODEL_STATUS_INF) {
lda.inference();
}
if (lda.model_status == MODEL_STATUS_PREPROCESS) {
lda.preprocess();
}
if (lda.model_status == MODEL_STATUS_RANKING) {
lda.ranking();
}
if (lda.model_status == MODEL_STATUS_CLASSIFIER) {
lda.classification();
}
if (lda.model_status == MODEL_STATUS_SERVER) {
transmit sv(port);
while(1) {
cout << "again" << endl;
transmit *client = new transmit();
sv.accept(*client);
cout << "almost enter" << endl;
operation(sv, lda, client);
//boost::thread newThread(operation, client);
}
}
return 0;
}