Skip to content

Commit

Permalink
allow passing empty value in parameters (#2289)
Browse files Browse the repository at this point in the history
  • Loading branch information
guolinke committed Jul 28, 2019
1 parent 207bb3e commit 274f340
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 4 additions & 4 deletions include/LightGBM/config.h
Expand Up @@ -850,7 +850,7 @@ struct Config {
inline bool Config::GetString(
const std::unordered_map<std::string, std::string>& params,
const std::string& name, std::string* out) {
if (params.count(name) > 0) {
if (params.count(name) > 0 && !params.at(name).empty()) {
*out = params.at(name);
return true;
}
Expand All @@ -860,7 +860,7 @@ inline bool Config::GetString(
inline bool Config::GetInt(
const std::unordered_map<std::string, std::string>& params,
const std::string& name, int* out) {
if (params.count(name) > 0) {
if (params.count(name) > 0 && !params.at(name).empty()) {
if (!Common::AtoiAndCheck(params.at(name).c_str(), out)) {
Log::Fatal("Parameter %s should be of type int, got \"%s\"",
name.c_str(), params.at(name).c_str());
Expand All @@ -873,7 +873,7 @@ inline bool Config::GetInt(
inline bool Config::GetDouble(
const std::unordered_map<std::string, std::string>& params,
const std::string& name, double* out) {
if (params.count(name) > 0) {
if (params.count(name) > 0 && !params.at(name).empty()) {
if (!Common::AtofAndCheck(params.at(name).c_str(), out)) {
Log::Fatal("Parameter %s should be of type double, got \"%s\"",
name.c_str(), params.at(name).c_str());
Expand All @@ -886,7 +886,7 @@ inline bool Config::GetDouble(
inline bool Config::GetBool(
const std::unordered_map<std::string, std::string>& params,
const std::string& name, bool* out) {
if (params.count(name) > 0) {
if (params.count(name) > 0 && !params.at(name).empty()) {
std::string value = params.at(name);
std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
if (value == std::string("false") || value == std::string("-")) {
Expand Down
7 changes: 5 additions & 2 deletions src/io/config.cpp
Expand Up @@ -14,9 +14,12 @@ namespace LightGBM {

void Config::KV2Map(std::unordered_map<std::string, std::string>& params, const char* kv) {
std::vector<std::string> tmp_strs = Common::Split(kv, '=');
if (tmp_strs.size() == 2) {
if (tmp_strs.size() == 2 || tmp_strs.size() == 1) {
std::string key = Common::RemoveQuotationSymbol(Common::Trim(tmp_strs[0]));
std::string value = Common::RemoveQuotationSymbol(Common::Trim(tmp_strs[1]));
std::string value = "";
if (tmp_strs.size() == 2) {
value = Common::RemoveQuotationSymbol(Common::Trim(tmp_strs[1]));
}
if (!Common::CheckASCII(key) || !Common::CheckASCII(value)) {
Log::Fatal("Do not support non-ascii characters in config.");
}
Expand Down

0 comments on commit 274f340

Please sign in to comment.