-
Notifications
You must be signed in to change notification settings - Fork 0
/
phoneme.cpp
60 lines (49 loc) · 1.62 KB
/
phoneme.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include "random.hpp"
#include "phoneme.hpp"
std::vector<std::string> tk::phoneme::phonemes;
std::vector<std::vector<std::string>> tk::phoneme::pronounce;
tk::phoneme::phoneme(std::string s = "null"){ // Constructor
p = s;
}
void tk::phoneme::set_phonemes(){
std::ifstream phoneme_file("./data/phonemes", std::ios::in); // Loads the file containing a list of phonemes.
if(!phoneme_file.is_open()) std::cout << "Phonemes not found.\n";
std::string line;
while(std::getline(phoneme_file, line)){ // Reads the file.
phonemes.push_back(line);
std::ifstream file("./data/phoneme/"+line, std::ios::in);
if(!file.is_open()) std::cout << line << " not found.\n";
std::vector<std::string> vec;
std::string var;
while(std::getline(file, var)) vec.push_back(var); // Populate the pronounce vector.
pronounce.push_back(vec);
file.close();
}
phoneme_file.close();
}
tk::phoneme tk::phoneme::get_phoneme(tk::random& rand){
return tk::phoneme(phonemes[rand(0,phonemes.size()-1)]); // Returns a random phoneme.
}
tk::phoneme tk::phoneme::get_phoneme(tk::random& rand, tk::phoneme p){ // Selects a random phoneme, then checks pronounceability. Returns if valid, loops if not.
//return tk::phoneme::get_phoneme(rand);
std::string var;
while(true){
var = phonemes[rand(0,phonemes.size()-1)];
for(int i = 0; i < phonemes.size(); i++){
if(phonemes[i] == p.get()){
for(int j = 0; j < pronounce[i].size(); j++){
if(pronounce[i][j] == var){
return tk::phoneme(var);
}
}
}
}
}
}
std::string tk::phoneme::get(){
return this->p;
}