Datasets:
Download ctxstream/src/reduce.cpp from Rickesh/rlm-oolong-reproduction: direct link, hf CLI and curl.
- Browser
- Download file 4.07 kB
-
https://huggingface.co/datasets/Rickesh/rlm-oolong-reproduction/resolve/main/ctxstream/src/reduce.cpp
- Command line
-
hf download hf://datasets/Rickesh/rlm-oolong-reproduction/ctxstream/src/reduce.cpp
-
curl -L -o reduce.cpp https://huggingface.co/datasets/Rickesh/rlm-oolong-reproduction/resolve/main/ctxstream/src/reduce.cpp
4.07 kB
| 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 | |