#include "word.h" #include "FRC.h" #include Word& Word::append(const std::map& other, bool alsoVal) { Grammar::channelID c = 0, m_c = 0, cV = source->chI(u"val"); if (!channels.empty()) { m_c = (*channels.rbegin()).first; } if (!other.empty()) { m_c = std::max(m_c, (*other.rbegin()).first); } for (; c <= m_c; ++c) { if (c != cV || alsoVal) { if (other.count(c)) { const auto& ov = other.at(c); auto p = channels.insert(std::make_pair(c, ov)); if (!p.second) { (*p.first).second += ov; } } } } return *this; } Word& Word::appendVal(const icu::UnicodeString& v) { channels[source->chI(u"val")] += v; return *this; } Word& Word::append(const Word& other) { if (source != other.source) { throw std::runtime_error("Cannot append words from different grammars"); } Grammar::channelID c = 0, m_c = 0; if (!channels.empty()) { m_c = (*channels.rbegin()).first; } if (!other.channels.empty()) { m_c = std::max(m_c, (*other.channels.rbegin()).first); } for (; c <= m_c; ++c) { if (other.channels.count(c)) { const auto& ov = other.channels.at(c); auto p = channels.insert(std::make_pair(c, ov)); if (!p.second) { (*p.first).second += ov; } } } path.append(other.path); freq *= other.freq; return *this; } icu::UnicodeString Word::format(const std::string& fmt) { Grammar::ParsedString fstr{fromUTF8(fmt)}; icu::UnicodeString ret; for (const auto& c : channels) { // std::clog<chN(c.first))<<" = "<chI(s.second.getName()); if (s.second) { if (channels.count(c)) { ret += channels.at(c); } else { throw std::runtime_error("Channel "+toUTF8(s.second.getName())+" not found in word"); } } } return ret; } icu::UnicodeString Word::printAll(const icu::UnicodeString& sep) { if (channels.empty()) { return u""; } return std::accumulate( std::next(channels.begin()), channels.end(), channels.begin()->second, [&sep](const icu::UnicodeString& a, const auto& b) -> icu::UnicodeString { return a+sep+b.second; } ); } Word TransformatedWord::getWord(const icu::UnicodeString& sep) { Word out{*source, {}}; for (auto& c : history) { out[c.first] = join(c.second, sep); } return out; } void TransformatedWord::fix() { return; }