ConfigLine Class Reference

This class is responsible for parsing input like hi-there xx=yyy a=b c empty= f-oo=Append(bar, sss) ba_z=123 bing='a b c' baz="a b c d='a b' e" and giving you access to the fields, in this case. More...

#include <text-utils.h>

Collaboration diagram for ConfigLine:

Public Member Functions

bool ParseLine (const std::string &line)
 
bool GetValue (const std::string &key, std::string *value)
 
bool GetValue (const std::string &key, BaseFloat *value)
 
bool GetValue (const std::string &key, int32 *value)
 
bool GetValue (const std::string &key, std::vector< int32 > *value)
 
bool GetValue (const std::string &key, bool *value)
 
bool HasUnusedValues () const
 
std::string UnusedValues () const
 returns e.g. More...
 
const std::string & FirstToken () const
 
const std::string WholeLine ()
 

Private Attributes

std::string whole_line_
 
std::string first_token_
 
std::map< std::string, std::pair< std::string, bool > > data_
 

Detailed Description

This class is responsible for parsing input like hi-there xx=yyy a=b c empty= f-oo=Append(bar, sss) ba_z=123 bing='a b c' baz="a b c d='a b' e" and giving you access to the fields, in this case.

FirstToken() == "hi-there", and key->value pairs:

xx->yyy, a->"b c", empty->"", f-oo->"Append(bar, sss)", ba_z->"123", bing->"a b c", baz->"a b c d='a b' e"

The first token is optional, if the line started with a key-value pair then FirstValue() will be empty.

Note: it can parse value fields with space inside them only if they are free of the '=' character. If values are going to contain the '=' character, you need to quote them with either single or double quotes.

Key values may contain -_a-zA-Z0-9, but must begin with a-zA-Z_.

Definition at line 205 of file text-utils.h.

Member Function Documentation

◆ FirstToken()

◆ GetValue() [1/5]

bool GetValue ( const std::string &  key,
std::string *  value 
)

Definition at line 427 of file text-utils.cc.

References ConfigLine::data_, and KALDI_ASSERT.

Referenced by LstmNonlinearityComponent::ConsolidateMemory(), PnormComponent::InitFromConfig(), DistributeComponent::InitFromConfig(), NormalizeComponent::InitFromConfig(), DropoutComponent::InitFromConfig(), RestrictedAttentionComponent::InitFromConfig(), ConvolutionComponent::InitFromConfig(), ElementwiseProductComponent::InitFromConfig(), BatchNormComponent::InitFromConfig(), StatisticsExtractionComponent::InitFromConfig(), TimeHeightConvolutionComponent::InitFromConfig(), LstmNonlinearityComponent::InitFromConfig(), StatisticsPoolingComponent::InitFromConfig(), AffineComponent::InitFromConfig(), TdnnComponent::InitFromConfig(), BackpropTruncationComponent::InitFromConfig(), MaxpoolingComponent::InitFromConfig(), BlockAffineComponent::InitFromConfig(), RepeatedAffineComponent::InitFromConfig(), NonlinearComponent::InitFromConfig(), ConstantComponent::InitFromConfig(), DropoutMaskComponent::InitFromConfig(), NaturalGradientAffineComponent::InitFromConfig(), GeneralDropoutComponent::InitFromConfig(), LinearComponent::InitFromConfig(), FixedAffineComponent::InitFromConfig(), SpecAugmentTimeMaskComponent::InitFromConfig(), SumGroupComponent::InitFromConfig(), FixedScaleComponent::InitFromConfig(), FixedBiasComponent::InitFromConfig(), NoOpComponent::InitFromConfig(), SumBlockComponent::InitFromConfig(), ClipGradientComponent::InitFromConfig(), PermuteComponent::InitFromConfig(), PerElementScaleComponent::InitFromConfig(), PerElementOffsetComponent::InitFromConfig(), ConstantFunctionComponent::InitFromConfig(), NaturalGradientPerElementScaleComponent::InitFromConfig(), ScaleAndOffsetComponent::InitFromConfig(), CompositeComponent::InitFromConfig(), UpdatableComponent::InitLearningRatesFromConfig(), Nnet::ProcessComponentConfigLine(), Nnet::ProcessComponentNodeConfigLine(), Nnet::ProcessDimRangeNodeConfigLine(), Nnet::ProcessInputNodeConfigLine(), Nnet::ProcessOutputNodeConfigLine(), kaldi::nnet3::ReadEditConfig(), Nnet::RemoveRedundantConfigLines(), and kaldi::UnitTestConfigLineParse().

427  {
428  KALDI_ASSERT(value != NULL);
429  std::map<std::string, std::pair<std::string, bool> >::iterator it = data_.begin();
430  for (; it != data_.end(); ++it) {
431  if (it->first == key) {
432  *value = (it->second).first;
433  (it->second).second = true;
434  return true;
435  }
436  }
437  return false;
438 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::map< std::string, std::pair< std::string, bool > > data_
Definition: text-utils.h:240

◆ GetValue() [2/5]

bool GetValue ( const std::string &  key,
BaseFloat value 
)

Definition at line 440 of file text-utils.cc.

References kaldi::ConvertStringToReal(), ConfigLine::data_, and KALDI_ASSERT.

440  {
441  KALDI_ASSERT(value != NULL);
442  std::map<std::string, std::pair<std::string, bool> >::iterator it = data_.begin();
443  for (; it != data_.end(); ++it) {
444  if (it->first == key) {
445  if (!ConvertStringToReal((it->second).first, value))
446  return false;
447  (it->second).second = true;
448  return true;
449  }
450  }
451  return false;
452 }
bool ConvertStringToReal(const std::string &str, T *out)
ConvertStringToReal converts a string into either float or double and returns false if there was any ...
Definition: text-utils.cc:238
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::map< std::string, std::pair< std::string, bool > > data_
Definition: text-utils.h:240

◆ GetValue() [3/5]

bool GetValue ( const std::string &  key,
int32 value 
)

Definition at line 454 of file text-utils.cc.

References kaldi::ConvertStringToInteger(), ConfigLine::data_, and KALDI_ASSERT.

454  {
455  KALDI_ASSERT(value != NULL);
456  std::map<std::string, std::pair<std::string, bool> >::iterator it = data_.begin();
457  for (; it != data_.end(); ++it) {
458  if (it->first == key) {
459  if (!ConvertStringToInteger((it->second).first, value))
460  return false;
461  (it->second).second = true;
462  return true;
463  }
464  }
465  return false;
466 }
bool ConvertStringToInteger(const std::string &str, Int *out)
Converts a string into an integer via strtoll and returns false if there was any kind of problem (i...
Definition: text-utils.h:118
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::map< std::string, std::pair< std::string, bool > > data_
Definition: text-utils.h:240

◆ GetValue() [4/5]

bool GetValue ( const std::string &  key,
std::vector< int32 > *  value 
)

Definition at line 468 of file text-utils.cc.

References ConfigLine::data_, KALDI_ASSERT, and kaldi::SplitStringToIntegers().

468  {
469  KALDI_ASSERT(value != NULL);
470  value->clear();
471  std::map<std::string, std::pair<std::string, bool> >::iterator it = data_.begin();
472  for (; it != data_.end(); ++it) {
473  if (it->first == key) {
474  if (!SplitStringToIntegers((it->second).first, ":,", true, value)) {
475  // KALDI_WARN << "Bad option " << (it->second).first;
476  return false;
477  }
478  (it->second).second = true;
479  return true;
480  }
481  }
482  return false;
483 }
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::map< std::string, std::pair< std::string, bool > > data_
Definition: text-utils.h:240

◆ GetValue() [5/5]

bool GetValue ( const std::string &  key,
bool value 
)

Definition at line 485 of file text-utils.cc.

References ConfigLine::data_, and KALDI_ASSERT.

485  {
486  KALDI_ASSERT(value != NULL);
487  std::map<std::string, std::pair<std::string, bool> >::iterator it = data_.begin();
488  for (; it != data_.end(); ++it) {
489  if (it->first == key) {
490  if ((it->second).first.size() == 0) return false;
491  switch (((it->second).first)[0]) {
492  case 'F':
493  case 'f':
494  *value = false;
495  break;
496  case 'T':
497  case 't':
498  *value = true;
499  break;
500  default:
501  return false;
502  }
503  (it->second).second = true;
504  return true;
505  }
506  }
507  return false;
508 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::map< std::string, std::pair< std::string, bool > > data_
Definition: text-utils.h:240

◆ HasUnusedValues()

bool HasUnusedValues ( ) const

Definition at line 510 of file text-utils.cc.

References ConfigLine::data_.

Referenced by kaldi::nnet3::GenerateRandomSimpleComponent(), PnormComponent::InitFromConfig(), DistributeComponent::InitFromConfig(), NormalizeComponent::InitFromConfig(), DropoutComponent::InitFromConfig(), ConvolutionComponent::InitFromConfig(), ElementwiseProductComponent::InitFromConfig(), BatchNormComponent::InitFromConfig(), StatisticsExtractionComponent::InitFromConfig(), LstmNonlinearityComponent::InitFromConfig(), StatisticsPoolingComponent::InitFromConfig(), AffineComponent::InitFromConfig(), BackpropTruncationComponent::InitFromConfig(), MaxpoolingComponent::InitFromConfig(), BlockAffineComponent::InitFromConfig(), RepeatedAffineComponent::InitFromConfig(), NonlinearComponent::InitFromConfig(), ConstantComponent::InitFromConfig(), NaturalGradientAffineComponent::InitFromConfig(), LinearComponent::InitFromConfig(), FixedAffineComponent::InitFromConfig(), SumGroupComponent::InitFromConfig(), FixedScaleComponent::InitFromConfig(), FixedBiasComponent::InitFromConfig(), NoOpComponent::InitFromConfig(), SumBlockComponent::InitFromConfig(), ClipGradientComponent::InitFromConfig(), PermuteComponent::InitFromConfig(), PerElementScaleComponent::InitFromConfig(), PerElementOffsetComponent::InitFromConfig(), ConstantFunctionComponent::InitFromConfig(), NaturalGradientPerElementScaleComponent::InitFromConfig(), ScaleAndOffsetComponent::InitFromConfig(), CompositeComponent::InitFromConfig(), Nnet::ProcessComponentConfigLine(), Nnet::ProcessComponentNodeConfigLine(), Nnet::ProcessDimRangeNodeConfigLine(), Nnet::ProcessInputNodeConfigLine(), Nnet::ProcessOutputNodeConfigLine(), kaldi::nnet3::ReadEditConfig(), and kaldi::UnitTestConfigLineParse().

510  {
511  std::map<std::string, std::pair<std::string, bool> >::const_iterator it = data_.begin();
512  for (; it != data_.end(); ++it) {
513  if (!(it->second).second) return true;
514  }
515  return false;
516 }
std::map< std::string, std::pair< std::string, bool > > data_
Definition: text-utils.h:240

◆ ParseLine()

bool ParseLine ( const std::string &  line)

Definition at line 343 of file text-utils.cc.

References ConfigLine::data_, ConfigLine::first_token_, kaldi::IsValidName(), KALDI_WARN, and ConfigLine::whole_line_.

Referenced by kaldi::nnet3::GenerateRandomSimpleComponent(), CompositeComponent::InitFromConfig(), kaldi::nnet3::ModifyNnetIvectorPeriod(), and kaldi::UnitTestConfigLineParse().

343  {
344  data_.clear();
345  whole_line_ = line;
346  if (line.size() == 0) return false; // Empty line
347  size_t pos = 0, size = line.size();
348  while (isspace(line[pos]) && pos < size) pos++;
349  if (pos == size)
350  return false; // whitespace-only line
351  size_t first_token_start_pos = pos;
352  // first get first_token_.
353  while (!isspace(line[pos]) && pos < size) {
354  if (line[pos] == '=') {
355  // If the first block of non-whitespace looks like "foo-bar=...",
356  // then we ignore it: there is no initial token, and FirstToken()
357  // is empty.
358  pos = first_token_start_pos;
359  break;
360  }
361  pos++;
362  }
363  first_token_ = std::string(line, first_token_start_pos, pos - first_token_start_pos);
364  // first_token_ is expected to be either empty or something like
365  // "component-node", which actually is a slightly more restrictive set of
366  // strings than IsValidName() checks for this is a convenient way to check it.
367  if (!first_token_.empty() && !IsValidName(first_token_))
368  return false;
369 
370  while (pos < size) {
371  if (isspace(line[pos])) {
372  pos++;
373  continue;
374  }
375 
376  // OK, at this point we know that we are pointing at nonspace.
377  size_t next_equals_sign = line.find_first_of("=", pos);
378  if (next_equals_sign == pos || next_equals_sign == std::string::npos) {
379  // we're looking for something like 'key=value'. If there is no equals sign,
380  // or it's not preceded by something, it's a parsing failure.
381  return false;
382  }
383  std::string key(line, pos, next_equals_sign - pos);
384  if (!IsValidName(key)) return false;
385 
386  // handle any quotes. we support key='blah blah' or key="foo bar".
387  // no escaping is supported.
388  if (line[next_equals_sign+1] == '\'' || line[next_equals_sign+1] == '"') {
389  char my_quote = line[next_equals_sign+1];
390  size_t next_quote = line.find_first_of(my_quote, next_equals_sign + 2);
391  if (next_quote == std::string::npos) { // no matching quote was found.
392  KALDI_WARN << "No matching quote for " << my_quote << " in config line '"
393  << line << "'";
394  return false;
395  } else {
396  std::string value(line, next_equals_sign + 2,
397  next_quote - next_equals_sign - 2);
398  data_.insert(std::make_pair(key, std::make_pair(value, false)));
399  pos = next_quote + 1;
400  continue;
401  }
402  } else {
403  // we want to be able to parse something like "... input=Offset(a, -1) foo=bar":
404  // in general, config values with spaces in them, even without quoting.
405 
406  size_t next_next_equals_sign = line.find_first_of("=", next_equals_sign + 1),
407  terminating_space = size;
408 
409  if (next_next_equals_sign != std::string::npos) { // found a later equals sign.
410  size_t preceding_space = line.find_last_of(" \t", next_next_equals_sign);
411  if (preceding_space != std::string::npos &&
412  preceding_space > next_equals_sign)
413  terminating_space = preceding_space;
414  }
415  while (isspace(line[terminating_space - 1]) && terminating_space > 0)
416  terminating_space--;
417 
418  std::string value(line, next_equals_sign + 1,
419  terminating_space - (next_equals_sign + 1));
420  data_.insert(std::make_pair(key, std::make_pair(value, false)));
421  pos = terminating_space;
422  }
423  }
424  return true;
425 }
std::string whole_line_
Definition: text-utils.h:233
bool IsValidName(const std::string &name)
Returns true if &#39;name&#39; would be a valid name for a component or node in a nnet3Nnet.
Definition: text-utils.cc:553
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string first_token_
Definition: text-utils.h:237
std::map< std::string, std::pair< std::string, bool > > data_
Definition: text-utils.h:240

◆ UnusedValues()

std::string UnusedValues ( ) const

returns e.g.

foo=bar xxx=yyy if foo and xxx were not consumed by one of the GetValue() functions.

Definition at line 518 of file text-utils.cc.

References ConfigLine::data_.

Referenced by kaldi::nnet3::GenerateRandomSimpleComponent(), ConvolutionComponent::InitFromConfig(), BatchNormComponent::InitFromConfig(), StatisticsExtractionComponent::InitFromConfig(), LstmNonlinearityComponent::InitFromConfig(), StatisticsPoolingComponent::InitFromConfig(), AffineComponent::InitFromConfig(), MaxpoolingComponent::InitFromConfig(), RepeatedAffineComponent::InitFromConfig(), NaturalGradientAffineComponent::InitFromConfig(), LinearComponent::InitFromConfig(), SumBlockComponent::InitFromConfig(), PermuteComponent::InitFromConfig(), PerElementScaleComponent::InitFromConfig(), PerElementOffsetComponent::InitFromConfig(), ScaleAndOffsetComponent::InitFromConfig(), CompositeComponent::InitFromConfig(), Nnet::ProcessComponentConfigLine(), Nnet::ProcessComponentNodeConfigLine(), Nnet::ProcessDimRangeNodeConfigLine(), Nnet::ProcessInputNodeConfigLine(), Nnet::ProcessOutputNodeConfigLine(), kaldi::nnet3::ReadEditConfig(), and kaldi::UnitTestConfigLineParse().

518  {
519  std::string unused_str;
520  std::map<std::string, std::pair<std::string, bool> >::const_iterator it = data_.begin();
521  for (; it != data_.end(); ++it) {
522  if (!(it->second).second) {
523  if (unused_str == "")
524  unused_str = it->first + "=" + (it->second).first;
525  else
526  unused_str += " " + it->first + "=" + (it->second).first;
527  }
528  }
529  return unused_str;
530 }
std::map< std::string, std::pair< std::string, bool > > data_
Definition: text-utils.h:240

◆ WholeLine()

const std::string WholeLine ( )
inline

Definition at line 230 of file text-utils.h.

References ConfigLine::whole_line_.

Referenced by kaldi::nnet3::GenerateRandomSimpleComponent(), PnormComponent::InitFromConfig(), DistributeComponent::InitFromConfig(), NormalizeComponent::InitFromConfig(), DropoutComponent::InitFromConfig(), RestrictedAttentionComponent::InitFromConfig(), ConvolutionComponent::InitFromConfig(), ElementwiseProductComponent::InitFromConfig(), StatisticsExtractionComponent::InitFromConfig(), TimeHeightConvolutionComponent::InitFromConfig(), LstmNonlinearityComponent::InitFromConfig(), StatisticsPoolingComponent::InitFromConfig(), AffineComponent::InitFromConfig(), TdnnComponent::InitFromConfig(), BackpropTruncationComponent::InitFromConfig(), MaxpoolingComponent::InitFromConfig(), BlockAffineComponent::InitFromConfig(), RepeatedAffineComponent::InitFromConfig(), NonlinearComponent::InitFromConfig(), ConstantComponent::InitFromConfig(), NaturalGradientAffineComponent::InitFromConfig(), LinearComponent::InitFromConfig(), FixedAffineComponent::InitFromConfig(), SumGroupComponent::InitFromConfig(), FixedScaleComponent::InitFromConfig(), FixedBiasComponent::InitFromConfig(), NoOpComponent::InitFromConfig(), ClipGradientComponent::InitFromConfig(), PermuteComponent::InitFromConfig(), ConstantFunctionComponent::InitFromConfig(), NaturalGradientPerElementScaleComponent::InitFromConfig(), ScaleAndOffsetComponent::InitFromConfig(), CompositeComponent::InitFromConfig(), UpdatableComponent::InitLearningRatesFromConfig(), Nnet::ProcessComponentConfigLine(), Nnet::ProcessComponentNodeConfigLine(), Nnet::ProcessDimRangeNodeConfigLine(), Nnet::ProcessInputNodeConfigLine(), Nnet::ProcessOutputNodeConfigLine(), kaldi::nnet3::ReadEditConfig(), and Nnet::RemoveRedundantConfigLines().

230 { return whole_line_; }
std::string whole_line_
Definition: text-utils.h:233

Member Data Documentation

◆ data_

std::map<std::string, std::pair<std::string, bool> > data_
private

◆ first_token_

std::string first_token_
private

Definition at line 237 of file text-utils.h.

Referenced by ConfigLine::FirstToken(), and ConfigLine::ParseLine().

◆ whole_line_

std::string whole_line_
private

Definition at line 233 of file text-utils.h.

Referenced by ConfigLine::ParseLine(), and ConfigLine::WholeLine().


The documentation for this class was generated from the following files: