Skip to content

Commit

Permalink
fixed prediction sentiment stuff (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mast3rwaf1z authored May 20, 2024
1 parent 1520bac commit 00550e4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
12 changes: 6 additions & 6 deletions files/testdata/journals.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
{
"data":[{
"qid":"1",
"meta":"It was horrible!",
"rating":"4"
"meta":"I loved it!",
"rating":"1"
}]
},
{
"data":[{
"qid":"2",
"qid":"1",
"meta":"It was okay...",
"rating":"2"
}]
},
{
"data":[{
"qid":"3",
"meta":"I loved it!",
"rating":"1"
"qid":"1",
"meta":"It was horrible!",
"rating":"4"
}]
}
]
20 changes: 8 additions & 12 deletions include/Routes/Journals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,20 @@ class Journals : public Route {
auto rating = entry["rating"].get<string>();
// run sentiment analysis on answer
increment_running(userid);
sentiment_threads[userid].push_back(thread([meta, this, rating, jid, qid, userid, question]() {
auto analysis = run_cmd(format("python ./lib/datasets/sentiment_analysis.py \"{}\" \"{}\"", meta, question));
auto sa_stdout = split_string(analysis["stdout"], "\n");
if (sa_stdout.size() < 2) {
decrement_running(userid);
return;
}
auto meta_value = sa_stdout[0];
auto question_value = sa_stdout[1];
sentiment_threads[userid].push_back(thread([this](string question, string meta, int uid, int jid, int qid, string rating) {
log<DEBUG>("Question: {}", question);
log<DEBUG>("Answer: {}", meta);
auto meta_value = run_cmd(format("python ./lib/datasets/sentiment_analysis.py \"{}\"", meta))["stdout"];
auto question_value = run_cmd(format("python ./lib/datasets/sentiment_analysis.py \"{}\"", question))["stdout"];
auto final_value = mean({stod(question_value), stod(meta_value)});
db["answers"].add({
{"value", to_string(final_value)},
{"rating", to_string(stod(rating)/5.0)},
{"journalId", to_string(jid)},
{"questionId", qid}
{"questionId", to_string(qid)}
});
decrement_running(userid);
}));
decrement_running(uid);
}, question, meta, userid, jid, stoi(qid), rating));
}
respond(&response, string("Successfully created new journal."));
} else {
Expand Down
6 changes: 4 additions & 2 deletions include/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ class RunCommandException : public exception {
map<string, string> run_cmd(string command) {
try {
map<string, string> result;
auto stdout_path = "/tmp/p8_backend_stdout";
auto stderr_path = "/tmp/p8_backend_stderr";
char stdout_path[L_tmpnam];
char stderr_path[L_tmpnam];
tmpnam(stdout_path);
tmpnam(stderr_path);
auto final_command = format("bash -c '{} 2> {} 1> {}'", command, stderr_path, stdout_path);
auto error_code = system(final_command.c_str());
ifstream stdout_file{stdout_path};
Expand Down
2 changes: 1 addition & 1 deletion lib/datasets

0 comments on commit 00550e4

Please sign in to comment.