-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
64 lines (52 loc) · 1.52 KB
/
utils.h
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
#ifndef UTILS_H
#define UTILS_H
#include <map>
#include <QString>
#include <QStringList>
#include <vector>
#include <fstream>
#include <sstream>
namespace casimiro {
typedef std::map<std::string, float> StringFloatMap;
typedef std::map<std::string, int> StringIntMap;
typedef std::vector<StringIntMap> StringIntMaps;
inline StringFloatMap BuildProfileFromString(const QString &_content)
{
StringFloatMap profile;
auto pairs = _content.split(" ");
for(auto pair : pairs)
{
auto values = pair.split(":");
profile[values.at(0).toStdString()] = atof(values.at(1).toStdString().c_str());
}
return profile;
}
inline StringFloatMap BuildBOWProfileFromString(const QString &_content)
{
int totalTokens = 0;
StringFloatMap profile;
auto tokens = _content.split(" ");
for(auto token : tokens)
{
profile[token.toStdString()] += 1;
totalTokens++;
}
for(auto it = profile.begin(); it != profile.end(); it++)
it->second /= totalTokens;
return profile;
}
inline StringIntMap BuildTopicLifeSpanMapFromFile(const std::string& _fileName)
{
StringIntMap topics;
std::ifstream file(_fileName);
std::string line;
while(std::getline(file, line))
{
std::istringstream iss(line);
std::vector<std::string> tokens{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
topics.insert(std::make_pair(tokens.at(0), atoi(tokens.at(1).c_str())));
}
return topics;
}
}
#endif // UTILS_H