-
Notifications
You must be signed in to change notification settings - Fork 0
/
userprofile.cpp
336 lines (283 loc) · 11.1 KB
/
userprofile.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "userprofile.h"
#include <iostream>
#include <vector>
#include <boost/regex.hpp>
#include <algorithm>
#include <sstream>
#include "dateutils.h"
#include <boost/algorithm/string.hpp>
namespace casimiro {
UserProfile::UserProfile(PqConnectionPtr _con, long _userId, ConceptMapPtr _profile, ptime _start, ptime _end, ProfileType _profileType, std::string _sqlQuery):
m_con(_con),
m_userId(_userId),
m_profile(_profile),
m_start(_start),
m_end(_end),
m_profileType(_profileType),
m_sqlQuery(_sqlQuery)
{
}
UserProfile::~UserProfile()
{
}
void UserProfile::buildConceptMap(pqxx::result &_rows, std::string _pattern)
{
boost::regex rx(_pattern);
boost::match_results<std::string::const_iterator> match;
std::string::const_iterator start;
float sum = 0;
for(auto row : _rows)
{
std::string content = row["content"].c_str();
start = content.cbegin();
while(boost::regex_search(start, content.cend(), match, rx, boost::match_default))
{
std::string found(match[0].first, match[0].second);
std::transform(found.begin(), found.end(), found.begin(), ::tolower);
if(m_profile->find(found) == m_profile->end())
m_profile->insert(std::make_pair(found, 0));
if(row["user_id"].as<long>() == m_userId)
{
(*m_profile)[found] += 2;
sum += 2;
}
else
{
(*m_profile)[found] += 1;
sum++;
}
start = match[0].second;
}
}
if(m_profile->begin() == m_profile->end())
throw EmptyProfileException();
for(auto it = m_profile->begin(); it != m_profile->end(); it++)
it->second = it->second / sum;
}
void UserProfile::buildTopicsConceptMap(pqxx::result _rows)
{
float sum = 0;
float val = 0;
std::vector<std::string> pairs;
size_t colonPos;
std::string topic;
for(auto row : _rows)
{
pairs.clear();
std::string line = row["topics"].c_str();
boost::split(pairs, line, boost::is_any_of(" "));
for (auto pair : pairs)
{
colonPos = pair.find(":");
topic = pair.substr(0, colonPos).c_str();
val = atof(pair.substr(colonPos+1, pair.size() - colonPos).c_str());
if(row["user_id"].as<long>() == m_userId)
{
(*m_profile)[topic] += 2*val;
sum += 2*val;
}
else
{
(*m_profile)[topic] += val;
sum += val;
}
}
}
for(auto it = m_profile->begin(); it != m_profile->end(); it++)
it->second = it->second / sum;
}
void UserProfile::loadProfile()
{
pqxx::nontransaction t(*m_con);
pqxx::result rows = t.exec(m_sqlQuery);
switch(m_profileType)
{
case BAG_OF_WORDS:
buildConceptMap(rows, "\\w{3,}");
break;
case HASHTAG:
buildConceptMap(rows, "#\\w+");
break;
case TOPICS:
buildTopicsConceptMap(rows);
}
t.commit();
}
UserProfilePtr UserProfile::getBagOfWordsProfile(PqConnectionPtr _con, long _userId, ptime _start, ptime _end, bool _social)
{
std::stringstream ss;
ss << _userId;
std::string sUserId = ss.str();
std::string query = "SELECT content, user_id FROM tweet WHERE user_id = "+sUserId+" "
"AND creation_time >= '"+to_iso_extended_string(_start)+"' AND creation_time <= '"+to_iso_extended_string(_end)+"'";
if(_social)
{
query = "SELECT content, user_id FROM tweet WHERE user_id in ((SELECT followed_id FROM relationship WHERE follower_id="+sUserId+") UNION (SELECT GREATEST(0,"+sUserId+"))) "
"AND creation_time >= '"+to_iso_extended_string(_start)+"' AND creation_time <= '"+to_iso_extended_string(_end)+"'";
}
UserProfilePtr up = std::make_shared<UserProfile>(_con, _userId, std::make_shared<ConceptMap>(), _start, _end, BAG_OF_WORDS, query);
return up;
}
UserProfilePtr UserProfile::getHashtagProfile(PqConnectionPtr _con, long _userId, ptime _start, ptime _end, bool _social)
{
std::stringstream ss;
ss << _userId;
std::string sUserId = ss.str();
std::string query = "SELECT content, user_id FROM tweet WHERE user_id = "+sUserId+" "
"AND creation_time >= '"+to_iso_extended_string(_start)+"' AND creation_time <= '"+to_iso_extended_string(_end)+"' AND content LIKE '%#%'";
if(_social)
{
query = "SELECT content, user_id FROM tweet WHERE user_id in ((SELECT followed_id FROM relationship WHERE follower_id="+sUserId+") UNION (SELECT GREATEST(0,"+sUserId+"))) "
"AND creation_time >= '"+to_iso_extended_string(_start)+"' AND creation_time <= '"+to_iso_extended_string(_end)+"' AND content LIKE '%#%'";
}
UserProfilePtr up = std::make_shared<UserProfile>(_con, _userId, std::make_shared<ConceptMap>(), _start, _end, HASHTAG, query);
return up;
}
UserProfilePtr UserProfile::getTopicsProfile(PqConnectionPtr _con, long _userId, ptime _start, ptime _end, bool _social)
{
std::stringstream ss;
ss << _userId;
std::string sUserId = ss.str();
std::string query = "SELECT topics, user_id FROM tweet_topics WHERE user_id = "+sUserId+" "
"AND topics != '' AND creation_time >= '"+to_iso_extended_string(_start)+"' AND creation_time <= '"+to_iso_extended_string(_end)+"'";
if(_social)
{
query = "SELECT topics, user_id FROM tweet_topics WHERE user_id in ((SELECT followed_id FROM relationship WHERE follower_id="+sUserId+") UNION (SELECT GREATEST(0,"+sUserId+"))) "
"AND topics != '' AND creation_time >= '"+to_iso_extended_string(_start)+"' AND creation_time <= '"+to_iso_extended_string(_end)+"'";
}
UserProfilePtr up = std::make_shared<UserProfile>(_con, _userId, std::make_shared<ConceptMap>(), _start, _end, TOPICS, query);
return up;
}
double UserProfile::cosineSimilarity(ConceptMapPtr _profile)
{
float aNorm,bNorm,dot;
aNorm = bNorm = dot = 0;
auto uIt = m_profile->begin();
auto nIt = _profile->begin();
auto uEnd = m_profile->end();
auto nEnd = _profile->end();
int compare;
while (uIt != uEnd && nIt != nEnd)
{
compare = uIt->first.compare(nIt->first);
if(compare == 0)
{
dot += uIt->second * nIt->second;
uIt++;
nIt++;
}
else if(compare < 0)
uIt++;
else if(compare > 0)
nIt++;
}
for (uIt = m_profile->begin(); uIt != uEnd; uIt++)
aNorm += pow(uIt->second, 2);
aNorm = sqrt(aNorm);
for (nIt = _profile->begin(); nIt != nEnd; nIt++)
bNorm += pow(nIt->second, 2);
bNorm = sqrt(bNorm);
return dot / (aNorm * bNorm);
}
struct {
bool operator()(TweetProfilePtr a, TweetProfilePtr b)
{
return a->getTweetId() < b->getTweetId();
}
} TweetProfileLess;
TweetProfileVectorPtr UserProfile::getCandidateTweets(const ptime &_start, const ptime &_end)
{
pqxx::nontransaction t(*m_con);
TweetProfileVector tweetProfiles;
std::stringstream ss;
ss << m_userId;
std::string sUserId = ss.str();
std::string start = to_iso_extended_string(_start);
std::string end = to_iso_extended_string(_end);
switch(m_profileType)
{
case HASHTAG:
{
pqxx::result rows = t.exec("SELECT id, content, creation_time FROM tweet,relationship "
"WHERE user_id = followed_id AND follower_id = "+sUserId+" "
"AND creation_time >= '"+start+"' AND creation_time <= '"+end+"' AND content LIKE '%#%'");
for(auto row : rows)
{
long tweetId = row["id"].as<long>();
std::string content = row["content"].c_str();
ptime creationTime = time_from_string(row["creation_time"].c_str());
tweetProfiles.push_back(TweetProfile::getHashtagProfile(tweetId, creationTime, content));
}
break;
}
case BAG_OF_WORDS:
{
pqxx::result rows = t.exec("SELECT id, content, creation_time FROM tweet "
"WHERE user_id IN (select followed_id FROM relationship WHERE follower_id = "+sUserId+") "
"AND creation_time >= '"+start+"' AND creation_time <= '"+end+"'");
for(auto row : rows)
{
long tweetId = row["id"].as<long>();
std::string content = row["content"].c_str();
ptime creationTime = time_from_string(row["creation_time"].c_str());
tweetProfiles.push_back(TweetProfile::getBagOfWordsProfile(tweetId, creationTime, content));
}
break;
}
case TOPICS:
{
pqxx::result rows = t.exec("SELECT id, topics, creation_time FROM tweet_topics "
"WHERE topics != '' AND user_id IN (select followed_id FROM relationship WHERE follower_id = "+sUserId+") "
"AND creation_time >= '"+start+"' AND creation_time <= '"+end+"'");
for(auto row : rows)
{
long tweetId = row["id"].as<long>();
std::string topics = row["topics"].c_str();
ptime creationTime = time_from_string(row["creation_time"].c_str());
tweetProfiles.push_back(TweetProfile::getTopicProfile(tweetId, creationTime, topics));
}
break;
}
}
t.commit();
std::sort(tweetProfiles.begin(), tweetProfiles.end(), TweetProfileLess);
return std::make_shared<TweetProfileVector>(tweetProfiles);
}
RetweetVectorPtr UserProfile::getRetweets(const ptime &_start, const ptime &_end)
{
RetweetVector retweets;
pqxx::nontransaction t(*m_con);
switch(m_profileType)
{
case HASHTAG:
m_con->prepare("gr",
"SELECT creation_time, retweeted FROM tweet WHERE user_id = $1 "
"AND retweeted IS NOT NULL AND creation_time >= $2 AND creation_time <= $3 AND content LIKE '%#%' "
"ORDER BY creation_time ASC"
);
break;
case BAG_OF_WORDS:
m_con->prepare("gr",
"SELECT creation_time, retweeted FROM tweet WHERE user_id = $1 "
"AND retweeted IS NOT NULL AND creation_time >= $2 AND creation_time <= $3 "
"ORDER BY creation_time ASC"
);
break;
case TOPICS:
m_con->prepare("gr",
"SELECT creation_time, retweeted FROM tweet_topics WHERE user_id = $1 "
"AND retweeted IS NOT NULL AND retweeted != 0 AND creation_time >= $2 AND creation_time <= $3 "
"ORDER BY creation_time ASC"
);
break;
}
pqxx::result rows = t.prepared("gr")(m_userId)(to_iso_extended_string(_start))(to_iso_extended_string(_end)).exec();
for(auto row : rows)
{
ptime creationTime = time_from_string(row["creation_time"].c_str());
retweets.push_back(std::make_pair(creationTime, row["retweeted"].as<long>()));
}
t.commit();
return std::make_shared<RetweetVector>(retweets);
}
}