From 1174ceb06633d82775df90d295dce1e110f4af84 Mon Sep 17 00:00:00 2001 From: hashirama Date: Fri, 4 Apr 2025 07:13:04 +0000 Subject: [PATCH] add in-memory processing --- silly_decoder.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 silly_decoder.cpp diff --git a/silly_decoder.cpp b/silly_decoder.cpp new file mode 100644 index 0000000..13ff321 --- /dev/null +++ b/silly_decoder.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include + +// Function that takes a list of indices and returns a single string +// concatenating the corresponding vocab entries. +std::string processIndices( + const std::vector& indices, + const std::vector>& vocab +) { + std::ostringstream oss; + for (size_t i = 0; i < indices.size(); ++i) { + int idx = indices[i]; + if (idx > 0 && idx <= static_cast(vocab.size())) { + oss << vocab[idx - 1].second; + } else { + oss << "[Index " << idx << " out of range]"; + } + + if (i != indices.size() - 1) + oss << ""; + } + return oss.str(); +} + +int main(int argc, char* argv[]) { + if (argc < 2) { + std::cerr << "Usage: " << argv[0] << " index1,index2,...\n"; + return 1; + } + + std::ifstream file("vocab.txt"); + if (!file) { + std::cerr << "Error opening file.\n"; + return 1; + } + + // Read vocab.txt and assign indices starting from 1. + std::vector> vocab; + std::string line; + int index = 1; + while (std::getline(file, line)) { + vocab.emplace_back(index++, line); + } + file.close(); + + // Convert CLI input into a list of indices. + std::vector indices; + std::istringstream input(argv[1]); + std::string token; + while (std::getline(input, token, ',')) { + try { + indices.push_back(std::stoi(token)); + } catch (const std::exception&) { + std::cerr << "Invalid index: " << token << '\n'; + } + } + + // Process the indices and retrieve the aggregated result as a single string. + std::string result = processIndices(indices, vocab); + + + std::cout << "Result: " << result << '\n'; + + return 0; +}