about summary refs log tree commit diff stats
path: root/cadence.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-03-28 15:49:50 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-03-28 15:49:50 -0400
commit251248adfab36b061eb8fdd08aa54b318462da65 (patch)
tree4ff1eb792a1e1b442ec2fa6a99e01b28518c5cf4 /cadence.cpp
parentaf49d3435302118d036c512aef1def848fb88089 (diff)
downloadcadence-251248adfab36b061eb8fdd08aa54b318462da65.tar.gz
cadence-251248adfab36b061eb8fdd08aa54b318462da65.tar.bz2
cadence-251248adfab36b061eb8fdd08aa54b318462da65.zip
Revamped bot with new algorithm
The percentages currently used by the generator are trial-and-error and may be changed in the future.

refs #1
Diffstat (limited to 'cadence.cpp')
-rw-r--r--cadence.cpp49
1 files changed, 27 insertions, 22 deletions
diff --git a/cadence.cpp b/cadence.cpp index 704f946..97a74b7 100644 --- a/cadence.cpp +++ b/cadence.cpp
@@ -13,8 +13,6 @@
13#include <hkutil/string.h> 13#include <hkutil/string.h>
14#include <hkutil/database.h> 14#include <hkutil/database.h>
15 15
16const std::vector<std::string> moods = {"party", "chill", "crazy", "happy", "sad", "instrumental", "vocal"};
17
18int main(int argc, char** argv) 16int main(int argc, char** argv)
19{ 17{
20 if (argc != 2) 18 if (argc != 2)
@@ -81,21 +79,25 @@ int main(int argc, char** argv)
81 { 79 {
82 std::cout << "Generating tweet..." << std::endl; 80 std::cout << "Generating tweet..." << std::endl;
83 81
84 std::string mood = moods[std::uniform_int_distribution<int>(0, moods.size()-1)(random_engine)]; 82 hatkirby::row songRow =
83 abdb.queryFirst(
84 "SELECT song_id, title, artist FROM songs ORDER BY RANDOM() LIMIT 1");
85 85
86 hatkirby::row song = 86 hatkirby::row moodRow =
87 abdb.queryFirst( 87 abdb.queryFirst(
88 "SELECT title, artist FROM songs WHERE category = ? ORDER BY RANDOM() LIMIT 1", 88 "SELECT mood FROM moods WHERE song_id = ? ORDER BY RANDOM() LIMIT 1",
89 { mood }); 89 { mpark::get<int>(songRow[0]) });
90 90
91 std::string songTitle = mpark::get<std::string>(song[0]); 91 std::string songTitle = mpark::get<std::string>(songRow[1]);
92 std::string songArtist = mpark::get<std::string>(song[1]); 92 std::string songArtist = mpark::get<std::string>(songRow[2]);
93 std::string mood = mpark::get<std::string>(moodRow[0]);
93 94
94 std::string action = "{" + hatkirby::uppercase(mood) + "}"; 95 std::string action = "{" + hatkirby::uppercase(mood) + "}";
95 int tknloc; 96 int tknloc;
96 while ((tknloc = action.find("{")) != std::string::npos) 97 while ((tknloc = action.find("{")) != std::string::npos)
97 { 98 {
98 std::string token = action.substr(tknloc+1, action.find("}")-tknloc-1); 99 std::string token = action.substr(tknloc+1, action.find("}")-tknloc-1);
100
99 std::string modifier; 101 std::string modifier;
100 int modloc; 102 int modloc;
101 if ((modloc = token.find(":")) != std::string::npos) 103 if ((modloc = token.find(":")) != std::string::npos)
@@ -104,10 +106,7 @@ int main(int argc, char** argv)
104 token = token.substr(0, modloc); 106 token = token.substr(0, modloc);
105 } 107 }
106 108
107 std::string canontkn; 109 std::string canontkn = hatkirby::uppercase(token);
108 std::transform(std::begin(token), std::end(token), std::back_inserter(canontkn), [] (char ch) {
109 return std::toupper(ch);
110 });
111 110
112 std::string result; 111 std::string result;
113 if (canontkn == "\\N") 112 if (canontkn == "\\N")
@@ -125,18 +124,23 @@ int main(int argc, char** argv)
125 124
126 result = group[dist(random_engine)]; 125 result = group[dist(random_engine)];
127 } else { 126 } else {
128 std::cout << "Badly formatted forms file:" << std::endl; 127 throw std::logic_error("No such form as " + canontkn);
129 std::cout << "No such form as " + canontkn << std::endl;
130
131 return 1;
132 } 128 }
133 129
134 if (modifier == "indefinite") 130 if (modifier == "indefinite")
135 { 131 {
136 if ((result.length() > 1) && (isupper(result[0])) && (isupper(result[1]))) 132 if (
133 (result.length() > 1) &&
134 isupper(result[0]) &&
135 isupper(result[1]))
137 { 136 {
138 result = "an " + result; 137 result = "an " + result;
139 } else if ((result[0] == 'a') || (result[0] == 'e') || (result[0] == 'i') || (result[0] == 'o') || (result[0] == 'u')) 138 } else if (
139 (result[0] == 'a') ||
140 (result[0] == 'e') ||
141 (result[0] == 'i') ||
142 (result[0] == 'o') ||
143 (result[0] == 'u'))
140 { 144 {
141 result = "an " + result; 145 result = "an " + result;
142 } else { 146 } else {
@@ -147,9 +151,7 @@ int main(int argc, char** argv)
147 std::string finalresult; 151 std::string finalresult;
148 if (islower(token[0])) 152 if (islower(token[0]))
149 { 153 {
150 std::transform(std::begin(result), std::end(result), std::back_inserter(finalresult), [] (char ch) { 154 finalresult = hatkirby::lowercase(result);
151 return std::tolower(ch);
152 });
153 } else if (isupper(token[0]) && !isupper(token[1])) 155 } else if (isupper(token[0]) && !isupper(token[1]))
154 { 156 {
155 auto words = hatkirby::split<std::list<std::string>>(result, " "); 157 auto words = hatkirby::split<std::list<std::string>>(result, " ");
@@ -158,7 +160,10 @@ int main(int argc, char** argv)
158 word[0] = std::toupper(word[0]); 160 word[0] = std::toupper(word[0]);
159 } 161 }
160 162
161 finalresult = hatkirby::implode(std::begin(words), std::end(words), " "); 163 finalresult = hatkirby::implode(
164 std::begin(words),
165 std::end(words),
166 " ");
162 } else { 167 } else {
163 finalresult = result; 168 finalresult = result;
164 } 169 }