Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check program input #23

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
run: conan install cmake/3.16.4@ -g=virtualrunenv
working-directory: ./build
- name: Build
run: conan install -s compiler.libcxx=libstdc++ .. && cmake .. && cmake --build ./ --target all --parallel
run: conan install -s compiler.libcxx=libstdc++ --build=missing .. && cmake .. && cmake --build ./ --target all --parallel
working-directory: ./build
- name: Test
run: ctest --parallel
Expand Down
9 changes: 9 additions & 0 deletions headers/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ class Configuration {

std::string getClientSecret() const;

void validate_configuration_values();

private:

void validate_string_value(const char *name, const std::string &value, const std::vector<std::string> &allowedValues);

std::string language;
std::string topic;
std::string grammarPath;
Expand All @@ -60,6 +65,10 @@ class Configuration {
std::string label;
std::string clientId;
std::string clientSecret;
std::vector<std::string> allowedTopicValues = {"GENERIC", "BANKING", "TELCO", "INSURANCE"};
std::vector<std::string> allowedLanguageValues = {"en-US", "en-GB", "pt-BR", "es", "es-419", "tr", "ja", "fr", "fr-CA", "de", "it"};
std::vector<std::string> allowedAsrVersionValues = {"V1", "V2"};

};


Expand Down
30 changes: 28 additions & 2 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "Configuration.h"

#include "gRpcExceptions.h"
#include "logger.h"

#include <cxxopts.hpp>


Configuration::Configuration() : host("csr.api.speechcenter.verbio.com"), topic("generic"), language("en-US"),
Configuration::Configuration() : host("us.speechcenter.verbio.com"), topic("generic"), language("en-US"),
sampleRate(8000) {}

Configuration::Configuration(int argc, char **argv) : Configuration() {
Expand Down Expand Up @@ -51,6 +51,7 @@ void Configuration::parse(int argc, char **argv) {
if ((parsedOptions.count("t") == 0) == (parsedOptions.count("g") == 0))
throw GrpcException("Topic and grammar options are mutually exclusive and at least one is needed.");

validate_configuration_values();
}

std::string Configuration::getAudioPath() const {
Expand Down Expand Up @@ -107,4 +108,29 @@ std::string Configuration::getClientId() const {

std::string Configuration::getClientSecret() const {
return clientSecret;
}

void Configuration::validate_configuration_values() {

if(sampleRate != 8000 and sampleRate != 16000) {
throw std::runtime_error("Unsupported parameter value. Allowed values sample rate: 8000 1600");
}

validate_string_value("topic", topic, allowedTopicValues);
validate_string_value("language", language, allowedLanguageValues);
validate_string_value("asr version", asrVersion, allowedAsrVersionValues);
}


void Configuration::validate_string_value(const char *name, const std::string &value, const std::vector<std::string> &allowedValues) {
if (std::find(allowedValues.begin(), allowedValues.end(), value) == allowedValues.end())
{
std::stringstream ss;
std::copy(allowedValues.begin(), allowedValues.end(), std::ostream_iterator<std::string>(ss, " "));
std::string allowedValuesList = ss.str();
std::string nameString(name);
std::string errorMessage("Unsupported parameter value. Allowed values for ");
std::string exceptionMessage = errorMessage + nameString.append(": ") + allowedValuesList;
throw std::runtime_error(exceptionMessage);
}
}
1 change: 1 addition & 0 deletions src/RecognitionClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ RecognitionClient::convertTopic(const std::string &topicName) {

auto topicIter = validTopics.find(topicUpper);
if (topicIter == validTopics.end()) {
ERROR("Unsupported topic: {}", topicName);
throw UnknownTopicModel(topicUpper);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main(int argc, char *argv[]) {
Configuration configuration(argc, argv);
RecognitionClient client(configuration);
client.performStreamingRecognition();
} catch (GrpcException &e) {
} catch (std::exception &e) {
ERROR(e.what());
return -1;
}
Expand Down
Loading