tiny-voca-hindi / cpp /tools /g2p_dump.cpp
RL10x's picture
Add inference code (Python + C++ engine + web page)
ab1b068 verified
Raw History Blame Contribute Delete
1.79 kB
// Run the C++ front end over a text file (one input per line) and write, per line, tab-separated:
// phoneme ids (space separated) or "NOTHING" | nasal flags | sentences(finished(text)) joined by U+001F |
// shorter(text) joined by U+001F
// scripts/tts_hi_g2p_compare.py runs the Python front end on the same lines and reports every difference.
// tts_g2p_dump model.gguf texts.txt out.tsv
#include <fstream>
#include <iostream>
#include <string>
#include "g2p.h"
#include "tts.h"
static std::string join(const std::vector<std::string>& v, const std::string& sep) {
std::string s;
for (size_t i = 0; i < v.size(); ++i) { if (i) s += sep; s += v[i]; }
return s;
}
int main(int argc, char** argv) {
if (argc < 4) { std::cerr << "usage: tts_g2p_dump model.gguf texts.txt out.tsv\n"; return 2; }
auto model = TtsModel::load(argv[1], 1);
HindiG2P g2p(model->symbols());
std::ifstream in(argv[2], std::ios::binary);
std::ofstream out(argv[3], std::ios::binary);
std::string line;
const std::string US = "\x1f";
size_t n = 0;
while (std::getline(in, line)) {
if (!line.empty() && line.back() == '\r') line.pop_back();
std::string ids, stress;
try {
const Phonemized p = g2p.phonemize(line);
for (size_t i = 0; i < p.ids.size(); ++i) {
if (i) { ids += ' '; stress += ' '; }
ids += std::to_string(p.ids[i]); stress += std::to_string(p.stress[i]);
}
} catch (const NothingToPronounce&) {
ids = "NOTHING";
}
out << ids << '\t' << stress << '\t' << join(split_sentences(finished(line)), US) << '\t'
<< join(shorter(line), US) << '\n';
++n;
}
std::cerr << n << " lines\n";
return 0;
}