Datasets:
File size: 4,069 Bytes
829dbce | 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | #include "ctxstream.hpp"
#include <algorithm>
#include <cctype>
#include <map>
#include <sstream>
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<unsigned char>(s[a]))) ++a;
while (b > a && std::isspace(static_cast<unsigned char>(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<Record> parse_records(const std::string& raw) {
std::vector<Record> 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<SegmentResult>& results) {
Tally out;
std::map<std::string, double> 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<Record> recs = parse_records(r.raw);
out.parsed_records += static_cast<int>(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<int>(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<long long>(v)) o << static_cast<long long>(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
|