|
Interest Generator 3.0.1
Religion, hobbies and favourite media by country, age and sex for C++23
|
Interest Generator: draws a person's religion, hobbies and favourite film, game, book and music genres. More...
#include <interestgen.hpp>
Public Types | |
| enum class | table : std::uint8_t { religion , interests , affinity , age_bands , regions } |
| The data tables. More... | |
Public Member Functions | |
| intg () | |
| Default constructor — creates an empty generator with no data. | |
| intg (const intg &)=delete | |
| Not copyable. | |
| intg & | operator= (const intg &)=delete |
| Not copyable. | |
| intg (intg &&) noexcept=default | |
| Movable. | |
| intg & | operator= (intg &&) noexcept=default |
| Movable. | |
| ~intg ()=default | |
| Destructor. | |
| interest | get_interest (const interest_request &request) |
| Generate a random interest for a request. | |
| interest | get_interest (const interest_request &request, std::uint64_t call_seed) const |
| Generate a deterministic interest for a request. | |
| std::vector< interest > | get_interest_batch (const interest_request &request, const core::batch_options &options) |
| Generate a batch of interest results from a seed drawn like get_interest() draws its own. | |
| std::vector< interest > | get_interest_batch (const interest_request &request, const core::batch_options &options, std::uint64_t batch_seed) const |
| Generate a batch of interest results, deterministically. | |
| template<typename Sink > | |
| void | for_each_interest (const interest_request &request, const core::batch_options &options, std::uint64_t batch_seed, Sink &&sink) const |
| Stream a batch of interest results to a sink without holding the whole batch. | |
| template<typename Sink > | |
| void | for_each_interest (const interest_request &request, const core::batch_options &options, Sink &&sink) |
| Stream a batch of interest results with a batch seed drawn like get_interest() draws its own. | |
| bool | has_data () const |
| Check whether religion data has been loaded. | |
| std::size_t | country_count () const |
| Return the number of countries with religion data. | |
| std::size_t | item_count () const |
| Return the number of hobbies and genres loaded, all kinds. | |
| std::span< const religion_share > | shares (std::string_view cca2) const |
| The religion shares loaded for a country. | |
| const std::vector< std::string > & | unresolved () const |
Affinity rows whose target is neither an item key nor class: and an activity class that is loaded. They are ignored, so removing an item needs no other change; this lists them to catch misspelt names. | |
| void | load (const std::filesystem::path &dir) |
| Load a resource directory, replacing all data and layers loaded before. | |
| bool | load (dataset tier) |
| Load a dataset tier, replacing all data and layers loaded before. | |
| void | extend (const std::filesystem::path &path, core::layer_mode mode=core::layer_mode::override) |
| Add data layers from a directory or a file. | |
| const core::layer_info & | extend (table which, core::tsv_document doc, core::layer_mode mode=core::layer_mode::override) |
| Add a data layer to one table. | |
| const core::layer_info & | extend_from_string (table which, std::string_view text, std::string source, core::layer_mode mode=core::layer_mode::override) |
| Add a data layer to one table from TSV text (for example from a WebAssembly host). | |
| const std::vector< core::layer_info > & | layers (table which) const |
| The layers in effect for one table, in order. | |
| std::uint64_t | data_hash () const |
| Hash of the loaded data (all tables), recorded in each result's signature(). | |
Static Public Member Functions | |
| static intg & | instance () |
| Access the global singleton instance. | |
Interest Generator: draws a person's religion, hobbies and favourite film, game, book and music genres.
The country (when not given) comes from the "country" stream of the call seed, the religion from "religion", and each kind of item from a stream of its own ("hobby", "film", "game", "book", "music"), so fields added later leave these unchanged. Output is identical on every compiler and platform (dasmig-core).
An item's weight for a person is its base weight multiplied by factors for the person's sex, age band, country (or else region) and religion group, for the item and for its activity class; a missing factor is 1. Items are drawn without replacement.
A country chosen at random is uniform over the countries loaded; the data has no population column. Pass a country chosen by country-generator for a population-weighted world.
Can be used as a singleton via instance() or constructed independently.
Definition at line 189 of file interestgen.hpp.
|
strong |
The data tables.
Definition at line 193 of file interestgen.hpp.
|
inline |
Default constructor — creates an empty generator with no data.
Definition at line 204 of file interestgen.hpp.
|
inline |
Return the number of countries with religion data.
Definition at line 335 of file interestgen.hpp.
|
inline |
Hash of the loaded data (all tables), recorded in each result's signature().
Definition at line 495 of file interestgen.hpp.
|
inline |
Add data layers from a directory or a file.
A directory may hold any of religion.tsv, interests.tsv, affinity.tsv, age_bands.tsv and regions.tsv; all are applied together or, if any fails, none. A file must have one of those names.
| path | Directory or file. |
| mode | How the layers combine with the data loaded before. |
| core::tsv_error | If a file cannot be read or a row is invalid; the data is then unchanged. |
| std::invalid_argument | If path is a file with another name. |
Definition at line 430 of file interestgen.hpp.
|
inline |
Add a data layer to one table.
Rows are keyed by the table's key columns (see table). In the default override mode a row replaces the loaded row with the same key and new keys are added; a row with disabled set to 1 removes its key.
| which | The table. |
| doc | TSV with that table's columns. |
| mode | How the layer combines with the data loaded before. |
| core::tsv_error | If a row is invalid; the data is unchanged. |
Definition at line 461 of file interestgen.hpp.
|
inline |
Add a data layer to one table from TSV text (for example from a WebAssembly host).
| which | The table. |
| text | TSV with that table's columns. |
| source | Name used in errors. |
| mode | How the layer combines with the data loaded before. |
| core::tsv_error | If a row is invalid; the data is unchanged. |
Definition at line 478 of file interestgen.hpp.
|
inline |
Stream a batch of interest results with a batch seed drawn like get_interest() draws its own.
| Sink | Callable taking (std::uint64_t, interest&). |
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
| sink | Receives the items. |
Definition at line 320 of file interestgen.hpp.
|
inline |
Stream a batch of interest results to a sink without holding the whole batch.
Generates the same items as get_interest_batch() and calls sink(index, item) in index order on the calling thread; index includes options.first.
| Sink | Callable taking (std::uint64_t, interest&); for example core::jsonl_writer or core::csv_writer. |
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
| batch_seed | Seed of the whole batch. |
| sink | Receives the items. |
Definition at line 305 of file interestgen.hpp.
|
inline |
Generate a random interest for a request.
| request | Country, age, sex and counts. Use {} for defaults. |
| std::runtime_error | If no data has been loaded. |
| std::invalid_argument | If the country has no data, or every share of it has weight 0 (or, with no country, no country has a positive weight). |
Definition at line 232 of file interestgen.hpp.
|
inline |
Generate a deterministic interest for a request.
| request | Country, age, sex and counts. Use {} for defaults. |
| call_seed | Seed; the same seed, request and data give the same interest, independent of prior calls. Does not touch the instance's random engine. |
| std::runtime_error | If no data has been loaded. |
| std::invalid_argument | As for the unseeded overload. |
Definition at line 245 of file interestgen.hpp.
|
inline |
Generate a batch of interest results from a seed drawn like get_interest() draws its own.
Item i is get_interest(request, core::item_seed(batch_seed, options.first + i)) for the drawn batch seed; see the seeded overload.
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
Definition at line 262 of file interestgen.hpp.
|
inline |
Generate a batch of interest results, deterministically.
Item i is exactly get_interest(request, core::item_seed(batch_seed, options.first + i)), so an item replays on its own, a range can be generated directly, two batches that follow each other equal one, and the output does not depend on options.threads.
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness (see core::unique_rule; the rule must be made for interest). |
| batch_seed | Seed of the whole batch. |
| std::invalid_argument | If the request or options are invalid. |
| std::runtime_error | If no data has been loaded, or a unique batch runs out of attempts. |
Definition at line 283 of file interestgen.hpp.
|
inline |
Check whether religion data has been loaded.
Definition at line 331 of file interestgen.hpp.
|
inlinestatic |
Access the global singleton instance.
Loads the lite tier (or full if lite is missing) on first access, from compiled-in data or an auto-probed resources/.
Definition at line 217 of file interestgen.hpp.
|
inline |
Return the number of hobbies and genres loaded, all kinds.
Definition at line 339 of file interestgen.hpp.
|
inline |
The layers in effect for one table, in order.
| which | The table. |
Definition at line 487 of file interestgen.hpp.
|
inline |
Load a resource directory, replacing all data and layers loaded before.
The directory must contain religion.tsv; the other tables are read when present (a missing one leaves its table empty, so a directory with only religion.tsv draws no hobbies or media). A path without religion.tsv is ignored.
| dir | Directory such as resources/lite. |
| core::tsv_error | If a file is malformed; the data is then unchanged. |
Definition at line 380 of file interestgen.hpp.
|
inline |
Load a dataset tier, replacing all data and layers loaded before.
Uses the compiled-in copy when the library is built with DASMIG_INTEREST_EMBED, otherwise searches resources/, ../resources, interest-generator/resources and ../interest-generator/resources.
| tier | The tier. |
| core::tsv_error | If a file is malformed. |
Definition at line 402 of file interestgen.hpp.
|
inline |
The religion shares loaded for a country.
| cca2 | ISO 3166-1 alpha-2 country code. |
Definition at line 349 of file interestgen.hpp.
|
inline |
Affinity rows whose target is neither an item key nor class: and an activity class that is loaded. They are ignored, so removing an item needs no other change; this lists them to catch misspelt names.
Definition at line 362 of file interestgen.hpp.