Vowpal Wabbit
Classes | Namespaces | Macros | Functions
parse_example_json.h File Reference
#include "parse_primitives.h"
#include "v_array.h"
#include <rapidjson/reader.h>
#include <rapidjson/error/en.h>
#include "cb.h"
#include "conditional_contextual_bandit.h"
#include "best_constant.h"
#include <algorithm>
#include <vector>

Go to the source code of this file.

Classes

struct  BaseState< audit >
 
struct  Context< audit >
 
struct  Namespace< audit >
 
struct  BaseState< audit >
 
class  LabelObjectState< audit >
 
struct  LabelSinglePropertyState< audit >
 
struct  LabelIndexState< audit >
 
struct  LabelState< audit >
 
struct  TextState< audit >
 
struct  TagState< audit >
 
struct  MultiState< audit >
 
struct  SlotsState< audit >
 
class  ArrayState< audit >
 
struct  IgnoreState< audit >
 
class  DefaultState< audit >
 
class  ArrayToVectorState< audit, T >
 
class  StringToStringState< audit >
 
class  FloatToFloatState< audit >
 
class  BoolToBoolState< audit >
 
struct  DecisionServiceInteraction
 
class  CCBOutcomeList< audit >
 
class  DecisionServiceState< audit >
 
struct  Context< audit >
 
struct  VWReaderHandler< audit >
 
struct  json_parser< audit >
 

Namespaces

 VW
 

Macros

#define _stricmp   strcasecmp
 

Functions

template<bool audit>
void VW::read_line_json (vw &all, v_array< example *> &examples, char *line, example_factory_t example_factory, void *ex_factory_context)
 
void VW::apply_pdrop (vw &all, float pdrop, v_array< example *> &examples)
 
template<bool audit>
void VW::read_line_decision_service_json (vw &all, v_array< example *> &examples, char *line, size_t length, bool copy_line, example_factory_t example_factory, void *ex_factory_context, DecisionServiceInteraction *data)
 
template<bool audit>
bool parse_line_json (vw *all, char *line, size_t num_chars, v_array< example *> &examples)
 
void prepare_for_learner (vw *all, v_array< example *> &examples)
 
template<bool audit>
void line_to_examples_json (vw *all, char *line, size_t num_chars, v_array< example *> &examples)
 
template<bool audit>
int read_features_json (vw *all, v_array< example *> &examples)
 

Macro Definition Documentation

◆ _stricmp

#define _stricmp   strcasecmp

Function Documentation

◆ line_to_examples_json()

template<bool audit>
void line_to_examples_json ( vw all,
char *  line,
size_t  num_chars,
v_array< example *> &  examples 
)

Definition at line 1489 of file parse_example_json.h.

References VW::get_unused_example(), prepare_for_learner(), v_array< T >::push_back(), and VW::return_multiple_example().

1490 {
1491  bool good_example = parse_line_json<audit>(all, line, num_chars, examples);
1492  if (!good_example)
1493  {
1494  VW::return_multiple_example(*all, examples);
1495  examples.push_back(&VW::get_unused_example(all));
1496  return;
1497  }
1498 
1499  prepare_for_learner(all, examples);
1500 }
void return_multiple_example(vw &all, v_array< example *> &examples)
Definition: example.cc:251
void push_back(const T &new_ele)
Definition: v_array.h:107
void prepare_for_learner(vw *all, v_array< example *> &examples)
example & get_unused_example(vw *all)
Definition: parser.cc:664

◆ parse_line_json()

template<bool audit>
bool parse_line_json ( vw all,
char *  line,
size_t  num_chars,
v_array< example *> &  examples 
)

Definition at line 1429 of file parse_example_json.h.

References DecisionServiceInteraction::actions, parser::decision_service_json, VW::get_unused_example(), vw::p, VW::return_multiple_example(), and DecisionServiceInteraction::skipLearn.

1430 {
1431  if (all->p->decision_service_json)
1432  {
1433  // Skip lines that do not start with "{"
1434  if (line[0] != '{')
1435  {
1436  return false;
1437  }
1438 
1439  DecisionServiceInteraction interaction;
1440  VW::template read_line_decision_service_json<audit>(*all, examples, line, num_chars, false,
1441  reinterpret_cast<VW::example_factory_t>(&VW::get_unused_example), all, &interaction);
1442 
1443  // TODO: In refactoring the parser to be usable standalone, we need to ensure that we
1444  // stop suppressing "skipLearn" interactions. Also, not sure if this is the right logic
1445  // for counterfactual. (@marco)
1446  if (interaction.skipLearn)
1447  {
1448  VW::return_multiple_example(*all, examples);
1449  examples.push_back(&VW::get_unused_example(all));
1450  return false;
1451  }
1452 
1453  // let's ask to continue reading data until we find a line with actions provided
1454  if (interaction.actions.size() == 0)
1455  {
1456  // VW::return_multiple_example(*all, examples);
1457  // examples.push_back(&VW::get_unused_example(all));
1458  return false;
1459  }
1460  }
1461  else
1462  VW::template read_line_json<audit>(
1463  *all, examples, line, reinterpret_cast<VW::example_factory_t>(&VW::get_unused_example), all);
1464 
1465  return true;
1466 }
void return_multiple_example(vw &all, v_array< example *> &examples)
Definition: example.cc:251
example &(* example_factory_t)(void *)
parser * p
Definition: global_data.h:377
void push_back(const T &new_ele)
Definition: v_array.h:107
bool decision_service_json
Definition: parser.h:105
example & get_unused_example(vw *all)
Definition: parser.cc:664

◆ prepare_for_learner()

void prepare_for_learner ( vw all,
v_array< example *> &  examples 
)
inline

Definition at line 1468 of file parse_example_json.h.

References VW::get_unused_example(), v_array< T >::push_back(), v_array< T >::size(), and substring_to_example().

Referenced by line_to_examples_json(), and read_features_json().

1469 {
1470  // note: the json parser does single pass parsing and cannot determine if a shared example is needed.
1471  // since the communication between the parsing thread the main learner expects examples to be requested in order (as
1472  // they're layed out in memory) there is no way to determine upfront if a shared example exists thus even if there are
1473  // no features for the shared example, still an empty example is returned.
1474 
1475  // insert new line example at the end
1476  if (examples.size() > 1)
1477  {
1478  example& ae = VW::get_unused_example(all);
1479  char empty = '\0';
1480  substring example = {&empty, &empty};
1481  substring_to_example(all, &ae, example);
1482 
1483  examples.push_back(&ae);
1484  }
1485 }
size_t size() const
Definition: v_array.h:68
void substring_to_example(vw *all, example *ae, substring example)
void push_back(const T &new_ele)
Definition: v_array.h:107
example & get_unused_example(vw *all)
Definition: parser.cc:664

◆ read_features_json()

template<bool audit>
int read_features_json ( vw all,
v_array< example *> &  examples 
)

Definition at line 1503 of file parse_example_json.h.

References prepare_for_learner(), and read_features().

1504 {
1505  // Keep reading lines until a valid set of examples is produced.
1506  bool reread;
1507  do
1508  {
1509  reread = false;
1510 
1511  char* line;
1512  size_t num_chars;
1513  size_t num_chars_initial = read_features(all, line, num_chars);
1514  if (num_chars_initial < 1)
1515  return (int)num_chars_initial;
1516 
1517  // Ensure there is a null terminator.
1518  line[num_chars] = '\0';
1519 
1520  reread = !parse_line_json<audit>(all, line, num_chars, examples);
1521  } while (reread);
1522 
1523  prepare_for_learner(all, examples);
1524 
1525  return 1;
1526 }
void prepare_for_learner(vw *all, v_array< example *> &examples)
size_t read_features(vw *all, char *&line, size_t &num_chars)