68 lines
1.8 KiB
C++
68 lines
1.8 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <sstream>
|
|
|
|
// Function that takes a list of indices and returns a single string
|
|
// concatenating the corresponding vocab entries.
|
|
std::string processIndices(
|
|
const std::vector<int>& indices,
|
|
const std::vector<std::pair<int, std::string>>& vocab
|
|
) {
|
|
std::ostringstream oss;
|
|
for (size_t i = 0; i < indices.size(); ++i) {
|
|
int idx = indices[i];
|
|
if (idx > 0 && idx <= static_cast<int>(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<std::pair<int, std::string>> 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<int> 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;
|
|
}
|