From 328139fa0efefd6c4dfced424cce92acfc19c98f Mon Sep 17 00:00:00 2001 From: Thomas Jensen <32744171+Mast3rwaf1z@users.noreply.github.com> Date: Mon, 20 May 2024 09:36:52 +0200 Subject: [PATCH] fixed issues with journals (#86) * fixed issues * changed printing --- include/Routes/Journals.hpp | 10 +++++++--- include/Utils.hpp | 20 ++++++++++++-------- lib/datasets | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/include/Routes/Journals.hpp b/include/Routes/Journals.hpp index 36129a5..04547ae 100644 --- a/include/Routes/Journals.hpp +++ b/include/Routes/Journals.hpp @@ -38,9 +38,13 @@ class Journals : public Route { auto meta = entry["meta"].get(); auto rating = entry["rating"].get(); // run sentiment analysis on answer - auto result = split_string(run_cmd(format("python ./lib/datasets/sentiment_analysis.py \"{}\" \"{}\"", meta, question))["stdout"], "\n"); - auto meta_value = result[0]; - auto question_value = result[1]; + 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) { + continue; + } + auto meta_value = sa_stdout[0]; + auto question_value = sa_stdout[1]; auto final_value = mean({stod(question_value), stod(meta_value)}); db["answers"].add({ {"value", to_string(final_value)}, diff --git a/include/Utils.hpp b/include/Utils.hpp index f15fb6a..164dd28 100644 --- a/include/Utils.hpp +++ b/include/Utils.hpp @@ -12,6 +12,7 @@ #include #include #include +#include using namespace std; @@ -173,14 +174,17 @@ map run_cmd(string command) { 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}; - ifstream stderr_file{stdout_path}; - stdout_file >> result["stdout"]; - stderr_file >> result["stderr"]; - if (error_code) { - cout << "command: " << final_command << endl; - cout << "stdout: " << result["stdout"] << endl; - cout << "stderr: " << result["stderr"] << endl; - } + ifstream stderr_file{stderr_path}; + stringstream out_stream; + stringstream err_stream; + out_stream << stdout_file.rdbuf(); + err_stream << stderr_file.rdbuf(); + result["stdout"] = out_stream.str(); + result["stderr"] = err_stream.str(); + + cout << "stdout: " << result["stdout"] << endl; + cout << "stderr: " << result["stderr"] << endl; + return result; } catch (exception& e) { diff --git a/lib/datasets b/lib/datasets index 870dda2..0d40f71 160000 --- a/lib/datasets +++ b/lib/datasets @@ -1 +1 @@ -Subproject commit 870dda2a064243c96c3dd15f400834d0f9b32b91 +Subproject commit 0d40f715999199b0e4759a56a35e5fecd407e545