File size: 1,791 Bytes
ab1b068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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;
}