-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVenuemanager.cpp
307 lines (269 loc) · 6.46 KB
/
Venuemanager.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include"node.h"
#include"tools.h"
#define threshold 10
#define Using_Alg_Simi false
#define DEFAULT_V_MIN_SIM 0.1
#define scale 5//3.25
bool outputflag = false;
#define DEBUG false
VenueManager::VenueManager() {
VMINSIM = DEFAULT_V_MIN_SIM;
vs2id = NULL;
vsize = 0;
maxid = 0;
//missvenue = new set<string>();
//fmiss = NULL;
//if (DEBUG){
// string fpath = DATA_DIR;
// fpath.append("mapid/missVenue.tsv");
// fmiss = new ofstream(fpath);
//}
}
/*
* initiate the Venuemanager
-read map file
-read simi file
*/
void VenueManager::init(string &mapid, string &simipath) {
vs2id = new map<string, int>();
cout << "initial vm\r";
readfile(mapid);
vector<vpair> vps;
readsimi(simipath, vps);
cout << "maxid\t" << maxid << endl;
//adding self simi
for (int i = 0; i<maxid; i++) {
vpair p1;
p1.vi = i;
p1.vj = i;
p1.score = 1.0;
vps.push_back(p1);
}
// vector 2 array and sort by id
pairsimi = (vpair*)malloc(sizeof(vpair)*vps.size());
memset(pairsimi, 0, sizeof(vpair)*vps.size());
for (int i = 0; i<vps.size(); i++)
pairsimi[i] = vps[i];
//cout<<pairsimi<<endl;
sort(pairsimi, pairsimi + vps.size());
// record the begin index of pairsimi
indexs = new int[maxid];
int begin = 0;
for (int index = 0; index<maxid; index++) {
for (; begin<vps.size(); begin++)
if (pairsimi[begin].vi>index) {
indexs[index] = begin;
break;
}
}
sizes = (int*)malloc(sizeof(int)*maxid);
memset(sizes, 0, sizeof(int)*maxid);
indexs[maxid - 1] = vps.size();
//normlize for each keywod=rd
begin = 0;
for (int index = 0; index<maxid; index++) {
float sum = 0;
for (int i = begin; i<indexs[index]; i++)
sum += pairsimi[i].score;
for (int i = begin; i<indexs[index]; i++)
pairsimi[i].score /= sum;
begin = indexs[index];
}
int max = 0;
int maxindex = -1;
for (int i = 1; i<maxid; i++) {
if (indexs[i] - indexs[i - 1]>max) {
max = indexs[i] - indexs[i - 1];
maxindex = i;
}
}
cout << "max venue\t" << id2name.find(maxindex)->second << "\t" << max << endl;
cout << "venue size" << maxid << "\tvenue simi pair size\t" << vps.size() << endl;
}
void VenueManager::init(double VThres, string &mapid, string &simipath) {
VMINSIM = scale * VThres;
//cout<<VMINSIM<<endl;
init(mapid, simipath);
}
int VenueManager::getID(string name) {
if (name.size()<2)
return -1;
string nname = norm(name);
if (nname == "corr")
return -1;
map<string, int>::iterator ite = vs2id->find(nname);
if (ite == vs2id->end()) {
return unseenid;
}
sizes[ite->second]++;
return ite->second;
}
void VenueManager::fileClose() {
//if(fmiss!=NULL)
// fmiss->close();
}
bool vpair::operator< (const vpair &c) const {
//cout<<this<<endl;
//cout<<&c<<endl;
if (vi<c.vi)
return true;
else if (vi>c.vi)
return false;
if (score<c.score)
return false;
if (score>c.score)
return true;
return false;
}
VenueManager::~VenueManager() {
clear();
}
int VenueManager::size() {
return vsize;
}
void VenueManager::clear() {
if (vs2id != NULL) {
delete vs2id;
vs2id = NULL;
}
map<int, string>().swap(id2name);
if (pairsimi != NULL) {
delete pairsimi;
pairsimi = NULL;
}
cout << "\t\t\t\t\t\t VenueManager pairsimi cleared\r";
if (indexs != NULL) {
delete indexs;
indexs = NULL;
}
cout << "\t\t\t\t\t\t VenueManager indexs cleared\r";
if (sizes != NULL) {
free(sizes);
sizes = NULL;
}
cout << "\t\t\t\t\t\t VenueManager sizes cleared\r";
VMINSIM = DEFAULT_V_MIN_SIM;
vs2id = NULL;
vsize = 0;
maxid = 0;
//fmiss = NULL;
}
/*
* read the map file
*/
void VenueManager::readfile(string &fpath) {
ifstream fin(fpath);
if (!fin) {
cout << "\"G:\\GraduateThesis\\data\\mapid\\id2venue.tsv\" connot find" << endl;
exit(1);
}
string temp;
set<int> ids;
while (!fin.eof()) {
getline(fin, temp);
if (temp.length()<2)
continue;
vector<string> ss;
split(ss, temp, '\t');
string name = norm(ss[1]);
int id = atoi(ss[0].c_str());
vs2id->insert(map<string, int>::value_type(name, id));
id2name.insert(map<int, string>::value_type(id, name));
if (ss.size() <= 1) {
cout << temp << endl;
system("pause");
}
ids.insert(id);
if (id >= maxid)
maxid = id + 1;
}
for (int i = 0; i<maxid; i++) {
if (ids.find(i) == ids.end()) {
string unseen = "unseen";
unseenid = i;
vs2id->insert(map<string, int>::value_type(unseen, i));
id2name.insert(map<int, string>::value_type(i, unseen));
if (i >= maxid)
maxid = i + 1;
ids.insert(i);
break;
}
}
vsize = ids.size();
fin.close();
}
/*
* read the similarity between venues
*/
void VenueManager::readsimi(string &fpath, vector<vpair> &vvp) {
ifstream fin(fpath);
if (!fin) {
cout << fpath << "not found" << endl;
return;
}
//cout<<"VThreshold =\t"<<VMINSIM<<endl;
string temp;
while (!fin.eof()) {
getline(fin, temp);
if (temp.length()<2)
continue;
vector<string> ss;
split(ss, temp, '\t');
int vid1 = atoi(ss[0].c_str());
int vid2 = atoi(ss[1].c_str());
if (id2name.find(vid1) == id2name.end() || id2name.find(vid2) == id2name.end())
continue;
float simi = scale * atof(ss[2].c_str());
// a threshold. only consider the similarity bigger than MINSIM
if (simi<VMINSIM)
continue;
vpair p1;
p1.vi = vid1;
p1.vj = vid2;
p1.score = simi;
if (vid1<maxid&&vid2<maxid)
vvp.push_back(p1);
p1.vi = vid2;
p1.vj = vid1;
vvp.push_back(p1);
if (vid1<maxid&&vid2<maxid)
vvp.push_back(p1);
}
fin.close();
}
int VenueManager::getsize(int id) {
if (id == unseenid)
return 5;
return sizes[id];
}
void VenueManager::addsimi(map<int, float> &v2s, map<int, float> &v2s_simi) {
for (map<int, float>::iterator ite = v2s.begin(); ite != v2s.end(); ite++) {
int vid = ite->first;
int count = 0;
int i = 0;
if (vid>0)
i = indexs[vid - 1];
for (; i<indexs[vid]; i++) {
int vjid = pairsimi[i].vj;
if (vjid >-1) {
count += 1;
v2s_simi[vjid] += (ite->second*pairsimi[i].score);// / (1 + (vt->papers.size()));
count++;
}
}
}
}
string VenueManager::getname(int id) {
return id2name.find(id)->second;
}
bool VenueManager::isSim(int id1, int id2) {
if (id1 == -1 || id2 == -1)
return false;
int i = 0;
if (id1>0)
i = indexs[id1 - 1];
for (; i<indexs[id1]; i++)
if (id2 == pairsimi[i].vj)
return true;
return 0;
}