initial commit
This commit is contained in:
commit
efcd3e1892
106 changed files with 27000 additions and 0 deletions
61
src/pfaedle/trgraph/Normalizer.cpp
Normal file
61
src/pfaedle/trgraph/Normalizer.cpp
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
// Copyright 2018, University of Freiburg,
|
||||
// Chair of Algorithms and Data Structures.
|
||||
// Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "pfaedle/trgraph/Normalizer.h"
|
||||
|
||||
using pfaedle::trgraph::Normalizer;
|
||||
|
||||
// _____________________________________________________________________________
|
||||
Normalizer::Normalizer(const ReplRules& rules) : _rulesOrig(rules) {
|
||||
buildRules(rules);
|
||||
}
|
||||
|
||||
// _____________________________________________________________________________
|
||||
std::string Normalizer::operator()(std::string sn) const {
|
||||
auto i = _cache.find(sn);
|
||||
if (i != _cache.end()) return i->second;
|
||||
|
||||
std::string ret = sn;
|
||||
for (auto rule : _rules) {
|
||||
std::string tmp;
|
||||
std::regex_replace(std::back_inserter(tmp), ret.begin(), ret.end(),
|
||||
rule.first, rule.second,
|
||||
std::regex_constants::format_sed);
|
||||
std::swap(ret, tmp);
|
||||
}
|
||||
|
||||
std::transform(ret.begin(), ret.end(), ret.begin(), ::tolower);
|
||||
|
||||
_cache[sn] = ret;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// _____________________________________________________________________________
|
||||
bool Normalizer::operator==(const Normalizer& b) const {
|
||||
return _rulesOrig == b._rulesOrig;
|
||||
}
|
||||
|
||||
// _____________________________________________________________________________
|
||||
void Normalizer::buildRules(const ReplRules& rules) {
|
||||
for (auto rule : rules) {
|
||||
try {
|
||||
_rules.push_back(ReplRuleComp(
|
||||
std::regex(rule.first, std::regex::ECMAScript | std::regex::icase |
|
||||
std::regex::optimize),
|
||||
rule.second));
|
||||
} catch (const std::regex_error& e) {
|
||||
std::stringstream ss;
|
||||
ss << "'" << rule.first << "'"
|
||||
<< ": " << e.what();
|
||||
throw std::runtime_error(ss.str());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue