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
|
#include <array>
#include <string_view>
#include <iostream>
#include <iterator>
#include <regex>
std::string drop_affixes(std::string_view w, auto pfxs_beg, auto pfxs_end, auto sfxs_beg, auto sfxs_end)
{
auto const p = std::find_if(pfxs_beg, pfxs_end, [w](auto prefix) { return w.starts_with(prefix); });
if (p != pfxs_end) w.remove_prefix(p->size());
auto const s = std::find_if(sfxs_beg, sfxs_end, [w](auto suffix) { return w.ends_with(suffix); });
if (s != sfxs_end) w.remove_suffix(s->size());
return std::string{w};
}
int main()
{
std::array<std::string_view, 3> constexpr pfxs {"com", "rela", "sci"};
std::array<std::string_view, 3> constexpr sfxs {"ship", "ter", "nce"};
auto const fixup_match = [&pfxs, sfxs](std::smatch const& m)
{ return m[1].str() + drop_affixes(m[2].str(), pfxs.begin(), pfxs.end(), sfxs.begin(), sfxs.end()); };
auto const word_re = std::regex{R"((\W*)\b(\w+)\b)"};
for (std::string line; std::getline(std::cin, line); )
{
auto const begin_matches = std::sregex_iterator{line.begin(), line.end(), word_re};
auto const end_matches = std::sregex_iterator{};
auto const out = std::ostream_iterator<std::string>{std::cout};
std::transform(begin_matches, end_matches, out, fixup_match);
}
}
|