Parse watchwords only once.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2019-12-29 00:05:13 +01:00
parent 90f9cecf7b
commit 937b08ff85
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 18 additions and 15 deletions

View File

@ -27,7 +27,6 @@
#include <algorithm>
#include <fstream>
#include <list>
#include <sstream>
#include <string>
#include <utility>
@ -38,7 +37,6 @@ using boost::regex;
using boost::regex_replace;
using std::transform;
using std::ifstream;
using std::list;
using std::istringstream;
using std::stringstream;
using std::string;
@ -127,6 +125,7 @@ void Document::download()
void Document::parse()
{
parse_watchwords();
pt::ptree tree;
istringstream iss{_raw_doc};
pt::read_xml(iss, tree);
@ -242,6 +241,19 @@ string Document::extract_location(const RestClient::HeaderFields &headers) const
}
string Document::add_hashtags(const string &text)
{
string out{text};
for (const auto &tag : _watchwords)
{
regex re_tag("([[:space:][:punct:]]|^)("
+ tag + ")([[:space:][:punct:]]|$)", regex::icase);
out = regex_replace(out, re_tag, "$1#$2$3", boost::format_first_only);
}
return out;
}
void Document::parse_watchwords()
{
Json::Value json;
const auto filepath = _cfg.get_config_dir() /= "watchwords.json";
@ -259,25 +271,14 @@ string Document::add_hashtags(const string &text)
+ (_cfg.get_config_dir() /= "watchwords.json").string()};
}
list<string> watchwords;
const auto &tags_profile = json[_cfg.profile]["tags"];
const auto &tags_global = json["global"]["tags"];
transform(tags_profile.begin(), tags_profile.end(),
std::back_inserter(watchwords),
std::back_inserter(_watchwords),
[](const Json::Value &value) { return value.asString(); });
transform(tags_global.begin(), tags_global.end(),
std::back_inserter(watchwords),
std::back_inserter(_watchwords),
[](const Json::Value &value) { return value.asString(); });
string out{text};
for (const auto &tag : watchwords)
{
regex re_tag("([[:space:][:punct:]]|^)("
+ tag + ")([[:space:][:punct:]]|$)", regex::icase);
out = regex_replace(out, re_tag, "$1#$2$3", boost::format_first_only);
}
return out;
}

View File

@ -71,6 +71,7 @@ private:
Config &_cfg;
ProfileData &_profiledata;
string _raw_doc;
list<string> _watchwords;
void parse_rss(const pt::ptree &tree);
[[nodiscard]]
@ -78,6 +79,7 @@ private:
[[nodiscard]]
string extract_location(const RestClient::HeaderFields &headers) const;
string add_hashtags(const string &text);
void parse_watchwords();
};
} // namespace mastorss