-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitteruser.cpp
252 lines (212 loc) · 7.32 KB
/
twitteruser.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
#include "twitteruser.h"
#include <boost/algorithm/string.hpp>
#include <QSql>
#include <QSqlQuery>
#include <QVariant>
#include "profilenotloadederror.h"
namespace casimiro {
using std::map;
typedef struct svm_node SVMNode;
TwitterUser::TwitterUser(long int _userId):
m_userId(_userId)
{
}
TwitterUser::~TwitterUser()
{
if(m_svmModel != nullptr)
svm_free_and_destroy_model(&m_svmModel);
}
long int TwitterUser::getUserId() const
{
return m_userId;
}
void TwitterUser::loadProfile(const QDateTime& _start, const QDateTime& _end)
{
if(!m_profile.empty())
return;
QSqlQuery query;
query.prepare("SELECT topics FROM tweet_topics WHERE user_id=:uid AND creation_time >= :s AND creation_time <= :e");
query.bindValue(":uid", static_cast<qlonglong>(m_userId));
query.bindValue(":s", _start.toString("yyyy-MM-dd HH:mm:ss"));
query.bindValue(":e", _end.toString("yyyy-MM-dd HH:mm:ss"));
query.exec();
float val = 0;
vector<std::string> pairs;
size_t colonPos;
std::string topic;
while(query.next())
{
pairs.clear();
std::string content = query.value(0).toString().toStdString();
boost::split(pairs, content, 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());
m_profile[topic] += val;
}
}
}
void TwitterUser::loadSVMProfile(const string &_modelPath)
{
m_svmModel = svm_load_model(_modelPath.c_str());
}
void TwitterUser::loadBOWProfile(const QDateTime& _start, const QDateTime& _end)
{
QSqlQuery query;
query.prepare("SELECT content FROM tweet_topics WHERE user_id=:uid AND creation_time >= :s AND creation_time <= :e");
query.bindValue(":uid", static_cast<qlonglong>(m_userId));
query.bindValue(":s", _start.toString("yyyy-MM-dd HH:mm:ss"));
query.bindValue(":e", _end.toString("yyyy-MM-dd HH:mm:ss"));
query.exec();
int totalTokens = 0;
while(query.next())
{
vector<std::string> tokens;
auto content = query.value(0).toString().toStdString();
boost::split(tokens, content, boost::is_any_of(" "));
for (auto token : tokens)
{
m_profile[token] += 1;
totalTokens++;
}
}
for(auto it = m_profile.begin(); it != m_profile.end(); it++)
it->second /= totalTokens;
}
const StringFloatMap& TwitterUser::getProfile() const
{
return m_profile;
}
TweetVector TwitterUser::getCandidates(const QDateTime& _start, const QDateTime& _end, ProfileType _profileType) const
{
TweetVector candidates;
QSqlQuery query;
query.prepare(
"SELECT id, topics, creation_time, content FROM tweet_topics WHERE user_id in (SELECT followed_id FROM relationship WHERE follower_id = :uid) "
"AND creation_time BETWEEN :s AND :e"
);
query.bindValue(":uid", static_cast<qlonglong>(m_userId));
query.bindValue(":s", _start.toString("yyyy-MM-dd HH:mm:ss"));
query.bindValue(":e", _end.toString("yyyy-MM-dd HH:mm:ss"));
query.exec();
while(query.next())
{
switch(_profileType)
{
case SVMProfile:
case TopicProfile:
candidates.push_back(Tweet(query.value(0).toLongLong(), query.value(2).toDateTime(), BuildProfileFromString(query.value(1).toString())));
break;
case BOWProfile:
candidates.push_back(Tweet(query.value(0).toLongLong(), query.value(2).toDateTime(), BuildBOWProfileFromString(query.value(3).toString())));
break;
}
}
return candidates;
}
TweetVector TwitterUser::getRetweets(const QDateTime& _start, const QDateTime& _end) const
{
TweetVector retweets;
QSqlQuery query;
query.prepare("SELECT id, topics, creation_time, retweeted FROM tweet_topics WHERE retweeted IS NOT NULL AND user_id = :uid AND creation_time BETWEEN :s AND :e");
query.bindValue(":uid", static_cast<qlonglong>(m_userId));
query.bindValue(":s", _start.toString("yyyy-MM-dd HH:mm:ss"));
query.bindValue(":e", _end.toString("yyyy-MM-dd HH:mm:ss"));
query.exec();
while(query.next())
{
StringFloatMap profile = BuildProfileFromString(query.value(1).toString());
retweets.push_back(Tweet(query.value(0).toLongLong(), query.value(2).toDateTime(), profile, query.value(3).toLongLong()));
}
return retweets;
}
float TwitterUser::cosineSimilarity(const StringFloatMap& _profile) const
{
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);
}
vector<SVMNode> NodeFromCandidate(const Tweet &_candidate)
{
vector<SVMNode> nodes;
for(auto pair : _candidate.getProfile())
{
SVMNode node;
node.index = atoi(pair.first.c_str()) + 1;
node.value = pair.second;
nodes.push_back(node);
}
SVMNode node;
node.index = -1;
nodes.push_back(node);
return nodes;
}
TweetVector TwitterUser::sortCandidates(const TweetVector& _candidates, const QDateTime& _recommendationTime, const StringIntMap& _topicLifeSpan) const
{
if(m_profile.empty() && m_svmModel == nullptr)
throw ProfileNotLoadedError();
TweetVector sorted;
map<float, vector<int>> aux;
int i = 0;
for(auto candidate : _candidates)
{
if(!CandidateHasOldTopics(candidate, _topicLifeSpan, _recommendationTime))
{
double sim;
if(m_svmModel != nullptr)
{
auto candidateNodes = NodeFromCandidate(candidate);
sim = svm_predict(m_svmModel, candidateNodes.data());
}
else
sim = cosineSimilarity(candidate.getProfile());
if(aux.find(sim) == aux.end())
aux[sim] = vector<int>();
aux.find(sim)->second.push_back(i);
}
i++;
}
for(auto it = aux.rbegin(); it != aux.crend(); it++)
for(auto index : it->second)
sorted.push_back(_candidates.at(index));
return sorted;
}
bool TwitterUser::CandidateHasOldTopics(const Tweet& _candidate, const StringIntMap& _topicLifeSpan, const QDateTime& _retweetCreationTime)
{
int lifeInSeconds = (_retweetCreationTime.toMSecsSinceEpoch() - _candidate.getCreationTime().toMSecsSinceEpoch()) / 1000;
for(auto pair : _candidate.getProfile())
{
auto it = _topicLifeSpan.find(pair.first);
if(it != _topicLifeSpan.end() && lifeInSeconds > it->second)
return true;
}
return false;
}
}