diff options
Diffstat (limited to 'generator/pronunciation.cpp')
-rw-r--r-- | generator/pronunciation.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/generator/pronunciation.cpp b/generator/pronunciation.cpp new file mode 100644 index 0000000..eb07607 --- /dev/null +++ b/generator/pronunciation.cpp | |||
@@ -0,0 +1,87 @@ | |||
1 | #include "pronunciation.h" | ||
2 | #include <list> | ||
3 | #include <algorithm> | ||
4 | #include <cctype> | ||
5 | #include <iterator> | ||
6 | #include "database.h" | ||
7 | #include "field.h" | ||
8 | #include "../lib/util.h" | ||
9 | |||
10 | namespace verbly { | ||
11 | namespace generator { | ||
12 | |||
13 | int pronunciation::nextId_ = 0; | ||
14 | |||
15 | pronunciation::pronunciation(std::string phonemes) : | ||
16 | id_(nextId_++), | ||
17 | phonemes_(phonemes) | ||
18 | { | ||
19 | auto phonemeList = split<std::list<std::string>>(phonemes, " "); | ||
20 | |||
21 | auto rhymeStart = std::find_if(std::begin(phonemeList), std::end(phonemeList), [] (std::string phoneme) { | ||
22 | return phoneme.find("1") != std::string::npos; | ||
23 | }); | ||
24 | |||
25 | // Rhyme detection | ||
26 | if (rhymeStart != std::end(phonemeList)) | ||
27 | { | ||
28 | std::list<std::string> rhymePhonemes; | ||
29 | |||
30 | std::transform(rhymeStart, std::end(phonemeList), std::back_inserter(rhymePhonemes), [] (std::string phoneme) { | ||
31 | std::string naked; | ||
32 | |||
33 | std::remove_copy_if(std::begin(phoneme), std::end(phoneme), std::back_inserter(naked), [] (char ch) { | ||
34 | return std::isdigit(ch); | ||
35 | }); | ||
36 | |||
37 | return naked; | ||
38 | }); | ||
39 | |||
40 | rhyme_ = implode(std::begin(rhymePhonemes), std::end(rhymePhonemes), " "); | ||
41 | |||
42 | if (rhymeStart != std::begin(phonemeList)) | ||
43 | { | ||
44 | prerhyme_ = *std::prev(rhymeStart); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | // Syllable/stress | ||
49 | for (std::string phoneme : phonemeList) | ||
50 | { | ||
51 | if (std::isdigit(phoneme.back())) | ||
52 | { | ||
53 | // It's a vowel! | ||
54 | syllables_++; | ||
55 | |||
56 | if (phoneme.back() == '1') | ||
57 | { | ||
58 | stress_.push_back('1'); | ||
59 | } else { | ||
60 | stress_.push_back('0'); | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | |||
66 | database& operator<<(database& db, const pronunciation& arg) | ||
67 | { | ||
68 | std::list<field> fields; | ||
69 | |||
70 | fields.emplace_back("pronunciation_id", arg.getId()); | ||
71 | fields.emplace_back("phonemes", arg.getPhonemes()); | ||
72 | fields.emplace_back("syllables", arg.getSyllables()); | ||
73 | fields.emplace_back("stress", arg.getStress()); | ||
74 | |||
75 | if (arg.hasRhyme()) | ||
76 | { | ||
77 | fields.emplace_back("rhyme", arg.getRhymePhonemes()); | ||
78 | fields.emplace_back("prerhyme", arg.getPrerhyme()); | ||
79 | } | ||
80 | |||
81 | db.insertIntoTable("pronunciations", std::move(fields)); | ||
82 | |||
83 | return db; | ||
84 | } | ||
85 | |||
86 | }; | ||
87 | }; | ||