#include "ctxstream.hpp" #include #include #include #include namespace ctxstream { static std::string trim(const std::string& s) { std::size_t a = 0, b = s.size(); while (a < b && std::isspace(static_cast(s[a]))) ++a; while (b > a && std::isspace(static_cast(s[b - 1]))) --b; return s.substr(a, b - a); } static bool to_number(const std::string& s, double* out) { if (s.empty()) return false; std::string clean; for (char c : s) { if (c != ',' && c != '_') clean += c; // 1,234 and 1_234 } try { std::size_t used = 0; const double v = std::stod(clean, &used); if (used != clean.size()) return false; *out = v; return true; } catch (...) { return false; } } std::vector parse_records(const std::string& raw) { std::vector out; std::istringstream in(raw); std::string line; while (std::getline(in, line)) { const std::string t = trim(line); if (t.empty() || t == "NONE") continue; // Split on the last tab, else the last run of whitespace, else a colon. // Keys legitimately contain spaces ("Spatial Relationship"), which is // exactly the case a naive \S+ split truncated in the previous harness. std::size_t cut = t.rfind('\t'); if (cut == std::string::npos) { const std::size_t colon = t.rfind(':'); const std::size_t space = t.find_last_of(" "); cut = (colon != std::string::npos && colon > space) ? colon : space; } if (cut == std::string::npos || cut == 0) continue; double v = 0.0; if (!to_number(trim(t.substr(cut + 1)), &v)) continue; const std::string key = trim(t.substr(0, cut)); if (key.empty()) continue; out.push_back({key, v}); } return out; } Tally tally(const std::vector& results) { Tally out; std::map sums; for (const SegmentResult& r : results) { if (!r.ok) continue; // a failed segment contributes nothing, silently // dropping it would understate every key const std::vector recs = parse_records(r.raw); out.parsed_records += static_cast(recs.size()); // Count lines the model produced that no parser could use, so a model // ignoring the output contract is visible rather than just quiet. int lines = 0; std::istringstream in(r.raw); std::string l; while (std::getline(in, l)) { if (!trim(l).empty() && trim(l) != "NONE") ++lines; } out.unparsed_lines += std::max(0, lines - static_cast(recs.size())); for (const Record& rec : recs) { sums[rec.key] += rec.value; out.total += rec.value; } } out.sorted.assign(sums.begin(), sums.end()); std::sort(out.sorted.begin(), out.sorted.end(), [](const auto& a, const auto& b) { if (a.second != b.second) return a.second < b.second; return a.first < b.first; // stable, reproducible ties }); return out; } static std::string fmt(double v) { std::ostringstream o; if (v == static_cast(v)) o << static_cast(v); else o << v; return o.str(); } std::string construct(const Tally& t, Answer how, const std::string& label) { if (t.sorted.empty()) return label + ": NONE"; switch (how) { case Answer::Least: return label + ": " + t.sorted.front().first; case Answer::Most: return label + ": " + t.sorted.back().first; case Answer::Total: return label + ": " + fmt(t.total); case Answer::List: { std::ostringstream o; for (const auto& [k, v] : t.sorted) o << k << "\t" << fmt(v) << "\n"; return o.str(); } } return label + ": NONE"; } } // namespace ctxstream